PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-components/jira-plugins/jira-project-config-plugin/src/main/resources/workflows/js/tab/model/IgniteWorkflows.js

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
JavaScript | 76 lines | 57 code | 11 blank | 8 comment | 9 complexity | e1dd1b019e920857dd9ba121822998cd MD5 | raw file
Possible License(s): Apache-2.0
  1. define("jira-project-config/workflows/tab/model/ignite-workflows", ["require"], function(require){
  2. "use strict";
  3. var _ = require('underscore');
  4. var Backbone = require('jira-project-config/workflows/tab/lib/backbone');
  5. var IgniteWorkflowModel = require('jira-project-config/workflows/tab/model/ignite-workflow');
  6. var localeComparator = require('jira-project-config/workflows/tab/utils/locale-comparator');
  7. return Backbone.Collection.extend({
  8. model: IgniteWorkflowModel,
  9. update: function (newCollection) {
  10. var oldDefaultWorkflow = this.find(function (model) {
  11. return model.get('default');
  12. });
  13. var oldDefaultName;
  14. if (oldDefaultWorkflow) {
  15. oldDefaultName = oldDefaultWorkflow.get('name');
  16. }
  17. //remove from our collection all the workflows that are not present in the new collection
  18. this.remove(_.chain(this.pluck('name')).difference(newCollection.pluck('name')).value());
  19. //move from the new collection to our collection all the workflows that are not present in our collection
  20. var newModelNames = _.chain(newCollection.pluck('name')).difference(this.pluck('name'));
  21. var newModels = newCollection.filter(function (model) {
  22. return newModelNames.include(model.get('name')).value();
  23. });
  24. newCollection.remove(newModels);
  25. //set the default flag to false for all models in the current collection. this is so that if there is a new
  26. //default in the new collection, the final collection will be sorted correctly. if not, this flag will get
  27. //reset correctly in the update part below.
  28. this.each(function (model) {
  29. model.set('default', false);
  30. });
  31. //update the workflows in our collection with data from the new collection
  32. newCollection.each(function (workflowModel) {
  33. var newName = workflowModel.get('name');
  34. var current = this.get(newName);
  35. current.set(workflowModel.toJSON());
  36. }, this);
  37. this.add(newModels);
  38. if (oldDefaultName) {
  39. // If the previously default workflow is still present in the collection, then check if it is still the default.
  40. // If it's not then reorder the workflow views accordingly.
  41. var oldDefaultIsStillPresent = this.any(function (model) {
  42. return model.get('name') === oldDefaultName;
  43. });
  44. if (oldDefaultIsStillPresent) {
  45. var newDefaultWorkflow = this.find(function (model) {
  46. return model.get('default');
  47. });
  48. if (newDefaultWorkflow && oldDefaultName !== newDefaultWorkflow.get('name')) {
  49. this.sort();
  50. this.trigger('reorder');
  51. }
  52. }
  53. }
  54. },
  55. comparator: function (a, b) {
  56. if (a.get('default')) {
  57. return -1;
  58. } else if (b.get('default')) {
  59. return 1;
  60. } else {
  61. return localeComparator('name')(a, b);
  62. }
  63. }
  64. });
  65. });