dashlet.js 3.1 KB

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