PageRenderTime 65ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/generalwill/static/js/model.js

https://bitbucket.org/deximer/generalwill
JavaScript | 245 lines | 217 code | 28 blank | 0 comment | 7 complexity | 519ad2af82b2d2fb32e0a2fad5fc948b MD5 | raw file
Possible License(s): LGPL-2.1, MIT
  1. window.Priority = Backbone.Model.extend({});
  2. window.PriorityView = Backbone.View.extend({
  3. tagName : 'li',
  4. className: 'ui-state-default',
  5. initialize: function() {
  6. _.bindAll(this, 'render');
  7. this.model.bind('change', this.render);
  8. this.template= _.template($('#priority-list-item-template').html());
  9. },
  10. render: function() {
  11. var renderedContent = this.template(this.model.toJSON());
  12. $(this.el).html(renderedContent);
  13. return this;
  14. }
  15. });
  16. window.UserPriorityView = Backbone.View.extend({
  17. tagName : 'li',
  18. className: 'ui-state-default',
  19. initialize: function() {
  20. _.bindAll(this, 'render');
  21. this.model.bind('change', this.render);
  22. this.template= _.template($(
  23. '#user-priority-list-item-template').html());
  24. },
  25. render: function() {
  26. var renderedContent = this.template(this.model.toJSON());
  27. $(this.el).attr('id', 'priority_' + this.model.get('uuid'));
  28. $(this.el).html(renderedContent);
  29. return this;
  30. }
  31. });
  32. window.TimeBoxPriorityView = Backbone.View.extend({
  33. tagName : 'li',
  34. className: 'ui-state-default',
  35. initialize: function() {
  36. _.bindAll(this, 'render');
  37. this.model.bind('change', this.render);
  38. this.template= _.template($(
  39. '#timebox-priority-list-item-template').html());
  40. },
  41. render: function() {
  42. var renderedContent = this.template(this.model.toJSON());
  43. $(this.el).html(renderedContent);
  44. return this;
  45. }
  46. });
  47. window.GroupPriorities = Backbone.Collection.extend({
  48. model : window.Priority,
  49. url : function() {
  50. return document.location + '/group_priorities';
  51. }
  52. });
  53. window.GroupPrioritiesView = Backbone.View.extend({
  54. initialize: function() {
  55. _.bindAll(this, 'render');
  56. this.template = _.template($('#group-priorities-template').html());
  57. this.collection.bind('reset', this.render);
  58. },
  59. events: {
  60. 'click .timebox' : 'timebox'
  61. },
  62. timebox : function(e) {
  63. timebox($(e.currentTarget).attr('id'));
  64. },
  65. render: function() {
  66. var $priorities,
  67. collection = this.collection;
  68. $(this.el).html(this.template({}));
  69. $priorities = $('#group-priorities');
  70. effort = 0;
  71. collection.each(function(priority) {
  72. if(! priority.get('archived')) {
  73. effort += priority.get('effort');
  74. var view = new PriorityView({
  75. model : priority,
  76. collection: collection
  77. });
  78. if(effort > priority.get('velocity')) {
  79. view.el.className = 'out-of-scope ui-state-default';
  80. } else {
  81. view.el.className = 'ui-state-hover';
  82. }
  83. $priorities.append(view.render().el);
  84. }
  85. });
  86. return this;
  87. }
  88. });
  89. window.TimeBoxPrioritiesView = Backbone.View.extend({
  90. initialize: function() {
  91. _.bindAll(this, 'render');
  92. this.template =_.template($('#timebox-priorities-template').html());
  93. this.collection.bind('reset', this.render);
  94. },
  95. events: {
  96. 'click .complete' : 'complete'
  97. },
  98. complete : function(e) {
  99. complete($(e.currentTarget).attr('id'));
  100. },
  101. render: function() {
  102. var $priorities,
  103. collection = this.collection;
  104. $(this.el).html(this.template({}));
  105. $priorities = $('#timebox-priorities');
  106. collection.each(function(priority) {
  107. if(priority.get('timeboxed')) {
  108. var view = new TimeBoxPriorityView({
  109. model : priority,
  110. collection: collection
  111. });
  112. $priorities.append(view.render().el);
  113. }
  114. });
  115. return this;
  116. }
  117. });
  118. window.UserPrioritiesView = Backbone.View.extend({
  119. initialize: function() {
  120. _.bindAll(this, 'render');
  121. this.template = _.template($('#user-priorities-template').html());
  122. this.collection.bind('reset', this.render);
  123. },
  124. events: {
  125. 'click .effort_menu .data-box' : 'set_effort',
  126. 'click .user-timebox' : 'timebox'
  127. },
  128. timebox : function(e) {
  129. vote_to_timebox($(e.currentTarget).attr('id'));
  130. },
  131. set_effort: function(e, ui) {
  132. effort = $(e.currentTarget).html();
  133. wrapper = acquire_wrapper(e.currentTarget, 'priority');
  134. uuid = $(wrapper).attr('id');
  135. set_effort(uuid, effort);
  136. },
  137. render: function() {
  138. var $priorities,
  139. collection = this.collection;
  140. $(this.el).html(this.template({}));
  141. $priorities = $('#user-priorities');
  142. user_order = new Array();
  143. unordered = new Array();
  144. collection.each(function(p) {
  145. if(p.get('user_order') == 0) {
  146. unordered.push(p);
  147. } else {
  148. user_order.push(p);
  149. }
  150. });
  151. user_order = _.sortBy(user_order,
  152. function(p) { return p.get('user_order');});
  153. $.each(user_order, function() {
  154. var view = new UserPriorityView({
  155. model : this,
  156. collection: collection
  157. });
  158. $priorities.append(view.render().el);
  159. });
  160. $.each(unordered, function() {
  161. var view = new UserPriorityView({
  162. model : this,
  163. collection: collection
  164. });
  165. $priorities.append(view.render().el);
  166. });
  167. $sortable = $('.sortable');
  168. $sortable.sortable({
  169. update:function(event, ui) {
  170. var ids = $sortable.sortable('serialize');
  171. var action = "action=set_priority_order&" + ids
  172. $.ajax({
  173. type: "POST",
  174. url: document.location,
  175. data: action
  176. }).done(function(html) {
  177. window.group_priorities.fetch();
  178. });
  179. }
  180. });
  181. $sortable.disableSelection();
  182. return this;
  183. }
  184. });
  185. window.group_priorities = new GroupPriorities();
  186. window.GroupRouter = Backbone.Router.extend({
  187. routes: {
  188. '': 'home'
  189. },
  190. initialize: function() {
  191. this.groupPrioritiesView = new GroupPrioritiesView({
  192. collection: window.group_priorities
  193. });
  194. this.userPrioritiesView = new UserPrioritiesView({
  195. collection: window.group_priorities
  196. });
  197. this.timeBoxPrioritiesView = new TimeBoxPrioritiesView({
  198. collection: window.group_priorities
  199. });
  200. },
  201. home: function() {
  202. var $container = $('#priority-list');
  203. $container.empty();
  204. $container.append(this.groupPrioritiesView.render().el);
  205. $container = $('#user-priority-list');
  206. $container.empty();
  207. $container.append(this.userPrioritiesView.render().el);
  208. $container = $('#timebox-priority-list');
  209. $container.empty();
  210. $container.append(this.timeBoxPrioritiesView.render().el);
  211. }
  212. });
  213. $(function() {
  214. window.group_priorities.fetch();
  215. window.router = new GroupRouter();
  216. Backbone.history.start();
  217. });