dashboard.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. this.ajax_div = $('<div></div>').appendTo(this.element);
  27. this._make_draggable();
  28. },
  29. // called when created, and later when changing options
  30. _refresh: function()
  31. {
  32. var oParams = this._get_state(this.options.render_parameters);
  33. var me = this;
  34. $.post(this.options.render_to, oParams, function(data){
  35. me.element.html(data);
  36. me._make_draggable();
  37. });
  38. },
  39. // events bound via _bind are removed automatically
  40. // revert other modifications here
  41. destroy: function()
  42. {
  43. this.element
  44. .removeClass('itop-dashboard');
  45. this.ajax_div.remove();
  46. $(document).unbind('keyup.dashboard_editor');
  47. // call the original destroy method since we overwrote it
  48. $.Widget.prototype.destroy.call( this );
  49. },
  50. // _setOptions is called with a hash of all options that are changing
  51. _setOptions: function()
  52. {
  53. // in 1.9 would use _superApply
  54. $.Widget.prototype._setOptions.apply( this, arguments );
  55. this._refresh();
  56. },
  57. // _setOption is called for each individual option that is changing
  58. _setOption: function( key, value )
  59. {
  60. // in 1.9 would use _super
  61. $.Widget.prototype._setOption.call( this, key, value );
  62. },
  63. _get_state: function(oMergeInto)
  64. {
  65. var oState = oMergeInto;
  66. oState.cells = [];
  67. this.element.find('.layout_cell').each(function() {
  68. var aList = [];
  69. $(this).find(':itop-dashlet').each(function() {
  70. var oDashlet = $(this).data('dashlet');
  71. if(oDashlet)
  72. {
  73. var oDashletParams = oDashlet.get_params();
  74. var sId = oDashletParams.dashlet_id;
  75. oState[sId] = oDashletParams;
  76. aList.push({dashlet_id: sId, dashlet_class: oDashletParams.dashlet_class} );
  77. }
  78. });
  79. if (aList.length == 0)
  80. {
  81. oState[0] = {dashlet_id: 0, dashlet_class: 'DashletEmptyCell'};
  82. aList.push({dashlet_id: 0, dashlet_class: 'DashletEmptyCell'});
  83. }
  84. oState.cells.push(aList);
  85. });
  86. oState.dashboard_id = this.options.dashboard_id;
  87. oState.layout_class = this.options.layout_class;
  88. oState.title = this.options.title;
  89. return oState;
  90. },
  91. save: function()
  92. {
  93. var oParams = this._get_state(this.options.submit_parameters);
  94. var me = this;
  95. $.post(this.options.submit_to, oParams, function(data){
  96. me.ajax_div.html(data);
  97. });
  98. },
  99. add_dashlet: function(options)
  100. {
  101. var sDashletId = this._get_new_id();
  102. var oDashlet = $('<div class="dashlet" id="dashlet_'+sDashletId+'"/>');
  103. oDashlet.appendTo(options.container);
  104. var oDashletProperties = $('<div class="dashlet_properties" id="dashlet_properties_'+sDashletId+'"/>');
  105. oDashletProperties.appendTo($('#dashlet_properties'));
  106. var oParams = this.options.new_dashlet_parameters;
  107. var sDashletClass = options.dashlet_class;
  108. oParams.dashlet_class = sDashletClass;
  109. oParams.dashlet_id = sDashletId;
  110. var me = this;
  111. $.post(this.options.render_to, oParams, function(data){
  112. me.ajax_div.html(data);
  113. $('#dashlet_'+sDashletId)
  114. .dashlet({dashlet_id: sDashletId, dashlet_class: sDashletClass})
  115. .dashlet('deselect_all')
  116. .dashlet('select')
  117. .draggable({
  118. revert: 'invalid', appendTo: 'body', zIndex: 9999,
  119. helper: function() {
  120. var oDragItem = $(this).dashlet('get_drag_icon');
  121. return oDragItem;
  122. },
  123. cursorAt: { top: 16, left: 16 },
  124. });
  125. if (options.refresh)
  126. {
  127. me._refresh();
  128. }
  129. });
  130. },
  131. _get_new_id: function()
  132. {
  133. var iMaxId = 0;
  134. this.element.find(':itop-dashlet').each(function() {
  135. var oDashlet = $(this).data('dashlet');
  136. if(oDashlet)
  137. {
  138. var oDashletParams = oDashlet.get_params();
  139. var id = parseInt(oDashletParams.dashlet_id, 10);
  140. if (id > iMaxId) iMaxId = id;
  141. }
  142. });
  143. return 1 + iMaxId;
  144. },
  145. _make_draggable: function()
  146. {
  147. var me = this;
  148. this.element.find('.dashlet').draggable({
  149. revert: 'invalid', appendTo: 'body', zIndex: 9999,
  150. helper: function() {
  151. var oDragItem = $(this).dashlet('get_drag_icon');
  152. return oDragItem;
  153. },
  154. cursorAt: { top: 16, left: 16 },
  155. });
  156. this.element.find('table td').droppable({
  157. accept: '.dashlet,.dashlet_icon',
  158. drop: function(event, ui) {
  159. $( this ).find( ".placeholder" ).remove();
  160. var bRefresh = $(this).hasClass('layout_extension');
  161. var oDashlet = ui.draggable;
  162. if (oDashlet.hasClass('dashlet'))
  163. {
  164. // moving around a dashlet
  165. oDashlet.detach();
  166. oDashlet.css({top: 0, left: 0});
  167. oDashlet.appendTo($(this));
  168. if( bRefresh )
  169. {
  170. // The layout was extended... refresh the whole dashboard
  171. me._refresh();
  172. }
  173. }
  174. else
  175. {
  176. // inserting a new dashlet
  177. var sDashletClass = ui.draggable.attr('dashlet_class');
  178. $(':itop-dashboard').dashboard('add_dashlet', {dashlet_class: sDashletClass, container: $(this), refresh: bRefresh });
  179. }
  180. },
  181. });
  182. }
  183. });
  184. });