icon_select.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. labels: {cancel: 'Cancel', pick_icon_file: 'Select an icon file to upload:', upload_dlg_title: 'Icon Upload...', upload: 'Upload...'},
  14. post_upload_to: null
  15. },
  16. // the constructor
  17. _create: function()
  18. {
  19. var me = this;
  20. var sLabel = '';
  21. var sIcon = '';
  22. if (this.options.items.length > 0)
  23. {
  24. sIcon = this.options.items[this.options.current_idx].icon;
  25. sLabel = this.options.items[this.options.current_idx].label;
  26. }
  27. this.oImg = $('<img src="'+sIcon+'" style="vertical-align: middle;">');
  28. this.oLabel = $('<span>'+sLabel+'</span>');
  29. this.oButton = $('<button><div style="display: inline-block;vertical-align: middle;"><span class="ui-icon ui-icon-triangle-1-s"/></div></button>');
  30. this.oButton.prepend(this.oLabel).prepend(this.oImg);
  31. this.oButton.click(function(event, ui) { me._on_button_clicked(event, ui); });
  32. this.element.after(this.oButton);
  33. this.element.addClass( "itop-icon-select" ).button();
  34. this.element.bind( "reverted.itop-icon-select", function(ev, data) {
  35. var idx = me._find_item(data.previous_value);
  36. if (idx != null)
  37. {
  38. me.oImg.attr('src', me.options.items[idx].icon);
  39. me.oLabel.text(me.options.items[idx].label);
  40. }
  41. });
  42. if (this.options.post_upload_to != null)
  43. {
  44. this.oUploadBtn = $('<button title="'+this.options.labels['upload']+'"><div style="display: inline-block;position: relative;vertical-align:middle;height:48px; line-height:48px; width:16px"><span style="height:16px;display:block;position:absolute;top:50%;margin-top:-8px" class="ui-icon ui-icon-circle-plus"/></div></button>');
  45. this.oUploadBtn.click( function() { me._upload_dlg(); } );
  46. this.oButton.after(this.oUploadBtn);
  47. }
  48. var id = this.element.attr('id');
  49. $('#event_bus').bind('tabshow.itop-icon-select'+id, function(event) {
  50. // Compute the offsetX the first time the 'element' becomes visible...
  51. var bVisible = me.element.parent().is(':visible');
  52. if ((me.options.offsetX == null) && (bVisible))
  53. {
  54. me._refresh();
  55. }
  56. });
  57. this.oUploadDlg = null;
  58. this._refresh();
  59. },
  60. // called when created, and later when changing options
  61. _refresh: function()
  62. {
  63. if (!this.element.parent().is(':visible'))
  64. {
  65. this.options.offsetX = null; // Menu needs to be reconstructed when the button becomes visible
  66. }
  67. else
  68. {
  69. if (this.options.items.length > 0)
  70. {
  71. this.element.val(this.options.items[this.options.current_idx].value);
  72. this.oImg.attr('src', this.options.items[this.options.current_idx].icon);
  73. this.oLabel.text(this.options.items[this.options.current_idx].label);
  74. }
  75. this._create_menu();
  76. }
  77. },
  78. _create_menu: function()
  79. {
  80. var me = this;
  81. var sMenu = '<ul>';
  82. for(var i in this.options.items)
  83. {
  84. 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>';
  85. }
  86. sMenu = sMenu + '</ul>';
  87. var iWidth = Math.max(250, this.oButton.width());
  88. 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} });
  89. },
  90. _on_button_clicked: function(event, ui)
  91. {
  92. // Adjust the position of the menu, in case the button was moved...
  93. // The simpler is to kill and rebuild the menu !!!
  94. KillAllMenus();
  95. this._create_menu();
  96. },
  97. // events bound via _bind are removed automatically
  98. // revert other modifications here
  99. _destroy: function()
  100. {
  101. this.element.removeClass( "itop-icon-select" );
  102. this.oButton.button( "destroy" );
  103. },
  104. // _setOptions is called with a hash of all options that are changing
  105. // always refresh when changing options
  106. _setOptions: function()
  107. {
  108. // in 1.9 would use _superApply
  109. this._superApply(arguments);
  110. this._refresh();
  111. },
  112. // _setOption is called for each individual option that is changing
  113. _setOption: function( key, value )
  114. {
  115. if (key == 'current_idx')
  116. {
  117. this.element.val(this.options.items[value].value).trigger('change');
  118. }
  119. // in 1.9 would use _super
  120. this._superApply(arguments);
  121. },
  122. _on_icon_selection: function(data)
  123. {
  124. this._setOptions({current_idx: data.item.attr('value')});
  125. },
  126. _find_item: function(value)
  127. {
  128. var res = null;
  129. for(var idx in this.options.items)
  130. {
  131. if (value == this.options.items[idx].value)
  132. {
  133. res = idx;
  134. break;
  135. }
  136. }
  137. return res;
  138. },
  139. add_item: function(value, label, icon, position)
  140. {
  141. if (position == 'bottom')
  142. {
  143. this.options.items.push({value: value, label: label, icon: icon });
  144. }
  145. else
  146. {
  147. // Assume 'top'
  148. this.options.items.unshift({value: value, label: label, icon: icon });
  149. }
  150. this._refresh();
  151. },
  152. _upload_dlg: function()
  153. {
  154. var me = this;
  155. this.oUploadDlg = $('<div><p>'+this.options.labels['pick_icon_file']+'</p><p><input type="file" name="file" id="file"/></p></div>');
  156. this.element.after(this.oUploadDlg);
  157. $('input[type=file]').bind('change', function() { me._do_upload(); });
  158. this.oUploadDlg.dialog({
  159. width: 400,
  160. modal: true,
  161. title: this.options.labels['upload_dlg_title'],
  162. buttons: [
  163. { text: this.options.labels['cancel'], click: function() {
  164. me.oUploadDlg.dialog( "close" );
  165. }
  166. }
  167. ],
  168. close: function() { me._on_upload_close(); }
  169. });
  170. },
  171. _on_upload_close: function()
  172. {
  173. this.oUploadDlg.remove();
  174. this.oUploadDlg = null;
  175. },
  176. _do_upload: function()
  177. {
  178. var me = this;
  179. $.ajaxFileUpload
  180. (
  181. {
  182. url: this.options.post_upload_to,
  183. secureuri:false,
  184. fileElementId:'file',
  185. dataType: 'json',
  186. success: function (data, status)
  187. {
  188. me._on_upload_complete(data);
  189. },
  190. error: function (data, status, e)
  191. {
  192. me._on_upload_error(data, status, e);
  193. }
  194. }
  195. );
  196. },
  197. _on_upload_complete: function(data)
  198. {
  199. console.log(data);
  200. console.log(data.icon);
  201. var sIcon = data.icon.replace(/&amp;/g, "&");
  202. console.log(sIcon);
  203. this.add_item(data.id, data.msg, sIcon, 'top');
  204. this.element.val(data.id);
  205. var idx = this._find_item(data.id);
  206. if (idx != null)
  207. {
  208. this.oImg.attr('src', this.options.items[idx].icon);
  209. this.oLabel.text(this.options.items[idx].label);
  210. }
  211. this.element.trigger('change');
  212. this.oUploadDlg.dialog( "close" );
  213. },
  214. _on_upload_error: function(data, status, e)
  215. {
  216. alert(e);
  217. }
  218. });
  219. });