dashboard.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // jQuery UI style "widget" for editing an iTop "dashboard"
  2. $(function()
  3. {
  4. // the widget definition, where "itop" is the namespace,
  5. // "dashboard" the widget name
  6. $.widget( "itop.dashboard",
  7. {
  8. // default options
  9. options:
  10. {
  11. dashboard_id: '',
  12. layout_class: '',
  13. title: '',
  14. submit_to: 'index.php',
  15. submit_parameters: {},
  16. render_to: 'index.php',
  17. render_parameters: {},
  18. new_dashlet_parameters: {}
  19. },
  20. // the constructor
  21. _create: function()
  22. {
  23. var me = this;
  24. this.element
  25. .addClass('itop-dashboard')
  26. .bind('mark_as_modified.itop-dashboard', function(){me.mark_as_modified();} );
  27. this.ajax_div = $('<div></div>').appendTo(this.element);
  28. this._make_draggable();
  29. this.bModified = false;
  30. },
  31. // called when created, and later when changing options
  32. _refresh: function()
  33. {
  34. var oParams = this._get_state(this.options.render_parameters);
  35. var me = this;
  36. $.post(this.options.render_to, oParams, function(data){
  37. me.element.html(data);
  38. me._make_draggable();
  39. });
  40. },
  41. // events bound via _bind are removed automatically
  42. // revert other modifications here
  43. destroy: function()
  44. {
  45. this.element
  46. .removeClass('itop-dashboard');
  47. this.ajax_div.remove();
  48. $(document).unbind('keyup.dashboard_editor');
  49. // call the original destroy method since we overwrote it
  50. $.Widget.prototype.destroy.call( this );
  51. },
  52. // _setOptions is called with a hash of all options that are changing
  53. _setOptions: function()
  54. {
  55. // in 1.9 would use _superApply
  56. $.Widget.prototype._setOptions.apply( this, arguments );
  57. this._refresh();
  58. },
  59. // _setOption is called for each individual option that is changing
  60. _setOption: function( key, value )
  61. {
  62. // in 1.9 would use _super
  63. $.Widget.prototype._setOption.call( this, key, value );
  64. },
  65. _get_state: function(oMergeInto)
  66. {
  67. var oState = oMergeInto;
  68. oState.cells = [];
  69. this.element.find('.layout_cell').each(function() {
  70. var aList = [];
  71. $(this).find(':itop-dashlet').each(function() {
  72. var oDashlet = $(this).data('dashlet');
  73. if(oDashlet)
  74. {
  75. var oDashletParams = oDashlet.get_params();
  76. var sId = oDashletParams.dashlet_id;
  77. oState[sId] = oDashletParams;
  78. aList.push({dashlet_id: sId, dashlet_class: oDashletParams.dashlet_class} );
  79. }
  80. });
  81. if (aList.length == 0)
  82. {
  83. oState[0] = {dashlet_id: 0, dashlet_class: 'DashletEmptyCell'};
  84. aList.push({dashlet_id: 0, dashlet_class: 'DashletEmptyCell'});
  85. }
  86. oState.cells.push(aList);
  87. });
  88. oState.dashboard_id = this.options.dashboard_id;
  89. oState.layout_class = this.options.layout_class;
  90. oState.title = this.options.title;
  91. return oState;
  92. },
  93. // Modified means: at least one change has been applied
  94. mark_as_modified: function()
  95. {
  96. this.bModified = true;
  97. },
  98. is_modified: function()
  99. {
  100. return this.bModified;
  101. },
  102. // Dirty means: at least one change has not been committed yet
  103. is_dirty: function()
  104. {
  105. if ($('#dashboard_editor .ui-layout-east .itop-property-field-modified').size() > 0)
  106. {
  107. return true;
  108. }
  109. else
  110. {
  111. return false;
  112. }
  113. },
  114. // Force the changes of all the properties being "dirty"
  115. apply_changes: function()
  116. {
  117. $('#dashboard_editor .ui-layout-east .itop-property-field-modified').trigger('apply_changes');
  118. },
  119. save: function()
  120. {
  121. var oParams = this._get_state(this.options.submit_parameters);
  122. var me = this;
  123. $.post(this.options.submit_to, oParams, function(data){
  124. me.ajax_div.html(data);
  125. });
  126. },
  127. add_dashlet: function(options)
  128. {
  129. var sDashletId = this._get_new_id();
  130. var oDashlet = $('<div class="dashlet" id="dashlet_'+sDashletId+'"/>');
  131. oDashlet.appendTo(options.container);
  132. var oDashletProperties = $('<div class="dashlet_properties" id="dashlet_properties_'+sDashletId+'"/>');
  133. oDashletProperties.appendTo($('#dashlet_properties'));
  134. var oParams = this.options.new_dashlet_parameters;
  135. var sDashletClass = options.dashlet_class;
  136. oParams.dashlet_class = sDashletClass;
  137. oParams.dashlet_id = sDashletId;
  138. var me = this;
  139. $.post(this.options.render_to, oParams, function(data){
  140. me.ajax_div.html(data);
  141. $('#dashlet_'+sDashletId)
  142. .dashlet({dashlet_id: sDashletId, dashlet_class: sDashletClass})
  143. .dashlet('deselect_all')
  144. .dashlet('select')
  145. .draggable({
  146. revert: 'invalid', appendTo: 'body', zIndex: 9999,
  147. helper: function() {
  148. var oDragItem = $(this).dashlet('get_drag_icon');
  149. return oDragItem;
  150. },
  151. cursorAt: { top: 16, left: 16 },
  152. });
  153. if (options.refresh)
  154. {
  155. me._refresh();
  156. }
  157. });
  158. },
  159. _get_new_id: function()
  160. {
  161. var iMaxId = 0;
  162. this.element.find(':itop-dashlet').each(function() {
  163. var oDashlet = $(this).data('dashlet');
  164. if(oDashlet)
  165. {
  166. var oDashletParams = oDashlet.get_params();
  167. var id = parseInt(oDashletParams.dashlet_id, 10);
  168. if (id > iMaxId) iMaxId = id;
  169. }
  170. });
  171. return 1 + iMaxId;
  172. },
  173. _make_draggable: function()
  174. {
  175. var me = this;
  176. this.element.find('.dashlet').draggable({
  177. revert: 'invalid', appendTo: 'body', zIndex: 9999,
  178. helper: function() {
  179. var oDragItem = $(this).dashlet('get_drag_icon');
  180. return oDragItem;
  181. },
  182. cursorAt: { top: 16, left: 16 },
  183. });
  184. this.element.find('table td').droppable({
  185. accept: '.dashlet,.dashlet_icon',
  186. drop: function(event, ui) {
  187. $( this ).find( ".placeholder" ).remove();
  188. var bRefresh = $(this).hasClass('layout_extension');
  189. var oDashlet = ui.draggable;
  190. if (oDashlet.hasClass('dashlet'))
  191. {
  192. // moving around a dashlet
  193. oDashlet.detach();
  194. oDashlet.css({top: 0, left: 0});
  195. oDashlet.appendTo($(this));
  196. if( bRefresh )
  197. {
  198. // The layout was extended... refresh the whole dashboard
  199. me._refresh();
  200. }
  201. }
  202. else
  203. {
  204. // inserting a new dashlet
  205. var sDashletClass = ui.draggable.attr('dashlet_class');
  206. $(':itop-dashboard').dashboard('add_dashlet', {dashlet_class: sDashletClass, container: $(this), refresh: bRefresh });
  207. }
  208. },
  209. });
  210. }
  211. });
  212. });