PageRenderTime 69ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/openid-connect-server-webapp/src/main/webapp/resources/js/scope.js

https://gitlab.com/jslee1/OpenID-Connect-Java-Spring-Server
JavaScript | 391 lines | 292 code | 76 blank | 23 comment | 21 complexity | 617421473ca46c0792a7079c3e5ad5da MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright 2016 The MITRE Corporation
  3. * and the MIT Internet Trust Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *******************************************************************************/
  17. var SystemScopeModel = Backbone.Model.extend({
  18. idAttribute: 'id',
  19. defaults:{
  20. id:null,
  21. description:null,
  22. icon:null,
  23. value:null,
  24. defaultScope:false,
  25. restricted:false,
  26. structured:false,
  27. structuredParamDescription:null,
  28. structuredValue:null
  29. },
  30. urlRoot: 'api/scopes'
  31. });
  32. var SystemScopeCollection = Backbone.Collection.extend({
  33. idAttribute: 'id',
  34. model: SystemScopeModel,
  35. url: 'api/scopes',
  36. defaultScopes: function() {
  37. filtered = this.filter(function(scope) {
  38. return scope.get("defaultScope") === true;
  39. });
  40. return new SystemScopeCollection(filtered);
  41. },
  42. unrestrictedScopes: function() {
  43. filtered = this.filter(function(scope) {
  44. return scope.get("restricted") !== true;
  45. });
  46. return new SystemScopeCollection(filtered);
  47. },
  48. defaultUnrestrictedScopes: function() {
  49. filtered = this.filter(function(scope) {
  50. return scope.get("defaultScope") === true && scope.get("restricted") !== true;
  51. });
  52. return new SystemScopeCollection(filtered);
  53. },
  54. getByValue: function(value) {
  55. var scopes = this.where({value: value});
  56. if (scopes.length == 1) {
  57. return scopes[0];
  58. } else {
  59. return null;
  60. }
  61. }
  62. });
  63. var SystemScopeView = Backbone.View.extend({
  64. tagName: 'tr',
  65. initialize:function (options) {
  66. this.options = options;
  67. if (!this.template) {
  68. this.template = _.template($('#tmpl-system-scope').html());
  69. }
  70. this.model.bind('change', this.render, this);
  71. },
  72. events: {
  73. 'click .btn-edit':'editScope',
  74. 'click .btn-delete':'deleteScope'
  75. },
  76. editScope:function(e) {
  77. e.preventDefault();
  78. app.navigate('admin/scope/' + this.model.id, {trigger: true});
  79. },
  80. render:function (eventName) {
  81. this.$el.html(this.template(this.model.toJSON()));
  82. $('.restricted', this.el).tooltip({title: $.t('scope.system-scope-table.tooltip-restricted')});
  83. $('.default', this.el).tooltip({title: $.t('scope.system-scope-table.tooltip-default')});
  84. return this;
  85. $(this.el).i18n();
  86. },
  87. deleteScope:function (e) {
  88. e.preventDefault();
  89. if (confirm($.t("scope.system-scope-table.confirm"))) {
  90. var _self = this;
  91. this.model.destroy({
  92. dataType: false, processData: false,
  93. success:function () {
  94. _self.$el.fadeTo("fast", 0.00, function () { //fade
  95. $(this).slideUp("fast", function () { //slide up
  96. $(this).remove(); //then remove from the DOM
  97. _self.parentView.togglePlaceholder();
  98. });
  99. });
  100. },
  101. error:function (error, response) {
  102. //Pull out the response text.
  103. var responseJson = JSON.parse(response.responseText);
  104. //Display an alert with an error message
  105. $('#modalAlert div.modal-header').html(responseJson.error);
  106. $('#modalAlert div.modal-body').html(responseJson.error_description);
  107. $("#modalAlert").modal({ // wire up the actual modal functionality and show the dialog
  108. "backdrop" : "static",
  109. "keyboard" : true,
  110. "show" : true // ensure the modal is shown immediately
  111. });
  112. }
  113. });
  114. _self.parentView.delegateEvents();
  115. }
  116. return false;
  117. },
  118. close:function () {
  119. $(this.el).unbind();
  120. $(this.el).empty();
  121. }
  122. });
  123. var SystemScopeListView = Backbone.View.extend({
  124. tagName: 'span',
  125. initialize:function(options) {
  126. this.options = options;
  127. },
  128. load:function(callback) {
  129. if (this.model.isFetched) {
  130. callback();
  131. return;
  132. }
  133. $('#loadingbox').sheet('show');
  134. $('#loading').html(
  135. '<span class="label" id="loading-scopes">' + $.t('common.scopes') + '</span> '
  136. );
  137. $.when(this.model.fetchIfNeeded({success:function(e) {$('#loading-scopes').addClass('label-success');}}))
  138. .done(function() {
  139. $('#loadingbox').sheet('hide');
  140. callback();
  141. });
  142. },
  143. events:{
  144. "click .new-scope":"newScope",
  145. "click .refresh-table":"refreshTable"
  146. },
  147. newScope:function(e) {
  148. this.remove();
  149. app.navigate('admin/scope/new', {trigger: true});
  150. },
  151. refreshTable:function(e) {
  152. var _self = this;
  153. $('#loadingbox').sheet('show');
  154. $('#loading').html(
  155. '<span class="label" id="loading-scopes">' + $.t('common.scopes') + '</span> '
  156. );
  157. $.when(this.model.fetch({success:function(e) {$('#loading-scopes').addClass('label-success');}}))
  158. .done(function() {
  159. $('#loadingbox').sheet('hide');
  160. _self.render();
  161. });
  162. },
  163. togglePlaceholder:function() {
  164. if (this.model.length > 0) {
  165. $('#scope-table', this.el).show();
  166. $('#scope-table-empty', this.el).hide();
  167. } else {
  168. $('#scope-table', this.el).hide();
  169. $('#scope-table-empty', this.el).show();
  170. }
  171. },
  172. render: function (eventName) {
  173. // append and render the table structure
  174. $(this.el).html($('#tmpl-system-scope-table').html());
  175. var _self = this;
  176. _.each(this.model.models, function (scope) {
  177. var view = new SystemScopeView({model: scope});
  178. view.parentView = _self;
  179. $("#scope-table", _self.el).append(view.render().el);
  180. }, this);
  181. this.togglePlaceholder();
  182. $(this.el).i18n();
  183. return this;
  184. }
  185. });
  186. var SystemScopeFormView = Backbone.View.extend({
  187. tagName: 'span',
  188. initialize:function(options) {
  189. this.options = options;
  190. if (!this.template) {
  191. this.template = _.template($('#tmpl-system-scope-form').html());
  192. }
  193. if (!this.iconTemplate) {
  194. this.iconTemplate = _.template($('#tmpl-system-scope-icon').html());
  195. }
  196. // initialize our icon set into slices for the selector
  197. if (!this.bootstrapIcons) {
  198. this.bootstrapIcons = [];
  199. var iconList = ['glass', 'music', 'search', 'envelope', 'heart', 'star', 'star-empty',
  200. 'user', 'film', 'th-large', 'th', 'th-list', 'ok', 'remove', 'zoom-in',
  201. 'zoom-out', 'off', 'signal', 'cog', 'trash', 'home', 'file', 'time', 'road',
  202. 'download-alt', 'download', 'upload', 'inbox', 'play-circle', 'repeat',
  203. 'refresh', 'list-alt', 'lock', 'flag', 'headphones', 'volume-off',
  204. 'volume-down', 'volume-up', 'qrcode', 'barcode', 'tag', 'tags', 'book',
  205. 'bookmark', 'print', 'camera', 'font', 'bold', 'italic', 'text-height',
  206. 'text-width', 'align-left', 'align-center', 'align-right', 'align-justify',
  207. 'list', 'indent-left', 'indent-right', 'facetime-video', 'picture', 'pencil',
  208. 'map-marker', 'adjust', 'tint', 'edit', 'share', 'check', 'move', 'step-backward',
  209. 'fast-backward', 'backward', 'play', 'pause', 'stop', 'forward', 'fast-forward',
  210. 'step-forward', 'eject', 'chevron-left', 'chevron-right', 'plus-sign',
  211. 'minus-sign', 'remove-sign', 'ok-sign', 'question-sign', 'info-sign',
  212. 'screenshot', 'remove-circle', 'ok-circle', 'ban-circle', 'arrow-left',
  213. 'arrow-right', 'arrow-up', 'arrow-down', 'share-alt', 'resize-full', 'resize-small',
  214. 'plus', 'minus', 'asterisk', 'exclamation-sign', 'gift', 'leaf', 'fire',
  215. 'eye-open', 'eye-close', 'warning-sign', 'plane', 'calendar', 'random',
  216. 'comment', 'magnet', 'chevron-up', 'chevron-down', 'retweet', 'shopping-cart',
  217. 'folder-close', 'folder-open', 'resize-vertical', 'resize-horizontal',
  218. 'hdd', 'bullhorn', 'bell', 'certificate', 'thumbs-up', 'thumbs-down',
  219. 'hand-right', 'hand-left', 'hand-up', 'hand-down', 'circle-arrow-right',
  220. 'circle-arrow-left', 'circle-arrow-up', 'circle-arrow-down', 'globe',
  221. 'wrench', 'tasks', 'filter', 'briefcase', 'fullscreen'];
  222. var size = 3;
  223. while (iconList.length > 0) {
  224. this.bootstrapIcons.push(iconList.splice(0, size));
  225. }
  226. }
  227. },
  228. events:{
  229. 'click .btn-save':'saveScope',
  230. 'click .btn-cancel': function() {app.navigate('admin/scope', {trigger: true}); },
  231. 'click .btn-icon':'selectIcon',
  232. 'change #isStructured input':'toggleStructuredParamDescription'
  233. },
  234. load:function(callback) {
  235. if (this.model.isFetched) {
  236. callback();
  237. return;
  238. }
  239. $('#loadingbox').sheet('show');
  240. $('#loading').html(
  241. '<span class="label" id="loading-scopes">' + $.t("common.scopes") + '</span> '
  242. );
  243. $.when(this.model.fetchIfNeeded({success:function(e) {$('#loading-scopes').addClass('label-success');}}))
  244. .done(function() {
  245. $('#loadingbox').sheet('hide');
  246. callback();
  247. });
  248. },
  249. toggleStructuredParamDescription:function(e) {
  250. if ($('#isStructured input', this.el).is(':checked')) {
  251. $('#structuredParamDescription', this.el).show();
  252. } else {
  253. $('#structuredParamDescription', this.el).hide();
  254. }
  255. },
  256. saveScope:function(e) {
  257. e.preventDefault();
  258. var value = $('#value input').val();
  259. if (value == null || value.trim() == "") {
  260. // error: can't have a blank scope
  261. return false;
  262. }
  263. var valid = this.model.set({
  264. value:value,
  265. description:$('#description textarea').val(),
  266. icon:$('#iconDisplay input').val(),
  267. defaultScope:$('#defaultScope input').is(':checked'),
  268. restricted:$('#restricted input').is(':checked'),
  269. structured:$('#isStructured input').is(':checked'),
  270. structuredParamDescription:$('#structuredParamDescription input').val()
  271. });
  272. if (valid) {
  273. var _self = this;
  274. this.model.save({}, {
  275. success:function() {
  276. app.systemScopeList.add(_self.model);
  277. app.navigate('admin/scope', {trigger: true});
  278. },
  279. error:function(error, response) {
  280. //Pull out the response text.
  281. var responseJson = JSON.parse(response.responseText);
  282. //Display an alert with an error message
  283. $('#modalAlert div.modal-header').html(responseJson.error);
  284. $('#modalAlert div.modal-body').html(responseJson.error_description);
  285. $("#modalAlert").modal({ // wire up the actual modal functionality and show the dialog
  286. "backdrop" : "static",
  287. "keyboard" : true,
  288. "show" : true // ensure the modal is shown immediately
  289. });
  290. }
  291. });
  292. }
  293. return false;
  294. },
  295. selectIcon:function(e) {
  296. e.preventDefault();
  297. var icon = e.target.value;
  298. $('#iconDisplay input').val(icon);
  299. $('#iconDisplay #iconName').html(icon);
  300. $('#iconDisplay i').removeClass();
  301. $('#iconDisplay i').addClass('icon-' + icon);
  302. $('#iconDisplay i').addClass('icon-white');
  303. $('#iconSelector').modal('hide');
  304. return false;
  305. },
  306. render: function(eventName) {
  307. this.$el.html(this.template(this.model.toJSON()));
  308. _.each(this.bootstrapIcons, function (items) {
  309. $("#iconSelector .modal-body", this.el).append(this.iconTemplate({items:items}));
  310. }, this);
  311. this.toggleStructuredParamDescription();
  312. $(this.el).i18n();
  313. return this;
  314. }
  315. });