PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/easysoa-web/www/easysoa/integration/dashboard/js/App.js

https://github.com/easysoa/EasySOA
JavaScript | 302 lines | 256 code | 32 blank | 14 comment | 66 complexity | 7ea0fec828b84a4ad58c2e83ff530b6d MD5 | raw file
  1. $(function(){
  2. window.ValidatorName = Backbone.Model.extend({});
  3. window.Validators = Backbone.Collection.extend({
  4. model: ValidatorName
  5. });
  6. window.AppView = Backbone.View.extend({
  7. /** Backbone data: view location **/
  8. el: $('#container'),
  9. events: {
  10. 'click .serviceEntryLocalServiceName' : 'selectLocalItem',
  11. 'click .serviceEntryReferencedServiceName' : 'selectReferencedItem',
  12. 'click #defineReferenceLink' : 'defineReferenceLink',
  13. 'click #breakReferenceLink' : 'breakReferenceLink',
  14. 'click .actionButton' : 'updateLifeCycleState'
  15. },
  16. /** Tags */
  17. entriesTableTag: $('#serviceentries'),
  18. entriesHeaderTag: $('#serviceentriesheader'),
  19. placeHolderTag: $('#placeholder'),
  20. errorHolder: $('#errorHolder'),
  21. /** Templates */
  22. errorTemplate: _.template($('#error-template').html()),
  23. entriesheadervalidatorTemplate: _.template($('#serviceentriescolumn-template').html()),
  24. correlationResultTemplate: _.template($('#correlationresult-template').html()),
  25. initialize: function() {
  26. // Makes sure the bound functions are called with the app as 'this'
  27. _.bindAll(this, 'add', 'hidePlaceholder', 'loadServices',
  28. 'selectLocalItem', 'selectReferencedItem', 'defineReferenceLink',
  29. 'loadValidators', 'addValidatorColumn', 'showError', 'updateLifeCycleState');
  30. // Create the model and bind it to the app functions
  31. this.serviceEntries = new Services();
  32. this.validatorNames = new Validators();
  33. this.services = new Services();
  34. this.selectedLocal = null;
  35. this.selectedReference = null;
  36. this.serviceEntries.bind('add', this.add);
  37. this.validatorNames.bind('add', this.addValidatorColumn);
  38. // Load the models
  39. this.loadValidators();
  40. this.loadServices();
  41. },
  42. loadValidators: function() {
  43. if (window.username != undefined) {
  44. var app = this;
  45. $.ajax({
  46. url: '/nuxeo/dashboard/validators',
  47. success: function(data, textStatus, jqXHR) {
  48. var names = $.parseJSON(jqXHR.responseText);
  49. for (i in names) {
  50. app.validatorNames.add(new ValidatorName({name: names[i]}));
  51. }
  52. },
  53. error: function(data) {
  54. app.showError("Failed to query validators");
  55. }
  56. });
  57. // Set up deployables view
  58. var deployablesModel = new DeployablesModel();
  59. new DeployablesView(deployablesModel);
  60. deployablesModel.setMyEnv(window.username);
  61. deployablesModel.setRefEnv('Master');
  62. }
  63. else {
  64. // Wait for username to be retrieved
  65. setTimeout(this.loadValidators, 100);
  66. }
  67. },
  68. loadServices: function() {
  69. if (window.username != undefined) {
  70. var app = this;
  71. $.ajax({
  72. url: '/nuxeo/dashboard/services/' + window.username,
  73. success: function(data, textStatus, jqXHR) {
  74. app.hidePlaceholder();
  75. var result = $.parseJSON(jqXHR.responseText);
  76. if (result.result == undefined) {
  77. for (var i in result) {
  78. var newService = new Service(result[i]);
  79. app.serviceEntries.add(newService);
  80. app.services.add(newService);
  81. app.services.add(newService.get('referencedService'));
  82. }
  83. }
  84. else {
  85. app.showError(result.result);
  86. }
  87. },
  88. error: function(data) {
  89. app.showError("Failed to query services");
  90. }
  91. });
  92. }
  93. else {
  94. // Wait for username to be retrieved
  95. setTimeout(this.loadServices, 100);
  96. }
  97. },
  98. selectLocalItem: function(event) {
  99. // Select item
  100. var target = $(event.target);
  101. var targetWasSelected = target.hasClass("selectedLocal");
  102. $('.serviceEntryLocalServiceName').removeClass("selectedLocal");
  103. if (!targetWasSelected) {
  104. target.addClass("selectedLocal");
  105. this.selectedLocal = $(".selectedLocal").attr('cid');
  106. }
  107. else {
  108. this.selectedLocal = null;
  109. }
  110. // Show correlation results if service has no reference
  111. $('.correlationResult').remove();
  112. if (this.selectedLocal != null) {
  113. var localServiceModel = this.serviceEntries.getByCid(this.selectedLocal);
  114. if (localServiceModel.get('referencedService').cid == null) {
  115. var app = this;
  116. $.ajax({
  117. url: '/nuxeo/dashboard/service/' + localServiceModel.get('id') + '/matches',
  118. success: function(data, textStatus, jqXHR) {
  119. // If the user didn't click somewhere before
  120. if (app.selectedLocal == localServiceModel.cid) {
  121. var matches = $.parseJSON(jqXHR.responseText);
  122. for (var i in matches) {
  123. $('#'+matches[i].id).append(app.correlationResultTemplate(matches[i]));
  124. }
  125. }
  126. },
  127. error: function(data) {
  128. app.showError("Failed to query services");
  129. }
  130. });
  131. }
  132. }
  133. this.updateEnabledButtons();
  134. },
  135. selectReferencedItem: function(event) {
  136. var target = $(event.target);
  137. if (!target.hasClass('correlationResult')) {
  138. var targetWasSelected = target.hasClass("selectedReference");
  139. $('.serviceEntryReferencedServiceName').removeClass("selectedReference");
  140. if (!targetWasSelected) {
  141. target.addClass("selectedReference");
  142. this.selectedReference = $(".selectedReference").attr('cid');
  143. }
  144. else {
  145. this.selectedReference = null;
  146. }
  147. this.updateEnabledButtons();
  148. }
  149. },
  150. updateEnabledButtons: function() {
  151. var hideAll = true;
  152. if (this.selectedLocal != null) {
  153. var service = this.services.getByCid(this.selectedLocal);
  154. if (service.get('id') != null) {
  155. hideAll = false;
  156. var lifeCycleState = service.get('lifeCycleState');
  157. this.toggle($('#defineReferenceLink'), this.selectedReference != null && this.selectedLocal != null);
  158. this.toggle($('#breakReferenceLink'), this.selectedLocal != null && service.hasReference());
  159. this.toggle($('#approveService'), this.selectedLocal != null && 'project' == lifeCycleState);
  160. this.toggle($('#maskService'), this.selectedLocal != null && 'project' == lifeCycleState);
  161. this.toggle($('#removeService'), this.selectedLocal != null && 'project' == lifeCycleState);
  162. this.toggle($('#resetService'), this.selectedLocal != null && ('approved' == lifeCycleState || 'obsoloete' == lifeCycleState));
  163. }
  164. }
  165. if (hideAll) {
  166. $('.actionButton').hide(100);
  167. }
  168. },
  169. toggle: function(element, value) {
  170. if (value) {
  171. element.show(100);
  172. }
  173. else {
  174. element.hide(100);
  175. }
  176. },
  177. defineReferenceLink: function() {
  178. if (this.selectedReference != null && this.selectedLocal != null) {
  179. var fromId = this.services.getByCid(this.selectedLocal).get('id');
  180. var toId = this.services.getByCid(this.selectedReference).get('id');
  181. var app = this;
  182. $.ajax({
  183. url: '/nuxeo/dashboard/service/' + fromId + '/linkto/' + toId,
  184. type: 'POST',
  185. success: function(data, textStatus, jqXHR) {
  186. location.reload();
  187. },
  188. error: function(data) {
  189. app.showError("Failed to link services");
  190. }
  191. });
  192. }
  193. else {
  194. alert("You must select both a service and a reference to match.");
  195. }
  196. },
  197. breakReferenceLink: function() {
  198. if (this.selectedLocal != null && this.services.getByCid(this.selectedLocal).hasReference()) {
  199. var fromId = this.services.getByCid(this.selectedLocal).get('id');
  200. var app = this;
  201. $.ajax({
  202. url: '/nuxeo/dashboard/service/' + fromId + '/linkto/null',
  203. type: 'POST',
  204. success: function(data, textStatus, jqXHR) {
  205. // XXX: Page is sometimes reloaded before the update is done ; why?
  206. setTimeout(location.reload, 300);
  207. },
  208. error: function(data) {
  209. app.showError("Failed to unlink services");
  210. }
  211. });
  212. }
  213. else {
  214. alert("You must select a service that has a reference.");
  215. }
  216. },
  217. updateLifeCycleState: function(event) {
  218. if (this.selectedLocal != null) {
  219. var buttonId = $(event.target).attr('id');
  220. var serviceId = this.services.getByCid(this.selectedLocal).get('id');
  221. var lifecycleTransition = null;
  222. if (buttonId == 'maskService' || buttonId == 'removeService') {
  223. if (!confirm('Are you sure?')) {
  224. return;
  225. }
  226. }
  227. switch (buttonId) {
  228. case 'approveService': lifecycleTransition = 'approve'; break;
  229. case 'maskService': lifecycleTransition = 'obsolete'; break;
  230. case 'removeService': lifecycleTransition = 'delete'; break;
  231. case 'resetService': lifecycleTransition = 'backToProject'; break;
  232. }
  233. var app = this;
  234. $.ajax({
  235. url: '/nuxeo/dashboard/service/' + serviceId + '/lifecycle/' + lifecycleTransition,
  236. type: 'POST',
  237. success: function(data, textStatus, jqXHR) {
  238. // XXX: Page is sometimes reloaded before the update is done ; why?
  239. setTimeout(function() { window.location.reload(); }, 300);
  240. },
  241. error: function(data) {
  242. app.showError("Failed to update lifecycle state");
  243. }
  244. });
  245. }
  246. },
  247. add: function(serviceEntry) {
  248. var serviceEntryView = new ServiceEntryView(serviceEntry);
  249. this.hidePlaceholder();
  250. this.entriesTableTag.append(serviceEntryView.render().el);
  251. },
  252. addValidatorColumn: function(validatorName) {
  253. this.entriesHeaderTag.append(this.entriesheadervalidatorTemplate(validatorName));
  254. },
  255. hidePlaceholder: function() {
  256. this.placeHolderTag.hide();
  257. },
  258. showError: function(error) {
  259. console.log("Error: " + error);
  260. this.hidePlaceholder();
  261. this.errorHolder.html(this.errorTemplate({error: error}));
  262. var error = $("#error");
  263. error.show('fast');
  264. setTimeout(function() {
  265. error.hide('slow');
  266. }, 3000);
  267. }
  268. });
  269. window.TheApp = new AppView();
  270. });