dashlet.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // jQuery UI style "widget" for editing an iTop "dashlet"
  2. $(function()
  3. {
  4. // the widget definition, where "itop" is the namespace,
  5. // "dashlet" the widget name
  6. $.widget( "itop.dashlet",
  7. {
  8. // default options
  9. options:
  10. {
  11. dashlet_id: '',
  12. dashlet_class: ''
  13. },
  14. // the constructor
  15. _create: function()
  16. {
  17. var me = this;
  18. this.element
  19. .addClass('itop-dashlet')
  20. .bind('click.itop-dashlet', function(event) { me._on_click(event); } );
  21. },
  22. // called when created, and later when changing options
  23. _refresh: function()
  24. {
  25. },
  26. // events bound via _bind are removed automatically
  27. // revert other modifications here
  28. destroy: function()
  29. {
  30. this.element
  31. .removeClass('itop-dashlet')
  32. .unbind('click.itop-dashlet');
  33. // call the original destroy method since we overwrote it
  34. $.Widget.prototype.destroy.call( this );
  35. },
  36. // _setOptions is called with a hash of all options that are changing
  37. _setOptions: function()
  38. {
  39. // in 1.9 would use _superApply
  40. $.Widget.prototype._setOptions.apply( this, arguments );
  41. },
  42. // _setOption is called for each individual option that is changing
  43. _setOption: function( key, value )
  44. {
  45. // in 1.9 would use _super
  46. $.Widget.prototype._setOption.call( this, key, value );
  47. },
  48. _select: function()
  49. {
  50. this.element.addClass('dashlet-selected');
  51. $('#event_bus').trigger('dashlet-selected', {'dashlet_id': this.options.dashlet_id, 'dashlet_class': this.options.dashlet_class})
  52. },
  53. _deselect: function()
  54. {
  55. this.element.removeClass('dashlet-selected');
  56. },
  57. _on_click: function(event)
  58. {
  59. var sCurrentId = this.element.attr('id');
  60. $(':itop-dashlet').each(function(){
  61. var sId = $(this).attr('id');
  62. var oWidget = $(this).data('dashlet');
  63. if (oWidget)
  64. {
  65. if (sCurrentId != sId)
  66. {
  67. oWidget._deselect();
  68. }
  69. }
  70. });
  71. this._select();
  72. },
  73. get_params: function()
  74. {
  75. var oParams = {};
  76. var oProperties = $('#dashlet_properties_'+this.options.dashlet_id);
  77. oProperties.find(':itop-property_field').each(function(){
  78. var oWidget = $(this).data('property_field');
  79. if (oWidget)
  80. {
  81. var oVal = oWidget._get_committed_value();
  82. oParams[oVal.name] = oVal.value;
  83. }
  84. });
  85. oParams.dashlet_id = this.options.dashlet_id;
  86. oParams.dashlet_class = this.options.dashlet_class;
  87. return oParams;
  88. }
  89. });
  90. });