PageRenderTime 91ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mitreid-connect/OpenID-Connect-Java-Spring-Server
JavaScript | 223 lines | 155 code | 52 blank | 16 comment | 7 complexity | 76c474d1202eb7054a04a14e181bec12 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright 2018 The MIT Internet Trust Consortium
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *******************************************************************************/
  16. var BlackListModel = Backbone.Model.extend({
  17. idAttribute: 'id',
  18. urlRoot: 'api/blacklist'
  19. });
  20. var BlackListCollection = Backbone.Collection.extend({
  21. initialize: function() {
  22. },
  23. url: "api/blacklist"
  24. });
  25. var BlackListListView = Backbone.View.extend({
  26. tagName: 'span',
  27. initialize: function(options) {
  28. this.options = options;
  29. },
  30. load: function(callback) {
  31. if (this.collection.isFetched) {
  32. callback();
  33. return;
  34. }
  35. $('#loadingbox').sheet('show');
  36. $('#loading').html('<span class="label" id="loading-blacklist">' + $.t('admin.blacklist') + '</span> ');
  37. $.when(this.collection.fetchIfNeeded({
  38. success: function(e) {
  39. $('#loading-blacklist').addClass('label-success');
  40. },
  41. error: app.errorHandlerView.handleError()
  42. })).done(function() {
  43. $('#loadingbox').sheet('hide');
  44. callback();
  45. });
  46. },
  47. events: {
  48. "click .refresh-table": "refreshTable",
  49. "click .btn-add": "addItem",
  50. "submit #add-blacklist form": "addItem"
  51. },
  52. refreshTable: function(e) {
  53. e.preventDefault();
  54. var _self = this;
  55. $('#loadingbox').sheet('show');
  56. $('#loading').html('<span class="label" id="loading-blacklist">' + $.t('admin.blacklist') + '</span> ');
  57. $.when(this.collection.fetch()).done(function() {
  58. $('#loadingbox').sheet('hide');
  59. _self.render();
  60. });
  61. },
  62. togglePlaceholder: function() {
  63. if (this.collection.length > 0) {
  64. $('#blacklist-table', this.el).show();
  65. $('#blacklist-table-empty', this.el).hide();
  66. } else {
  67. $('#blacklist-table', this.el).hide();
  68. $('#blacklist-table-empty', this.el).show();
  69. }
  70. },
  71. render: function(eventName) {
  72. $(this.el).html($('#tmpl-blacklist-table').html());
  73. var _self = this;
  74. _.each(this.collection.models, function(blacklist) {
  75. var view = new BlackListWidgetView({
  76. model: blacklist
  77. });
  78. view.parentView = _self;
  79. $("#blacklist-table", _self.el).append(view.render().el);
  80. }, this);
  81. this.togglePlaceholder();
  82. $(this.el).i18n();
  83. return this;
  84. },
  85. addItem: function(e) {
  86. e.preventDefault();
  87. var input_value = $("#blacklist-uri", this.el).val().trim();
  88. if (input_value === "") {
  89. return;
  90. }
  91. // TODO: URI/pattern validation, check against existing clients
  92. var item = new BlackListModel({
  93. uri: input_value
  94. });
  95. var _self = this; // closures...
  96. item.save({}, {
  97. success: function() {
  98. _self.collection.add(item);
  99. _self.render();
  100. },
  101. error: app.errorHandlerView.handleError()
  102. });
  103. }
  104. });
  105. var BlackListWidgetView = Backbone.View.extend({
  106. tagName: 'tr',
  107. initialize: function(options) {
  108. this.options = options;
  109. if (!this.template) {
  110. this.template = _.template($('#tmpl-blacklist-item').html());
  111. }
  112. },
  113. render: function() {
  114. this.$el.html(this.template(this.model.toJSON()));
  115. return this;
  116. },
  117. events: {
  118. 'click .btn-delete': 'deleteBlacklist'
  119. },
  120. deleteBlacklist: function(e) {
  121. e.preventDefault();
  122. if (confirm($.t("blacklist.confirm"))) {
  123. var _self = this;
  124. this.model.destroy({
  125. dataType: false,
  126. processData: false,
  127. success: function() {
  128. _self.$el.fadeTo("fast", 0.00, function() { // fade
  129. $(this).slideUp("fast", function() { // slide up
  130. $(this).remove(); // then remove from the DOM
  131. _self.parentView.togglePlaceholder();
  132. });
  133. });
  134. },
  135. error: app.errorHandlerView.handleError()
  136. });
  137. _self.parentView.delegateEvents();
  138. }
  139. return false;
  140. }
  141. });
  142. ui.routes.push({
  143. path: "admin/blacklist",
  144. name: "blackList",
  145. callback: function() {
  146. if (!isAdmin()) {
  147. this.root();
  148. return;
  149. }
  150. this.breadCrumbView.collection.reset();
  151. this.breadCrumbView.collection.add([{
  152. text: $.t('admin.home'),
  153. href: ""
  154. }, {
  155. text: $.t('admin.manage-blacklist'),
  156. href: "manage/#admin/blacklist"
  157. }]);
  158. this.updateSidebar('admin/blacklist');
  159. var view = new BlackListListView({
  160. collection: this.blackListList
  161. });
  162. view.load(function(collection, response, options) {
  163. $('#content').html(view.render().el);
  164. setPageTitle($.t('admin.manage-blacklist'));
  165. });
  166. }
  167. });
  168. ui.templates.push('resources/template/blacklist.html');
  169. ui.init.push(function(app) {
  170. app.blackListList = new BlackListCollection();
  171. });