dashboard.js 5.4 KB

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