dashboard.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. });
  213. function UploadDashboard(oOptions)
  214. {
  215. var sFileId = 'dashboard_upload_file';
  216. var oDlg = $('<div id="dashboard_upload_dlg"><form><p>'+oOptions.text+'</p><p><input type="file" id="'+sFileId+'" name="dashboard_upload_file"></p></form></div>');
  217. $('body').append(oDlg);
  218. oOptions.file_id = sFileId;
  219. oDlg.dashboard_upload_dlg(oOptions);
  220. }
  221. //jQuery UI style "widget" for managing a "import dashboard" dialog (file upload)
  222. $(function()
  223. {
  224. // the widget definition, where "itop" is the namespace,
  225. // "dashboard-upload-dlg" the widget name
  226. $.widget( "itop.dashboard_upload_dlg",
  227. {
  228. // default options
  229. options:
  230. {
  231. dashboard_id: '',
  232. file_id: '',
  233. text: 'Select a dashboard file to import',
  234. title: 'Dahsboard Import',
  235. close_btn: 'Close',
  236. submit_to: GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?operation=import_dashboard'
  237. },
  238. // the constructor
  239. _create: function()
  240. {
  241. var me = this;
  242. var oButtons = {};
  243. oButtons[this.options.close_btn] = function() {
  244. me.element.dialog('close');
  245. //me.onClose();
  246. };
  247. $('#'+this.options.file_id).bind('change', function() { me._doUpload(); } );
  248. this.element
  249. .addClass('itop-dashboard_upload_dlg')
  250. .dialog({
  251. modal: true,
  252. width: 500,
  253. height: 'auto',
  254. title: this.options.title,
  255. close: function() { me._onClose(); },
  256. buttons: oButtons
  257. });
  258. },
  259. // called when created, and later when changing options
  260. _refresh: function()
  261. {
  262. },
  263. // events bound via _bind are removed automatically
  264. // revert other modifications here
  265. destroy: function()
  266. {
  267. this.element
  268. .removeClass('itop-dashboard_upload_dlg');
  269. // call the original destroy method since we overwrote it
  270. $.Widget.prototype.destroy.call( this );
  271. },
  272. // _setOptions is called with a hash of all options that are changing
  273. _setOptions: function()
  274. {
  275. // in 1.9 would use _superApply
  276. $.Widget.prototype._setOptions.apply( this, arguments );
  277. this._refresh();
  278. },
  279. // _setOption is called for each individual option that is changing
  280. _setOption: function( key, value )
  281. {
  282. // in 1.9 would use _super
  283. $.Widget.prototype._setOption.call( this, key, value );
  284. },
  285. _onClose: function()
  286. {
  287. this.element.remove();
  288. },
  289. _doUpload: function()
  290. {
  291. var me = this;
  292. $.ajaxFileUpload
  293. (
  294. {
  295. url: me.options.submit_to+'&id='+me.options.dashboard_id,
  296. secureuri:false,
  297. fileElementId: me.options.file_id,
  298. dataType: 'json',
  299. success: function (data, status)
  300. {
  301. if(typeof(data.error) != 'undefined')
  302. {
  303. if(data.error != '')
  304. {
  305. alert(data.error);
  306. me.element.dialog('close');
  307. }
  308. else
  309. {
  310. me.element.dialog('close');
  311. location.reload();
  312. }
  313. }
  314. },
  315. error: function (data, status, e)
  316. {
  317. alert(e);
  318. me.element.dialog('close');
  319. }
  320. }
  321. );
  322. }
  323. });
  324. });