PageRenderTime 44ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mitreid-connect/OpenID-Connect-Java-Spring-Server
JavaScript | 341 lines | 231 code | 93 blank | 17 comment | 7 complexity | fcc09ba8fb8e5645ff85da948424977c MD5 | raw file
  1. var ClientModel = Backbone.Model.extend({
  2. idAttribute: "clientId",
  3. initialize: function () {
  4. // bind validation errors to dom elements
  5. // this will display form elements in red if they are not valid
  6. this.bind('error', function(model, errs) {
  7. _.map(errs, function (val, elID) {
  8. $('#' + elID).addClass('error');
  9. });
  10. });
  11. },
  12. validate:{
  13. clientName:{
  14. /* required:true,
  15. pattern:/^[\w ]+$/,
  16. minlength:3,*/
  17. maxlength:100
  18. },
  19. clientDescription:{
  20. /*required:true,
  21. pattern:/^[\w ]+$/,
  22. minlength:3,*/
  23. maxlength:200
  24. },
  25. accessTokenTimeout: {
  26. required: true,
  27. type:"number"
  28. },
  29. refreshTokenTimeout: {
  30. required: true,
  31. type:"number"
  32. },
  33. registeredRedirectUri: {
  34. custom: 'validateURI'
  35. }
  36. },
  37. validateURI: function(attributeName, attributeValue) {
  38. var expression = /^(?:([a-z0-9+.-]+:\/\/)((?:(?:[a-z0-9-._~!$&'()*+,;=:]|%[0-9A-F]{2})*)@)?((?:[a-z0-9-._~!$&'()*+,;=]|%[0-9A-F]{2})*)(:(?:\d*))?(\/(?:[a-z0-9-._~!$&'()*+,;=:@\/]|%[0-9A-F]{2})*)?|([a-z0-9+.-]+:)(\/?(?:[a-z0-9-._~!$&'()*+,;=:@]|%[0-9A-F]{2})+(?:[a-z0-9-._~!$&'()*+,;=:@\/]|%[0-9A-F]{2})*)?)(\?(?:[a-z0-9-._~!$&'()*+,;=:\/?@]|%[0-9A-F]{2})*)?(#(?:[a-z0-9-._~!$&'()*+,;=:\/?@]|%[0-9A-F]{2})*)?$/i;
  39. var regex = new RegExp(expression);
  40. for (var i in attributeValue) {
  41. if (!attributeValue[i].match(regex)) {
  42. return "Invalid URI";
  43. }
  44. }
  45. },
  46. // We can pass it default values.
  47. defaults:{
  48. clientName:"",
  49. clientSecret:"",
  50. registeredRedirectUri:[""],
  51. authorizedGrantTypes:[],
  52. scope:["openid"],
  53. authorities:[],
  54. clientDescription:"",
  55. clientId:null,
  56. allowRefresh:false,
  57. accessTokenTimeout: 0,
  58. refreshTokenTimeout: 0
  59. },
  60. urlRoot:"api/clients"
  61. });
  62. var ClientCollection = Backbone.Collection.extend({
  63. initialize: function() {
  64. this.fetch();
  65. },
  66. model:ClientModel,
  67. url:"api/clients"
  68. });
  69. var ClientView = Backbone.View.extend({
  70. tagName: 'tr',
  71. initialize:function () {
  72. if (!this.template) {
  73. this.template = _.template($('#tmpl-client').html());
  74. }
  75. this.model.bind('change', this.render, this);
  76. },
  77. render:function (eventName) {
  78. this.$el.html(this.template(this.model.toJSON()));
  79. return this;
  80. },
  81. events:{
  82. "click .btn-edit":"editClient",
  83. "click .btn-delete":"deleteClient"
  84. },
  85. editClient:function () {
  86. app.navigate('client/' + this.model.id, {trigger: true});
  87. },
  88. deleteClient:function () {
  89. if (confirm("Are you sure sure you would like to delete this client?")) {
  90. var self = this;
  91. this.model.destroy({
  92. success:function () {
  93. self.$el.fadeTo("fast", 0.00, function () { //fade
  94. $(this).slideUp("fast", function () { //slide up
  95. $(this).remove(); //then remove from the DOM
  96. });
  97. });
  98. }
  99. });
  100. app.clientListView.delegateEvents();
  101. }
  102. return false;
  103. },
  104. close:function () {
  105. $(this.el).unbind();
  106. $(this.el).empty();
  107. }
  108. });
  109. var ClientListView = Backbone.View.extend({
  110. tagName: 'span',
  111. initialize:function () {
  112. this.model.bind("reset", this.render, this);
  113. },
  114. events:{
  115. "click .btn-primary":"newClient"
  116. },
  117. newClient:function () {
  118. this.remove();
  119. app.navigate('client/new', {trigger: true});
  120. },
  121. render:function (eventName) {
  122. // append and render table structure
  123. $(this.el).html($('#tmpl-client-table').html());
  124. _.each(this.model.models, function (client) {
  125. $("#client-table",this.el).append(new ClientView({model:client}).render().el);
  126. }, this);
  127. return this;
  128. }
  129. });
  130. var ClientFormView = Backbone.View.extend({
  131. tagName:"span",
  132. initialize:function () {
  133. if (!this.template) {
  134. this.template = _.template($('#tmpl-client-form').html());
  135. }
  136. },
  137. events:{
  138. "click .btn-primary":"saveClient"
  139. },
  140. saveClient:function (event) {
  141. $('.control-group').removeClass('error');
  142. var valid = this.model.set({
  143. clientName:$('#clientName input').val(),
  144. clientSecret:$('#clientSecret input').val(),
  145. registeredRedirectUri:$.trim($('#registeredRedirectUri textarea').val()).replace(/ /g,'').split("\n"),
  146. clientDescription:$('#clientDescription textarea').val(),
  147. allowRefresh:$('#allowRefresh').is(':checked'),
  148. accessTokenTimeout: $('#accessTokenTimeout input').val(),
  149. refreshTokenTimeout: $('#refreshTokenTimeout input').val(),
  150. scope:$.map($('#scope textarea').val().replace(/,$/,'').replace(/\s/g,' ').split(","), $.trim)
  151. });
  152. if (valid) {
  153. this.model.save(this.model, {
  154. success:function () {
  155. app.navigate('clients', {trigger:true});
  156. },
  157. error:function () {
  158. }
  159. });
  160. if (this.model.isNew()) {
  161. var self = this;
  162. app.clientList.create(this.model, {
  163. success:function () {
  164. app.navigate('clients', {trigger:true});
  165. },
  166. error:function () {
  167. }
  168. });
  169. }
  170. }
  171. return false;
  172. },
  173. render:function (eventName) {
  174. $(this.el).html(this.template(this.model.toJSON()));
  175. return this;
  176. }
  177. });
  178. var URLListView = Backbone.View.extend({
  179. tagName: 'span',
  180. initialize:function () {
  181. },
  182. events:{
  183. "click .btn-primary":"save"
  184. },
  185. save:function () {
  186. },
  187. render:function (eventName) {
  188. // append and render
  189. $(this.el).html($('#tmpl-url-list').html());
  190. return this;
  191. }
  192. });
  193. // Router
  194. var AppRouter = Backbone.Router.extend({
  195. routes:{
  196. "clients":"listClients",
  197. "client/new":"newClient",
  198. "client/:id":"editClient",
  199. "white_list":"whiteList"
  200. },
  201. initialize:function () {
  202. this.clientList = new ClientCollection();
  203. this.clientListView = new ClientListView({model:this.clientList});
  204. this.whiteListView = new URLListView();
  205. this.blackListView = new URLListView();
  206. this.startAfter([this.clientList]);
  207. },
  208. startAfter:function (collections) {
  209. // Start history when required collections are loaded
  210. var start = _.after(collections.length, _.once(function () {
  211. Backbone.history.start()
  212. }));
  213. _.each(collections, function (collection) {
  214. collection.bind('reset', start, Backbone.history)
  215. });
  216. },
  217. listClients:function () {
  218. $('#content').html(this.clientListView.render().el);
  219. this.clientListView.delegateEvents();
  220. },
  221. newClient:function() {
  222. this.clientFormView = new ClientFormView({model:new ClientModel()});
  223. $('#content').html(this.clientFormView.render().el);
  224. },
  225. editClient:function(id) {
  226. var client = this.clientList.get(id);
  227. this.clientFormView = new ClientFormView({model:client});
  228. $('#content').html(this.clientFormView.render().el);
  229. },
  230. whiteList:function () {
  231. $('#content').html(this.whiteListView.render().el);
  232. }
  233. });
  234. // holds the global app.
  235. // this gets init after the templates load
  236. var app = null;
  237. // main
  238. $(function () {
  239. jQuery.ajaxSetup({async:false});
  240. var _load = function (templates) {
  241. $('body').append(templates);
  242. };
  243. // load templates and append them to the body
  244. $.get('resources/template/client.html', _load);
  245. $.get('resources/template/list.html', _load);
  246. jQuery.ajaxSetup({async:true});
  247. app = new AppRouter();
  248. });