dashboard.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. },
  19. // the constructor
  20. _create: function()
  21. {
  22. var me = this;
  23. this.element
  24. .addClass('itop-dashboard');
  25. this.ajax_div = $('<div></div>').appendTo(this.element);
  26. },
  27. // called when created, and later when changing options
  28. _refresh: function()
  29. {
  30. var oParams = this._get_state(this.options.render_parameters);
  31. var me = this;
  32. $.post(this.options.render_to, oParams, function(data){
  33. me.element.html(data);
  34. });
  35. },
  36. // events bound via _bind are removed automatically
  37. // revert other modifications here
  38. destroy: function()
  39. {
  40. this.element
  41. .removeClass('itop-dashboard');
  42. this.ajax_div.remove();
  43. // call the original destroy method since we overwrote it
  44. $.Widget.prototype.destroy.call( this );
  45. },
  46. // _setOptions is called with a hash of all options that are changing
  47. _setOptions: function()
  48. {
  49. // in 1.9 would use _superApply
  50. $.Widget.prototype._setOptions.apply( this, arguments );
  51. this._refresh();
  52. },
  53. // _setOption is called for each individual option that is changing
  54. _setOption: function( key, value )
  55. {
  56. // in 1.9 would use _super
  57. $.Widget.prototype._setOption.call( this, key, value );
  58. if (key == 'layout')
  59. {
  60. _refresh();
  61. }
  62. },
  63. _get_state: function(oMergeInto)
  64. {
  65. var oState = oMergeInto;
  66. oState.dashlets = [];
  67. this.element.find(':itop-dashlet').each(function() {
  68. var oDashlet = $(this).data('dashlet');
  69. if(oDashlet)
  70. {
  71. var oDashletParams = oDashlet.get_params();
  72. var sId = oDashletParams.dashlet_id;
  73. oState[sId] = oDashletParams;
  74. oState.dashlets.push({dashlet_id: sId, dashlet_class: oDashletParams.dashlet_class} );
  75. }
  76. });
  77. oState.dashboard_id = this.options.dashboard_id;
  78. oState.layout_class = this.options.layout_class;
  79. oState.title = this.options.title;
  80. return oState;
  81. },
  82. save: function()
  83. {
  84. var oParams = this._get_state(this.options.submit_parameters);
  85. var me = this;
  86. $.post(this.options.submit_to, oParams, function(data){
  87. me.ajax_div.html(data);
  88. });
  89. }
  90. });
  91. });