/public/js/script.js

https://github.com/Swizec/notmakingitbetter.com · JavaScript · 213 lines · 169 code · 44 blank · 0 comment · 17 complexity · 2b2c05d526b07235ff67978c73b14751 MD5 · raw file

  1. (function($){
  2. var Card = window.Card = Backbone.Model.extend({
  3. url: function () {
  4. return (!this.id) ? '/card' : '/card/'+this.id;
  5. },
  6. initialize: function() {
  7. var _this = this;
  8. this.attributes.changed = false;
  9. this.bind("change", function () {
  10. _this.attributes.changed = true;
  11. });
  12. }
  13. });
  14. var CardFormView = window.CardFormView = Backbone.View.extend({
  15. el: 'form',
  16. template: new EJS({url: '/client-views/form.ejs'}),
  17. step: 'image',
  18. events: {
  19. "change input": "update_data",
  20. "change textarea": "update_data",
  21. "submit": "update_data",
  22. "click a.flip": "flip",
  23. "click .magic-button": "send",
  24. "focus .image_form input[type=text]": "focus",
  25. "blur .image_form input[type=text]": "blur",
  26. "keyup textarea": "limit",
  27. "paste textarea": "limit",
  28. "blur textarea": "limit",
  29. "focus textarea#text": "change_step",
  30. "focus textarea#address": "change_step"
  31. },
  32. initialize: function () {
  33. _.bindAll(this, "render", "update_data", "flip", "send", "focus", "blur", "limit", "do_send", "change_step");
  34. this.model.bind("change", this.render);
  35. this.model.view = this;
  36. this.change_step();
  37. },
  38. render: function () {
  39. this.$(this.el).html(this.template.render(this.model.toJSON()));
  40. this.delegateEvents();
  41. return this.el;
  42. },
  43. update_data: function () {
  44. var image = this.$("#image").val();
  45. image = (image != '') ? image : this.model.get('image');
  46. if (image != this.model.get('image')) {
  47. mpq.track("Changed image");
  48. }else{
  49. mpq.track("Updated card");
  50. }
  51. var silent = (image == this.model.get('image'));
  52. this.model.set({image: image,
  53. text: this.$("#text").val(),
  54. address: this.$("#address").val()
  55. },
  56. {silent: silent});
  57. },
  58. flip: function (event) {
  59. event.preventDefault();
  60. var $el = this.$(this.el);
  61. $el.toggleClass('flip');
  62. this.step = "text";
  63. this.change_step();
  64. mpq.track("Flipped");
  65. },
  66. send: function (event) {
  67. event.preventDefault();
  68. this.model.save({},
  69. {error: function () {
  70. alert("Sorry, there was an error saving the postcard :(");
  71. },
  72. success: this.do_send});
  73. },
  74. do_send: function () {
  75. $(".notify_url").val("http://notmakingitbetter.com/ipn/"+this.model.id);
  76. $(".item_num").val(this.model.id);
  77. $(".return").val('http://notmakingitbetter.com/sent/'+this.model.id);
  78. mpq.track("Buy", {}, function () {
  79. $("#magic-button-form").submit();
  80. });
  81. },
  82. focus: function () {
  83. this.$(".image_form").addClass('focus');
  84. this.step = "image";
  85. },
  86. blur: function () {
  87. this.$(".image_form").removeClass('focus');
  88. },
  89. limit: function (event) {
  90. var $area = $(event.currentTarget);
  91. var lines = $area.val().split("\n");
  92. if ($area.attr("id") == "text" && lines.length > 14) {
  93. lines.pop();
  94. $area.val(lines.join("\n"));
  95. }else if ($area.attr("id") == "address" && lines.length > 5) {
  96. lines.pop();
  97. $area.val(lines.join("\n"));
  98. }
  99. if ($area.attr("id") == "address" && lines.length > 2) {
  100. this.step = "send";
  101. this.change_step();
  102. }
  103. },
  104. change_step: function (event) {
  105. $("section#card li").removeClass("step");
  106. if (event) {
  107. this.step = $(event.currentTarget).attr("id");
  108. }
  109. $("section#card li[step="+this.step+"]").addClass("step");
  110. }
  111. });
  112. var PostcardList = window.PostcardList = Backbone.Collection.extend({
  113. model: Card,
  114. url: '/recents'
  115. });
  116. var RecentView = window.RecentView = Backbone.View.extend({
  117. tagName: "li",
  118. template: new EJS({url: '/client-views/recent.ejs'}),
  119. initialize: function () {
  120. _.bindAll(this, "render");
  121. this.model.bind("change", this.render);
  122. this.model.view = this;
  123. },
  124. render: function () {
  125. this.$(this.el).html(this.template.render(this.model.toJSON()));
  126. return this.el;
  127. }
  128. });
  129. var Recents = window.Recents = new PostcardList;
  130. var AppView = window.AppView = Backbone.View.extend({
  131. el: $("#main"),
  132. events: {
  133. },
  134. initialize: function () {
  135. _.bindAll(this, "reset_recent");
  136. Recents.bind("reset", this.reset_recent);
  137. Recents.reset(window.recent_cards);
  138. var card;
  139. if (typeof(sent_card) != "object") {
  140. var _card = new Card(main_card);
  141. card = new Card({
  142. image: (_card) ? _card.get('image') : 'http://i.imgur.com/24uzu.jpg',
  143. text: "",
  144. address: ""
  145. });
  146. }else{
  147. card = new Card({
  148. image: sent_card.image,
  149. text: sent_card.text,
  150. address: ""
  151. });
  152. }
  153. var card_form = new CardFormView({model: card});
  154. card_form.render();
  155. },
  156. reset_recent: function (cards) {
  157. var $ol = this.$("#recent ol:last");
  158. _.map(cards.models, function (card) {
  159. var recent = new RecentView({model: card});
  160. $ol.append(recent.render());
  161. if ($(window).width()-$ol.width() < $ol.children("li:first").width()*($ol.children("li").size()-2)) {
  162. $ol = $("<ol></ol>");
  163. this.$("#recent").append($ol);
  164. }
  165. });
  166. }
  167. });
  168. var App = window.App = new AppView;
  169. })(jQuery);