Guest User

Untitled

a guest
Mar 10th, 2018
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.31 KB | None | 0 0
  1. // +---------------------------------------------------------------------------+
  2. // | This file is part of a Revolution Linux project. |
  3. // | Copyright (C) Revolution Linux |
  4. // | |
  5. // | For the full copyright and license information, please view the LICENSE |
  6. // | file that was distributed with this source code. |
  7. // +---------------------------------------------------------------------------+
  8.  
  9. var RSuggestListBox = RSuggestTextBox.extend({
  10.  
  11. /**
  12. * @var object The current listbox, select element.
  13. */
  14. select : null,
  15.  
  16. /**
  17. * @var object The hidden field which contains the value.
  18. */
  19. hidden : null,
  20.  
  21. /**
  22. * @var object The class options.
  23. */
  24. options : {
  25. // onMatch : $empty
  26. // onMatchs : $empty
  27. // onMissed : $empty
  28. button : null,
  29. },
  30.  
  31. /**
  32. * Constructior. Set the listbox, select element which will be replaced
  33. * by the new listbox.
  34. * @param object The select object.
  35. * @param object The class options.
  36. * @return void
  37. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  38. * @since 1.0.0
  39. */
  40. initialize : function(select, options) {
  41. this.values = [];
  42. this.select = $(select);
  43. this.setOptions(options);
  44. // create the hidden field which will contains the value
  45. this.hidden = new Element('input');
  46. this.hidden.setProperty('type', 'hidden');
  47. this.hidden.setProperty('name', this.select.name);
  48. this.hidden.setProperty('value', '');
  49. // create the textbox which will replace the select
  50. this.textbox = new Element('input');
  51. this.textbox.setProperty('type', 'text');
  52. this.textbox.setProperty('name', 'temp');
  53. this.textbox.setProperty('class', 'listbox');
  54. // replace the select with the textbox
  55. this.select.replaceWith(this.textbox);
  56. this.hidden.injectAfter(this.textbox);
  57. // load the values and sort them
  58. for (var i = 0; i < this.select.options.length; i++) {
  59. this.values.push({
  60. label : this.select.options[i].getText(),
  61. value : this.select.options[i].getProperty('value'),
  62. });
  63. }
  64. // create the listbox which will contains
  65. this.listbox = new RListBox();
  66. this.listbox.hide();
  67. this.listbox.injectAfter(this.textbox);
  68. this.listbox.addEvent('keyup', this.suggest.bind(this));
  69. this.listbox.addEvent('keydown', this.choose.bind(this));
  70. this.textbox.addEvent('keyup', this.suggest.bind(this));
  71. this.textbox.addEvent('keydown', this.choose.bind(this));
  72. },
  73.  
  74. /**
  75. * Put a suggestion into the textbox. This method will be automatically
  76. * called by the textbox keyup event.
  77. * @param object The event object.
  78. * @return void
  79. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  80. * @since 1.0.0
  81. */
  82. suggest : function(event) {
  83.  
  84. var len = this.textbox.value.length;
  85. if (len > 0) {
  86. // filter the correct keys
  87. if ((event.keyCode >= this.KEY_0 && event.keyCode <= this.KEY_9) ||
  88. (event.keyCode >= this.KEY_A && event.keyCode <= this.KEY_Z) ||
  89. (event.keyCode == this.KEY_BACKSPACE)) {
  90. // look for keys from 0 to 9 and a to z. only then we will try
  91. // to find a suggestion for the current input. we make sure the
  92. // list gets updated when the user hit teh backspace key
  93. this.listbox.hide();
  94. this.listbox.clear();
  95. var results = this.getSuggestions();
  96. // pick up the suggestions and add each one into the list
  97. if (results.length > 0) {
  98. for (var i = 0; i < results.length; i++) {
  99. // add an event so the user will be able to make a
  100. // selection by clicking on an item.
  101. this.listbox.addItem(results[i].label, results[i].value, this.listbox).addEvent('click', function() {
  102. var item = this.listbox.getSelectedItem();
  103. this.textbox.value = item.getLabel();
  104. this.hidden.value = item.getValue();
  105. this.listbox.unselectAll();
  106. this.listbox.hide();
  107. }.bind(this));
  108. }
  109. // select the first item in the list and display it
  110. this.listbox.select(0);
  111. this.listbox.show('block');
  112. }
  113. } else {
  114. // manually handle all the other keys
  115. switch (event.keyCode) {
  116. case 13 :
  117. // get the current item and set the textbox and hidden
  118. // field content that match the choosed item
  119. var item = this.listbox.getSelectedItem();
  120. this.textbox.value = item.getLabel();
  121. this.hidden.value = item.getValue();
  122. this.listbox.unselectAll();
  123. this.listbox.hide();
  124. break;
  125. }
  126. }
  127. this.parent(event);
  128. } else {
  129. this.listbox.hide();
  130. this.listbox.clear();
  131. }
  132. },
  133.  
  134. /**
  135. *
  136. * @return string A single suggestion.
  137. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  138. * @since 1.0.0
  139. */
  140. choose : function(event) {
  141. switch (event.keyCode) {
  142. case 38 : this.listbox.selectPrev(); break;
  143. case 40 : this.listbox.selectNext(); break;
  144. }
  145. },
  146.  
  147. /**
  148. * Return a single suggestion string from the array of possible values.
  149. * @return string A single suggestion.
  150. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  151. * @since 1.0.0
  152. */
  153. getSuggestion : function() {
  154. var value = this.textbox.value.toLowerCase();
  155. if (value.length) {
  156. this.values.each(function(elem) {
  157. if (elem.label.toLowerCase().indexOf(value) > -1) return elem;
  158. });
  159. }
  160. return null;
  161. },
  162.  
  163. /**
  164. * Return an array of suggestions that match the current input.
  165. * @return array An array of suggestions.
  166. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  167. * @since 1.0.0
  168. */
  169. getSuggestions : function() {
  170. console.log('NEW METHOD');
  171. var items = [];
  172. var value = this.textbox.value.toLowerCase();
  173. if (value.length) {
  174. this.values.each(function(elem) {
  175. if (elem.label.toLowerCase().indexOf(value) > -1) items.push(elem);
  176. });
  177. }
  178. return items;
  179. },
  180.  
  181. /**
  182. * Insert a suggestion in the textbox.
  183. * @param string The suggestion.
  184. * @return void
  185. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  186. * @since 1.0.0
  187. */
  188. setSuggestion : function(item) {
  189. var len = this.textbox.value.length;
  190. this.hidden.value = item.value;
  191. this.textbox.value = item.label;
  192. this.selectRange(len, item.label.length);
  193. }
  194. });
  195.  
  196. // +---------------------------------------------------------------------------+
  197. // | This file is part of a Revolution Linux project. |
  198. // | Copyright (C) Revolution Linux |
  199. // | |
  200. // | For the full copyright and license information, please view the LICENSE |
  201. // | file that was distributed with this source code. |
  202. // +---------------------------------------------------------------------------+
  203.  
  204. /**
  205. * RSuggestTextBox is a class which will put suggestions inside a textbox based
  206. * on the user input. The array of possibilities will be stored inside the object.
  207. * @package core
  208. * @subpackage form
  209. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  210. * @copyright Revolution Linux
  211. * @since 1.0.0
  212. * @version 1.0.0
  213. */
  214. var RSuggestTextBox = RPlugin.extend({
  215.  
  216. /**
  217. * @var int Some key constats.
  218. */
  219. KEY_0 : 48, KEY_9 : 57, KEY_A : 65, KEY_Z : 90, KEY_BACKSPACE : 9, KEY_ENTER : 13,
  220.  
  221. /**
  222. * @var object The textbox.
  223. */
  224. textbox : null,
  225.  
  226. /**
  227. * @var array The possible values.
  228. */
  229. values : null,
  230.  
  231. /**
  232. * @var object The class options.
  233. */
  234. options : {
  235. // onMatch : $empty
  236. // onMatchs : $empty
  237. // onMissed : $empty
  238.  
  239. },
  240.  
  241. /**
  242. * Constructor. Set the textbox and the values which will be used to make
  243. * a suggestion based on the textbox value.
  244. * @param object The textbox.
  245. * @param array The possible values.
  246. * @return void
  247. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  248. * @since 1.0.0
  249. */
  250. initialize : function(textbox, values, options) {
  251. this.values = values.sort();
  252. this.textbox = $(textbox);
  253. this.textbox.addEvent('keyup', this.suggest.bind(this));
  254. this.setOptions(options);
  255. },
  256.  
  257. /**
  258. * Put a suggestion into the textbox. This method will be automatically
  259. * called by the textbox keyup event.
  260. * @param object The event object.
  261. * @return void
  262. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  263. * @since 1.0.0
  264. */
  265. suggest : function(event) {
  266. var len = this.textbox.value.length;
  267. if (len > 0) {
  268. // look for keys from 0 to 9 and a to z. only then we will try
  269. // to find a suggestion for the current input
  270. if ((event.keyCode >= this.KEY_0 && event.keyCode <= this.KEY_9) ||
  271. (event.keyCode >= this.KEY_A && event.keyCode <= this.KEY_Z) ||
  272. (event.keyCode == this.KEY_BACKSPACE)) {
  273. var results = this.getSuggestions();
  274. switch (results.length) {
  275. case 0 : this.fireEvent('onMissed'); break;
  276. case 1 : this.fireEvent('onMatch'); break;
  277. default : this.fireEvent('onMatchs'); break;
  278. }
  279. if (results.length) {
  280. // place the suggestion inside the textbox
  281. this.setSuggestion(results[0]);
  282. }
  283. } else {
  284. switch (event.keyCode) {
  285. case 13 :
  286. // enter, fill the current selection
  287. this.selectRange(len, len);
  288. return;
  289. }
  290. }
  291. }
  292. },
  293.  
  294. /**
  295. * Return a single suggestion string from the array of possible values.
  296. * @return string A single suggestion.
  297. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  298. * @since 1.0.0
  299. */
  300. getSuggestion : function() {
  301. var value = this.textbox.value.toLowerCase();
  302. if (value.length) {
  303. this.values.each(function(elem) {
  304. if (elem.toLowerCase().indexOf(value) > -1) return elem;
  305. });
  306. }
  307. return null;
  308. },
  309.  
  310. /**
  311. * Return an array of suggestions that match the current input.
  312. * @return array An array of suggestions.
  313. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  314. * @since 1.0.0
  315. */
  316. getSuggestions : function() {
  317. console.log('OLD METHOD');
  318. var items = [];
  319. var value = this.textbox.value.toLowerCase();
  320. if (value.length) {
  321. this.values.each(function(elem) {
  322. if (elem.toLowerCase().indexOf(value) > -1) items.push(elem);
  323. });
  324. }
  325. return items;
  326. },
  327.  
  328. /**
  329. * Insert a suggestion in the textbox.
  330. * @param string The suggestion.
  331. * @return void
  332. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  333. * @since 1.0.0
  334. */
  335. setSuggestion : function(suggestion) {
  336. var len = this.textbox.value.length;
  337. this.textbox.value = suggestion;
  338. this.selectRange(len, suggestion.length);
  339. },
  340.  
  341. /**
  342. * Create a selected range in the current textbox.
  343. * @param int The start index.
  344. * @param in The range length.
  345. * @return void
  346. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  347. * @since 1.0.0
  348. */
  349. selectRange : function(start, length) {
  350. if (this.textbox.createTextRange) {
  351. // create a range in internet explorer
  352. var r = this.textbox.createTextRange();
  353. r.moveStart('character', start);
  354. r.moveEnd('character', length - this.textbox.value.length);
  355. } else if (this.textbox.setSelectionRange) {
  356. // create a range in firefox
  357. this.textbox.setSelectionRange(start, length);
  358. }
  359. this.textbox.focus();
  360. }
  361. });
Add Comment
Please, Sign In to add comment