dashlet.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. this.closeBox = $('<div class="close-box"/>');
  22. this.closeBox.click(function() { me._remove_dashlet(); }).hide().prependTo(this.element);
  23. },
  24. // called when created, and later when changing options
  25. _refresh: function()
  26. {
  27. },
  28. // events bound via _bind are removed automatically
  29. // revert other modifications here
  30. destroy: function()
  31. {
  32. this.element
  33. .removeClass('itop-dashlet')
  34. .unbind('click.itop-dashlet');
  35. this.closeBox.remove();
  36. // call the original destroy method since we overwrote it
  37. $.Widget.prototype.destroy.call( this );
  38. },
  39. // _setOptions is called with a hash of all options that are changing
  40. _setOptions: function()
  41. {
  42. // in 1.9 would use _superApply
  43. $.Widget.prototype._setOptions.apply( this, arguments );
  44. },
  45. // _setOption is called for each individual option that is changing
  46. _setOption: function( key, value )
  47. {
  48. // in 1.9 would use _super
  49. $.Widget.prototype._setOption.call( this, key, value );
  50. },
  51. select: function()
  52. {
  53. this.element.addClass('dashlet-selected');
  54. this.closeBox.fadeIn(500);
  55. $('#event_bus').trigger('dashlet-selected', {'dashlet_id': this.options.dashlet_id, 'dashlet_class': this.options.dashlet_class})
  56. },
  57. deselect: function()
  58. {
  59. this.element.removeClass('dashlet-selected');
  60. this.closeBox.hide();
  61. },
  62. deselect_all: function()
  63. {
  64. $(':itop-dashlet').each(function(){
  65. var sId = $(this).attr('id');
  66. var oWidget = $(this).data('dashlet');
  67. if (oWidget)
  68. {
  69. oWidget.deselect();
  70. }
  71. });
  72. },
  73. _on_click: function(event)
  74. {
  75. this.deselect_all();
  76. this.select();
  77. },
  78. get_params: function()
  79. {
  80. var oParams = {};
  81. var oProperties = $('#dashlet_properties_'+this.options.dashlet_id);
  82. oProperties.find(':itop-property_field').each(function(){
  83. var oWidget = $(this).data('property_field');
  84. if (oWidget)
  85. {
  86. var oVal = oWidget._get_committed_value();
  87. oParams[oVal.name] = oVal.value;
  88. }
  89. });
  90. oParams.dashlet_id = this.options.dashlet_id;
  91. oParams.dashlet_class = this.options.dashlet_class;
  92. return oParams;
  93. },
  94. get_drag_icon: function()
  95. {
  96. var oDragItem = $('#dashlet_'+this.options.dashlet_class).clone();
  97. oDragItem.css({zIndex: 999});
  98. oDragItem.appendTo('body');
  99. return oDragItem;
  100. },
  101. _remove_dashlet: function()
  102. {
  103. $('#dashlet_properties_'+this.options.dashlet_id).remove();
  104. this.element.remove();
  105. }
  106. });
  107. });