dashboard.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.cells = [];
  72. this.element.find('.layout_cell').each(function() {
  73. var aList = [];
  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. oState[sId] = oDashletParams;
  81. aList.push({dashlet_id: sId, dashlet_class: oDashletParams.dashlet_class} );
  82. }
  83. });
  84. if (aList.length == 0)
  85. {
  86. oState[0] = {dashlet_id: 0, dashlet_class: 'DashletEmptyCell'};
  87. aList.push({dashlet_id: 0, dashlet_class: 'DashletEmptyCell'});
  88. }
  89. oState.cells.push(aList);
  90. });
  91. oState.dashboard_id = this.options.dashboard_id;
  92. oState.layout_class = this.options.layout_class;
  93. oState.title = this.options.title;
  94. return oState;
  95. },
  96. save: function()
  97. {
  98. var oParams = this._get_state(this.options.submit_parameters);
  99. var me = this;
  100. $.post(this.options.submit_to, oParams, function(data){
  101. me.ajax_div.html(data);
  102. });
  103. },
  104. add_dashlet: function(options)
  105. {
  106. var sDashletId = this._get_new_id();
  107. var oDashlet = $('<div class="dashlet" id="dashlet_'+sDashletId+'"/>');
  108. oDashlet.appendTo(options.container);
  109. var oDashletProperties = $('<div class="dashlet_properties" id="dashlet_properties_'+sDashletId+'"/>');
  110. oDashletProperties.appendTo($('#dashlet_properties'));
  111. var oParams = this.options.new_dashlet_parameters;
  112. var sDashletClass = options.dashlet_class;
  113. oParams.dashlet_class = sDashletClass;
  114. oParams.dashlet_id = sDashletId;
  115. var me = this;
  116. $.post(this.options.render_to, oParams, function(data){
  117. me.ajax_div.html(data);
  118. $('#dashlet_'+sDashletId)
  119. .dashlet({dashlet_id: sDashletId, dashlet_class: sDashletClass})
  120. .dashlet('deselect_all')
  121. .dashlet('select')
  122. .draggable({
  123. revert: 'invalid', appendTo: 'body', zIndex: 9999,
  124. helper: function() {
  125. var oDragItem = $(this).dashlet('get_drag_icon');
  126. return oDragItem;
  127. },
  128. cursorAt: { top: 16, left: 16 },
  129. });
  130. });
  131. },
  132. _get_new_id: function()
  133. {
  134. var iMaxId = 0;
  135. this.element.find(':itop-dashlet').each(function() {
  136. var oDashlet = $(this).data('dashlet');
  137. if(oDashlet)
  138. {
  139. var oDashletParams = oDashlet.get_params();
  140. var id = parseInt(oDashletParams.dashlet_id, 10);
  141. if (id > iMaxId) iMaxId = id;
  142. }
  143. });
  144. return 1 + iMaxId;
  145. },
  146. _make_draggable: function()
  147. {
  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 oDashlet = ui.draggable;
  161. if (oDashlet.hasClass('dashlet'))
  162. {
  163. // moving around a dashlet
  164. oDashlet.detach();
  165. oDashlet.css({top: 0, left: 0});
  166. oDashlet.appendTo($(this));
  167. }
  168. else
  169. {
  170. // inserting a new dashlet
  171. var sDashletClass = ui.draggable.attr('dashlet_class');
  172. $(':itop-dashboard').dashboard('add_dashlet', {dashlet_class: sDashletClass, container: $(this) })
  173. }
  174. },
  175. });
  176. },
  177. _on_keyup: function(event)
  178. {
  179. console.log('Key pressed in Dashlet');
  180. }
  181. });
  182. });