PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/budjet/app.js

https://bitbucket.org/doubleface/doubleface.bitbucket.org
JavaScript | 197 lines | 187 code | 5 blank | 5 comment | 10 complexity | a56c7f31fd416547851655c9eca42e61 MD5 | raw file
  1. (function(){
  2. $(document).ready(function(){
  3. app.start();
  4. });
  5. var app = window.app = new Backbone.Marionette.Application();
  6. app.helpers = {
  7. stringToDate:function(datestring){
  8. datestring = datestring.split("/");
  9. return new Date(datestring[2], parseInt(datestring[1], 10)-1, datestring[0]);
  10. }
  11. ,dateToString: function(date){
  12. var pad0 = function(str){
  13. str = ""+str;
  14. var len = 2;
  15. return new Array(len + 1 - str.length).join(0) + str;
  16. };
  17. return pad0(date.getDate()) + "/" + pad0(date.getMonth()+1) + "/" + date.getFullYear();
  18. }
  19. };
  20. app.addInitializer(function(){
  21. this.main.show(new OperationView());
  22. new TotalView();
  23. this.operationeditview = new OperationEditView();
  24. this.router = new Router();
  25. Backbone.history.start();
  26. });
  27. app.addRegions({
  28. main : "#main"
  29. });
  30. var Router = Backbone.Router.extend({
  31. routes: {
  32. "": "list"
  33. ,"operations/list": "list"
  34. ,"operations/create": "create"
  35. ,"operations/edit/:id": "edit"
  36. }
  37. ,list: function(){
  38. app.operationeditview.$el.modal("hide");
  39. }
  40. ,create: function(){
  41. app.operationeditview.setParams(new OperationModel(), "create").$el.modal("show");
  42. }
  43. ,edit: function(id){
  44. app.operationeditview.setParams(window.operations.get(id), "edit").$el.modal("show");
  45. }
  46. });
  47. // MODELS & COLLECTIONS
  48. var OperationModel = Backbone.Model.extend({
  49. defaults: function(){
  50. // date normalization : hour is not important here
  51. // + the defaults are calculated for each new model
  52. var date = new Date();
  53. date = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  54. return {
  55. date: date.getTime()
  56. ,desc: ""
  57. ,tags: ""
  58. ,value: ""
  59. ,pointed: false
  60. };
  61. }
  62. });
  63. var OperationCollection = Backbone.Collection.extend({
  64. localStorage: new Backbone.LocalStorage("operations")
  65. ,initialize: function(){
  66. this.on("change:date", function(){
  67. this.sort();
  68. });
  69. }
  70. ,model: OperationModel
  71. ,comparator: function(m1, m2){
  72. var v1 = m1.get("date");
  73. var v2 = m2.get("date");
  74. if (v1 === v2) return 0;
  75. else return v1 < v2 ? 1 : -1;
  76. }
  77. ,getTotal: function(status){
  78. var total = 0;
  79. this.each(function(model){
  80. if (status === "all" || (model.get("pointed") === true && status === "pointed") || (model.get("pointed") === false && status === "not_pointed")){
  81. total+= model.get("value");
  82. }
  83. });
  84. return total;
  85. }
  86. });
  87. // VIEWS
  88. var RowView = Backbone.Marionette.ItemView.extend({
  89. modelEvents: {
  90. "change": "render"
  91. }
  92. ,tagName: "li"
  93. ,events: {
  94. "click .clickpointage": function(){
  95. var pointed = this.model.get("pointed");
  96. this.model.set("pointed", !pointed).save();
  97. }
  98. ,"click": function(e){
  99. if (!$(e.target).closest("span").hasClass("clickpointage")) {
  100. app.router.navigate("/operations/edit/" + this.model.cid, {trigger: true});
  101. }
  102. }
  103. }
  104. ,template: "#operation_row_template"
  105. ,templateHelpers: {
  106. getDateString : function(){
  107. return app.helpers.dateToString(new Date(this.date));
  108. }
  109. }
  110. });
  111. var OperationView = Backbone.Marionette.CollectionView.extend({
  112. initialize: function(){
  113. var self = this;
  114. window.operations = self.collection;
  115. self.collection.fetch().done(function(){
  116. self.render();
  117. });
  118. }
  119. ,id : "#operationlist"
  120. ,tagName : "ul"
  121. ,className : "unstyled"
  122. ,collection: new OperationCollection()
  123. ,collectionEvents: {
  124. "sort": "render"
  125. }
  126. ,template: "#operation_template"
  127. ,itemView: RowView
  128. ,appendHtml: function(collectionView, itemView, index){
  129. // sorted collection handling
  130. var childrenContainer = collectionView.$el;
  131. var children = childrenContainer.children();
  132. if (children.size() <= index) {
  133. childrenContainer.append(itemView.el);
  134. } else {
  135. childrenContainer.children().eq(index).before(itemView.el);
  136. }
  137. }
  138. });
  139. var OperationEditView = Backbone.Marionette.ItemView.extend({
  140. id : "#operationedit"
  141. ,className: "modal"
  142. ,events: {
  143. "click #submit" : function(){
  144. var date = this.$el.find("#date").val();
  145. date = app.helpers.stringToDate(date);
  146. var timestamp = date.getTime();
  147. this.model.set({
  148. date: timestamp
  149. ,desc: this.$el.find("#desc").val()
  150. ,tags: this.$el.find("#tags").val()
  151. ,value: parseFloat(this.$el.find("#value").val())
  152. });
  153. window.operations.add(this.model, {merge: true});
  154. this.model.save();
  155. }
  156. ,"click #delete" : function(){
  157. this.model.destroy();
  158. }
  159. }
  160. ,template: "#operation_edit_template"
  161. ,setParams: function(model, mode){
  162. this.model = model;
  163. this.mode = mode;
  164. this.render();
  165. return this;
  166. }
  167. ,serializeData : function(){
  168. var json = this.model.toJSON();
  169. var timestamp = parseInt(this.model.get("date"), 10);
  170. json.datestring = app.helpers.dateToString(new Date(timestamp));
  171. json.mode = this.mode;
  172. return json;
  173. }
  174. });
  175. var TotalView = Backbone.View.extend({
  176. events: {
  177. "change #total_selection" : "render"
  178. }
  179. ,el: function(){
  180. return $("#total");
  181. }
  182. ,initialize: function(){
  183. this.collection = window.operations;
  184. this.listenTo(this.collection, "reset add remove change", this.render, this);
  185. this.render();
  186. }
  187. ,render: function(){
  188. this.status = this.$el.find("#total_selection").val();
  189. this.$el.find("#total_text").text(this.collection.getTotal(this.status).toFixed(2));
  190. }
  191. });
  192. })();