icon_select.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. this.oUploadDlg = null;
  49. this._refresh();
  50. },
  51. // called when created, and later when changing options
  52. _refresh: function()
  53. {
  54. if (this.options.items.length > 0)
  55. {
  56. this.element.val(this.options.items[this.options.current_idx].value);
  57. this.oImg.attr('src', this.options.items[this.options.current_idx].icon);
  58. this.oLabel.text(this.options.items[this.options.current_idx].label);
  59. }
  60. this._create_menu();
  61. },
  62. _create_menu: function()
  63. {
  64. var me = this;
  65. var sMenu = '<ul>';
  66. for(var i in this.options.items)
  67. {
  68. 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>';
  69. }
  70. sMenu = sMenu + '</ul>';
  71. var iWidth = Math.max(250, this.oButton.width());
  72. 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} });
  73. },
  74. _on_button_clicked: function(event, ui)
  75. {
  76. // Adjust the position of the menu, in case the button was moved...
  77. // The simpler is to kill and rebuild the menu !!!
  78. KillAllMenus();
  79. this._create_menu();
  80. },
  81. // events bound via _bind are removed automatically
  82. // revert other modifications here
  83. _destroy: function()
  84. {
  85. this.element.removeClass( "itop-icon-select" );
  86. this.oButton.button( "destroy" );
  87. },
  88. // _setOptions is called with a hash of all options that are changing
  89. // always refresh when changing options
  90. _setOptions: function()
  91. {
  92. // in 1.9 would use _superApply
  93. this._superApply(arguments);
  94. this._refresh();
  95. },
  96. // _setOption is called for each individual option that is changing
  97. _setOption: function( key, value )
  98. {
  99. if (key == 'current_idx')
  100. {
  101. this.element.val(this.options.items[value].value).trigger('change');
  102. }
  103. // in 1.9 would use _super
  104. this._superApply(arguments);
  105. },
  106. _on_icon_selection: function(data)
  107. {
  108. this._setOptions({current_idx: data.item.attr('value')});
  109. },
  110. _find_item: function(value)
  111. {
  112. var res = null;
  113. for(var idx in this.options.items)
  114. {
  115. if (value == this.options.items[idx].value)
  116. {
  117. res = idx;
  118. break;
  119. }
  120. }
  121. return res;
  122. },
  123. add_item: function(value, label, icon, position)
  124. {
  125. if (position == 'bottom')
  126. {
  127. this.options.items.push({value: value, label: label, icon: icon });
  128. }
  129. else
  130. {
  131. // Assume 'top'
  132. this.options.items.unshift({value: value, label: label, icon: icon });
  133. }
  134. this._refresh();
  135. },
  136. _upload_dlg: function()
  137. {
  138. var me = this;
  139. this.oUploadDlg = $('<div><p>'+this.options.labels['pick_icon_file']+'</p><p><input type="file" name="file" id="file"/></p></div>');
  140. this.element.after(this.oUploadDlg);
  141. $('input[type=file]').bind('change', function() { me._do_upload(); });
  142. this.oUploadDlg.dialog({
  143. width: 400,
  144. modal: true,
  145. title: this.options.labels['upload_dlg_title'],
  146. buttons: [
  147. { text: this.options.labels['cancel'], click: function() {
  148. me.oUploadDlg.dialog( "close" );
  149. }
  150. }
  151. ],
  152. close: function() { me._on_upload_close(); }
  153. });
  154. },
  155. _on_upload_close: function()
  156. {
  157. this.oUploadDlg.remove();
  158. this.oUploadDlg = null;
  159. },
  160. _do_upload: function()
  161. {
  162. var me = this;
  163. $.ajaxFileUpload
  164. (
  165. {
  166. url: this.options.post_upload_to,
  167. secureuri:false,
  168. fileElementId:'file',
  169. dataType: 'json',
  170. success: function (data, status)
  171. {
  172. me._on_upload_complete(data);
  173. },
  174. error: function (data, status, e)
  175. {
  176. me._on_upload_error(data, status, e);
  177. }
  178. }
  179. );
  180. },
  181. _on_upload_complete: function(data)
  182. {
  183. console.log(data);
  184. console.log(data.icon);
  185. var sIcon = data.icon.replace(/&amp;/g, "&");
  186. console.log(sIcon);
  187. this.add_item(data.id, data.msg, sIcon, 'top');
  188. this.element.val(data.id);
  189. var idx = this._find_item(data.id);
  190. if (idx != null)
  191. {
  192. this.oImg.attr('src', this.options.items[idx].icon);
  193. this.oLabel.text(this.options.items[idx].label);
  194. }
  195. this.element.trigger('change');
  196. this.oUploadDlg.dialog( "close" );
  197. },
  198. _on_upload_error: function(data, status, e)
  199. {
  200. alert(e);
  201. }
  202. });
  203. });