PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/jslee1/OpenID-Connect-Java-Spring-Server
JavaScript | 211 lines | 142 code | 48 blank | 21 comment | 7 complexity | 49fd3d3dc372aab6a34be1aa93330f4a 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 BlackListModel = Backbone.Model.extend({
  18. idAttribute: 'id',
  19. urlRoot: 'api/blacklist'
  20. });
  21. var BlackListCollection = Backbone.Collection.extend({
  22. initialize: function() { },
  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(
  37. '<span class="label" id="loading-blacklist">' + $.t('admin.blacklist') + '</span> '
  38. );
  39. $.when(this.collection.fetchIfNeeded({success:function(e) {$('#loading-blacklist').addClass('label-success');}}))
  40. .done(function() {
  41. $('#loadingbox').sheet('hide');
  42. callback();
  43. });
  44. },
  45. events: {
  46. "click .refresh-table":"refreshTable",
  47. "click .btn-add":"addItem",
  48. "submit #add-blacklist form":"addItem"
  49. },
  50. refreshTable:function(e) {
  51. e.preventDefault();
  52. var _self = this;
  53. $('#loadingbox').sheet('show');
  54. $('#loading').html(
  55. '<span class="label" id="loading-blacklist">' + $.t('admin.blacklist') + '</span> '
  56. );
  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({model: blacklist});
  76. view.parentView = _self;
  77. $("#blacklist-table", _self.el).append(view.render().el);
  78. }, this);
  79. this.togglePlaceholder();
  80. $(this.el).i18n();
  81. return this;
  82. },
  83. addItem:function(e) {
  84. e.preventDefault();
  85. var input_value = $("#blacklist-uri", this.el).val().trim();
  86. if (input_value === "") {
  87. return;
  88. }
  89. // TODO: URI/pattern validation, check against existing clients
  90. var item = new BlackListModel({
  91. uri: input_value
  92. });
  93. var _self = this; // closures...
  94. item.save({}, {
  95. success:function() {
  96. _self.collection.add(item);
  97. _self.render();
  98. },
  99. error:function(error, response) {
  100. //Pull out the response text.
  101. var responseJson = JSON.parse(response.responseText);
  102. //Display an alert with an error message
  103. $('#modalAlert div.modal-header').html(responseJson.error);
  104. $('#modalAlert div.modal-body').html(responseJson.error_description);
  105. $("#modalAlert").modal({ // wire up the actual modal functionality and show the dialog
  106. "backdrop" : "static",
  107. "keyboard" : true,
  108. "show" : true // ensure the modal is shown immediately
  109. });
  110. }
  111. });
  112. }
  113. });
  114. var BlackListWidgetView = Backbone.View.extend({
  115. tagName: 'tr',
  116. initialize:function(options) {
  117. this.options = options;
  118. if (!this.template) {
  119. this.template = _.template($('#tmpl-blacklist-item').html());
  120. }
  121. },
  122. render:function() {
  123. this.$el.html(this.template(this.model.toJSON()));
  124. return this;
  125. },
  126. events:{
  127. 'click .btn-delete':'deleteBlacklist'
  128. },
  129. deleteBlacklist:function (e) {
  130. e.preventDefault();
  131. if (confirm($.t("blacklist.confirm"))) {
  132. var _self = this;
  133. this.model.destroy({
  134. dataType: false, processData: false,
  135. success:function () {
  136. _self.$el.fadeTo("fast", 0.00, function () { //fade
  137. $(this).slideUp("fast", function () { //slide up
  138. $(this).remove(); //then remove from the DOM
  139. _self.parentView.togglePlaceholder();
  140. });
  141. });
  142. },
  143. error:function (model, response) {
  144. //Pull out the response text.
  145. var responseJson = {error: 'Error', error_description: 'Error.'};
  146. if (response) {
  147. responseJson = JSON.parse(response.responseText);
  148. }
  149. //Display an alert with an error message
  150. $('#modalAlert div.modal-header').html(responseJson.error);
  151. $('#modalAlert div.modal-body').html(responseJson.error_description);
  152. $("#modalAlert").modal({ // wire up the actual modal functionality and show the dialog
  153. "backdrop" : "static",
  154. "keyboard" : true,
  155. "show" : true // ensure the modal is shown immediately
  156. });
  157. }
  158. });
  159. _self.parentView.delegateEvents();
  160. }
  161. return false;
  162. }
  163. });