PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/static/scripts/proposal-edit.js

https://bitbucket.org/efrenfuentes/proposalmatic
JavaScript | 205 lines | 186 code | 14 blank | 5 comment | 1 complexity | c258975e3da3e0efe181902e12ea1446 MD5 | raw file
  1. _.templateSettings = {
  2. interpolate: /\[\[(.+?)\]\]/g
  3. };
  4. $(function(){
  5. var save_proposal_data = function(cb) {
  6. var form = $('form#proposal-data');
  7. $.ajax({
  8. type: 'post',
  9. dataType: 'json',
  10. url: form.attr('action'),
  11. data: JSON.stringify({
  12. id: form.find('#id_id').val(),
  13. title: form.find('#id_title').val(),
  14. client_full: form.find('#id_client_full').val(),
  15. client_short: form.find('#id_client_short').val()
  16. }),
  17. success: function() {
  18. cb && cb();
  19. },
  20. error: function(resp) {
  21. alert("Error saving proposal.");
  22. }
  23. });
  24. };
  25. $('form#proposal-data input').change(function() {
  26. save_proposal_data();
  27. });
  28. $('#save-draft-button').click(function() {
  29. save_proposal_data(function() {
  30. window.location = '/proposals/';
  31. });
  32. return false;
  33. });
  34. $('#publish-button').click(function() {
  35. save_proposal_data(function() {
  36. $('#publish-form').submit();
  37. });
  38. return false;
  39. });
  40. $('.remove-snippet').live('click', function() {
  41. var id = $(this).parents('.proposal-snippet').find('.id').text();
  42. ProposalSnips.remove(id);
  43. $(this).parents('.proposal-snippet').remove();
  44. save_ordering();
  45. return false;
  46. });
  47. // Base ------------------------------------------------------------------------
  48. window.SnippetVersion = Backbone.Model.extend({
  49. defaults: {},
  50. clear: function() {
  51. this.destroy();
  52. },
  53. remove: function() {
  54. }
  55. });
  56. // Library Snippets ------------------------------------------------------------
  57. window.LibrarySnipList = Backbone.Collection.extend({
  58. model: SnippetVersion,
  59. url: '/snippets/a/snippetversion/'
  60. });
  61. window.LibrarySnips = new LibrarySnipList;
  62. window.LibrarySnipView = Backbone.View.extend({
  63. tagName: "article",
  64. template: _.template($('#templates .librarysnip').html()),
  65. initialize: function() {
  66. this.model.bind('change', this.render, this);
  67. this.model.bind('remove', this.remove, this);
  68. // TODO: remove this.
  69. this.model.view = this;
  70. SetupLibrarySnipUI($(this.el));
  71. },
  72. render: function() {
  73. $(this.el).html(this.template(this.model.toJSON()));
  74. return this;
  75. },
  76. remove: function() {
  77. $(this.el).remove();
  78. },
  79. clear: function() {
  80. this.model.clear();
  81. }
  82. });
  83. window.LibraryView = Backbone.View.extend({
  84. el: $('#snippet-library'),
  85. addSnip: function(sv) {
  86. var view = new LibrarySnipView({model: sv});
  87. this.$('#snippet-library-list').append(view.render().el);
  88. },
  89. addAll: function(svs) {
  90. svs.each(this.addSnip);
  91. },
  92. initialize: function() {
  93. LibrarySnips.bind('add', this.addSnip, this);
  94. LibrarySnips.bind('reset', this.addAll, this);
  95. LibrarySnips.bind('all', this.render, this);
  96. },
  97. });
  98. window.Library = new LibraryView;
  99. // Proposal Snippets -----------------------------------------------------------
  100. window.ProposalSnipList = Backbone.Collection.extend({
  101. model: SnippetVersion,
  102. url: '/snippets/a/snippetversion/'
  103. });
  104. window.ProposalSnips = new ProposalSnipList;
  105. window.ProposalSnipView = Backbone.View.extend({
  106. tagName: "article",
  107. template: _.template($('#templates .proposalsnip').html()),
  108. initialize: function() {
  109. this.model.bind('change', this.render, this);
  110. this.model.bind('remove', this.remove, this);
  111. SetupProposalSnipUI($(this.el));
  112. },
  113. render: function() {
  114. $(this.el).html(this.template(this.model.toJSON()));
  115. $(this.el).find('.tooltip').tipsy();
  116. return this;
  117. },
  118. remove: function() {
  119. $(this.el).remove();
  120. $('.tipsy').remove();
  121. },
  122. clear: function() {
  123. this.model.clear();
  124. }
  125. });
  126. window.ProposalView = Backbone.View.extend({
  127. el: $('#proposal'),
  128. addSnip: function(sv) {
  129. var view = new ProposalSnipView({model: sv});
  130. this.$('#proposal-snippets').append(view.render().el);
  131. },
  132. addAll: function(svs) {
  133. svs.each(this.addSnip);
  134. },
  135. initialize: function() {
  136. ProposalSnips.bind('add', this.addSnip, this);
  137. ProposalSnips.bind('reset', this.addAll, this);
  138. ProposalSnips.bind('all', this.render, this);
  139. }
  140. });
  141. window.Proposal = new ProposalView;
  142. // Set up the UI ---------------------------------------------------------------
  143. var save_ordering = function() {
  144. var ids = $('#proposal-snippets .id').map(function(i, e) { return $(e).text(); }).get();
  145. var current_proposal = $('#proposal-id').text();
  146. $.ajax({
  147. type: 'post',
  148. dataType: 'json',
  149. url: '/proposals/a/' + current_proposal + '/reorder/',
  150. data: JSON.stringify({
  151. snippet_versions: ids.join(',')
  152. }),
  153. success: function(resp) {
  154. $('#proposal-snippets .library-snippet').remove();
  155. $('#proposal-snippets .proposal-snippet').remove();
  156. $('#snippet-library-list .library-snippet').remove();
  157. LibrarySnips.remove(LibrarySnips.models);
  158. LibrarySnips.reset(resp.lib_sv_json);
  159. ProposalSnips.remove(ProposalSnips.models);
  160. ProposalSnips.reset(resp.prop_sv_json);
  161. },
  162. error: function(resp) {
  163. alert("Error reordering snippets.");
  164. }
  165. });
  166. }
  167. $('#proposal-snippets').sortable({
  168. 'tolerance': 'intersect',
  169. update: function(event, ui) {
  170. save_ordering();
  171. },
  172. });
  173. window.SetupLibrarySnipUI = function(el) {
  174. el.draggable({
  175. connectToSortable: "#proposal-snippets",
  176. helper: "clone",
  177. revert: "invalid",
  178. stop: function(event, ui) {
  179. var id = $(event.target).find('.id').text();
  180. LibrarySnips.remove(id);
  181. save_ordering();
  182. }
  183. });
  184. };
  185. window.SetupProposalSnipUI = function(el) {
  186. };
  187. $('form a.custom-button').click(function() {
  188. $(this).parents('form').submit();
  189. return false;
  190. });
  191. });