/wp-content/plugins/plethora-featureslib/features/widget/timetable/js/widget-timetable.js

https://bitbucket.org/kahara/glissanmedical · JavaScript · 190 lines · 150 code · 37 blank · 3 comment · 6 complexity · 50eb6c57a5318fd0df9d5266d7c1e694 MD5 · raw file

  1. // DRAG AND DROP SORTABLE WORKTABLE LIST //
  2. jQuery(function($) {
  3. window.WidgetTimetable = {
  4. rowDataID: {}
  5. };
  6. WidgetTimetable.updateRowData = function(rowData){
  7. var twID = rowData.twID;
  8. rowData = encodeURIComponent(JSON.stringify(rowData));
  9. WidgetTimetable.rowDataID[twID].val(rowData);
  10. };
  11. WidgetTimetable.EventBus = _({}).extend(Backbone.Events);
  12. WidgetTimetable.Item = Backbone.Model.extend({
  13. defaults: { day: "Day", time: "00:00" }
  14. });
  15. WidgetTimetable.ItemView = Backbone.View.extend({
  16. tagName: 'li',
  17. className: 'item-view',
  18. template: (function(str) {
  19. var orig_settings, t;
  20. orig_settings = _.templateSettings;
  21. _.templateSettings = {
  22. interpolate: /\{\{(.+?)\}\}/g
  23. };
  24. t = _.template($("#widgetTimetableTemplate").html());
  25. _.templateSettings = orig_settings;
  26. return t;
  27. })(),
  28. events: {
  29. 'drop' : 'drop',
  30. 'keyup input' : 'updateModel',
  31. 'click button' : 'removeModel'
  32. },
  33. removeModel: function(e) {
  34. return WidgetTimetable.EventBus.trigger('removeModel', this.model);
  35. },
  36. updateModel: function(e) {
  37. this.model.set(e.target.name, e.target.value);
  38. return WidgetTimetable.EventBus.trigger('updateModel');
  39. },
  40. drop: function(event, index) {
  41. this.$el.trigger('update-sort', [this.model, index]);
  42. },
  43. render: function() {
  44. $(this.el).html(this.template(this.model.attributes));
  45. this.$el.css({ 'cursor' : 'move', 'border' : '1px solid #ccc', 'padding' : '10px', 'margin' : '1px auto 1px auto' });
  46. return this;
  47. }
  48. });
  49. WidgetTimetable.Items = Backbone.Collection.extend({
  50. model: WidgetTimetable.Item,
  51. comparator: function(model) {
  52. return model.get('ordinal');
  53. },
  54. initialize: function( models, options ) {
  55. this.listenTo(WidgetTimetable.EventBus, 'removeModel', this.onModelRemoved);
  56. this.listenTo(WidgetTimetable.EventBus, 'updateModel', this.onModelUpdated);
  57. options || ( options = {} )
  58. if ( options.twID ) this.twID = options.twID;
  59. },
  60. onModelRemoved: function(model, collection, options) {
  61. this.remove(model);
  62. },
  63. onModelUpdated: function() {
  64. WidgetTimetable.updateRowData(this);
  65. }
  66. });
  67. WidgetTimetable.ItemsView = Backbone.View.extend({
  68. events: {
  69. 'update-sort': 'updateSort'
  70. },
  71. initialize: function() {
  72. this.collection.bind('add', this.addRow, this);
  73. return this.listenTo(WidgetTimetable.EventBus, 'removeModel', this.removeModel);
  74. },
  75. removeModel: function() {
  76. WidgetTimetable.updateRowData(this.collection);
  77. return this.render();
  78. },
  79. addRow: function(model) {
  80. return this.render();
  81. },
  82. render: function() {
  83. this.$el.children().remove();
  84. this.collection.each(this.appendModelView, this);
  85. return this;
  86. },
  87. appendModelView: function(model) {
  88. var el;
  89. el = new WidgetTimetable.ItemView({
  90. model: model
  91. }).render().el;
  92. this.$el.append(el);
  93. },
  94. updateSort: function(event, model, position) {
  95. this.collection.remove(model);
  96. this.collection.each(function(model, index) {
  97. var ordinal;
  98. ordinal = index;
  99. if (index >= position) {
  100. ordinal += 1;
  101. }
  102. model.set('ordinal', ordinal);
  103. });
  104. model.set('ordinal', position);
  105. this.collection.add(model, {
  106. at: position
  107. });
  108. this.render();
  109. }
  110. });
  111. WidgetTimetable.Init = function( options ){
  112. // GET ALL TIMETABLE SORTABLE SECTIONS
  113. var timetableWidgets = $("#widgets-right")
  114. .find("input[name^='widget-plethora-timetable']")
  115. .filter("input[name$='[rowData]']");
  116. if ( timetableWidgets.length < 1 ) return;
  117. timetableWidgets.each(function(index, tw){
  118. var $tw = $(tw);
  119. var $_id = $tw.attr("id").match(/\d+/);
  120. if ( typeof $_id === "null" ) return;
  121. $_id = $_id[0];
  122. // WidgetTimetable.rowDataID = $tw; // DEPRECATED TO FIX MULTIPLE WIDGETS BUG
  123. WidgetTimetable.rowDataID[$_id] = $tw;
  124. var $widgetInput = $("[id$=plethora-timetable-widget-" + $_id + "]");
  125. var $widgetControls = $widgetInput.find('.widgetTimetableControls');
  126. var Instance = {};
  127. Instance.twID = $_id;
  128. Instance.collection = new WidgetTimetable.Items([], { twID: $_id });
  129. Instance.collectionView = new WidgetTimetable.ItemsView({
  130. el: $widgetControls,
  131. collection: Instance.collection
  132. });
  133. var widgetRowData = $tw.val();
  134. if ( widgetRowData ){
  135. widgetRowData = JSON.parse( decodeURIComponent( widgetRowData ) );
  136. Instance.collection.add( widgetRowData );
  137. }
  138. Instance.collectionView.render();
  139. $widgetControls.sortable({
  140. stop: function(event, ui) {
  141. ui.item.trigger('drop', ui.item.index());
  142. WidgetTimetable.updateRowData(Instance.collection);
  143. }
  144. });
  145. $widgetInput.find('.widgetTimetableAddRow').on('click', function(){
  146. Instance.collection.add( new WidgetTimetable.Item({ ordinal: Instance.collection.length }) );
  147. WidgetTimetable.updateRowData(Instance.collection);
  148. });
  149. });
  150. };
  151. WidgetTimetable.Init();
  152. $(document).on('widget-updated widget-added', function(e){ WidgetTimetable.Init(); });
  153. });