icon_select.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //iTop Designer combo box for icons
  2. $(function()
  3. {
  4. // the widget definition, where "itop" is the namespace,
  5. // "icon_select" the widget name
  6. $.widget( "itop.icon_select",
  7. {
  8. // default options
  9. options:
  10. {
  11. items: [],
  12. current_idx: 0
  13. },
  14. // the constructor
  15. _create: function()
  16. {
  17. var me = this;
  18. var sLabel = '';
  19. var sIcon = '';
  20. if (this.options.items.length > 0)
  21. {
  22. sIcon = this.options.items[this.options.current_idx].icon;
  23. sLabel = this.options.items[this.options.current_idx].label;
  24. }
  25. this.oImg = $('<img src="'+sIcon+'" style="vertical-align: middle;">');
  26. this.oLabel = $('<span>'+sLabel+'</span>');
  27. this.oButton = $('<button><div style="display: inline-block;vertical-align: middle;"><span class="ui-icon ui-icon-triangle-1-s"/></div></button>');
  28. this.oButton.prepend(this.oLabel).prepend(this.oImg);
  29. this.element.after(this.oButton);
  30. this.element.addClass( "itop-icon-select" ).button();
  31. this.element.bind( "reverted.itop-icon-select", function(ev, data) {
  32. var idx = me._find_item(data.previous_value);
  33. if (idx != null)
  34. {
  35. me.oImg.attr('src', me.options.items[idx].icon);
  36. me.oLabel.text(me.options.items[idx].label);
  37. }
  38. });
  39. this._refresh();
  40. },
  41. // called when created, and later when changing options
  42. _refresh: function()
  43. {
  44. if (this.options.items.length > 0)
  45. {
  46. this.element.val(this.options.items[this.options.current_idx].value);
  47. this.oImg.attr('src', this.options.items[this.options.current_idx].icon);
  48. this.oLabel.text(this.options.items[this.options.current_idx].label);
  49. }
  50. this._create_menu();
  51. },
  52. _create_menu: function()
  53. {
  54. var me = this;
  55. var sMenu = '<ul>';
  56. for(var i in this.options.items)
  57. {
  58. sMenu = sMenu + '<li><a href="#" value="'+i+'"><img src="'+this.options.items[i].icon+'" style="vertical-align: middle;">'+this.options.items[i].label+'</a></li>';
  59. }
  60. sMenu = sMenu + '</ul>';
  61. var iWidth = Math.max(250, this.oButton.width());
  62. this.oMenu = this.oButton.menu({ content: sMenu, callback: function(data) {me._on_icon_selection(data);}, showSpeed: 0, maxHeight: 300, flyOut: true, width: iWidth, positionOpts: {posX: 'left', posY: 'top', offsetX: 0, offsetY: 0} });
  63. },
  64. // events bound via _bind are removed automatically
  65. // revert other modifications here
  66. _destroy: function()
  67. {
  68. this.element.removeClass( "itop-icon-select" );
  69. this.oButton.destroy();
  70. },
  71. // _setOptions is called with a hash of all options that are changing
  72. // always refresh when changing options
  73. _setOptions: function()
  74. {
  75. // in 1.9 would use _superApply
  76. $.Widget.prototype._setOptions.apply( this, arguments );
  77. this._refresh();
  78. },
  79. // _setOption is called for each individual option that is changing
  80. _setOption: function( key, value )
  81. {
  82. if (key == 'current_idx')
  83. {
  84. this.element.val(this.options.items[value].value).trigger('change');
  85. }
  86. // in 1.9 would use _super
  87. $.Widget.prototype._setOption.call( this, key, value );
  88. },
  89. _on_icon_selection: function(data)
  90. {
  91. this._setOptions({current_idx: data.item.attr('value')});
  92. },
  93. _find_item: function(value)
  94. {
  95. var res = null;
  96. for(var idx in this.options.items)
  97. {
  98. if (value == this.options.items[idx].value)
  99. {
  100. res = idx;
  101. break;
  102. }
  103. }
  104. return res;
  105. },
  106. add_item: function(value, label, position)
  107. {
  108. if (position == 'bottom')
  109. {
  110. this.options.items.push({value: value, label: label });
  111. }
  112. else
  113. {
  114. // Assume 'top'
  115. this.options.items.unshift({value: value, label: label });
  116. }
  117. this._refresh();
  118. }
  119. });
  120. });