Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

jquery.jec-1.3.1.js 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /**
  2. * jQuery jEC (jQuery Editable Combobox) 1.3.1
  3. * http://code.google.com/p/jquery-jec
  4. *
  5. * Copyright (c) 2008-2009 Lukasz Rajchel (lukasz@rajchel.pl | http://rajchel.pl)
  6. * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
  7. * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
  8. *
  9. * Documentation : http://code.google.com/p/jquery-jec/wiki/Documentation
  10. * Changelog : http://code.google.com/p/jquery-jec/wiki/Changelog
  11. *
  12. * Contributors : Lukasz Rajchel, Artem Orlov
  13. */
  14. /*jslint white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true,
  15. bitwise: true, regexp: true, strict: true, newcap: true, immed: true, maxerr: 50, indent: 4,
  16. maxlen: 100*/
  17. /*global Array, Math, String, clearInterval, document, jQuery, setInterval*/
  18. /*members ':', Handle, Remove, Set, acceptedKeys, addClass, all, append, appendTo, array, attr,
  19. before, bind, blinkingCursor, blinkingCursorInterval, blur, bool, browser, ceil, change, charCode,
  20. classes, clearCursor, click, css, cursorState, data, destroy, disable, each, editable, enable, eq,
  21. expr, extend, filter, find, floor, fn, focus, focusOnNewOption, fromCharCode, get, getId,
  22. handleCursor, ignoredKeys, ignoreOptGroups, inArray, init, initJS, integer, isArray, isPlainObject,
  23. jEC, jECTimer, jec, jecKill, jecOff, jecOn, jecPref, jecValue, keyCode, keyDown, keyPress,
  24. keyRange, keyUp, keys, length, max, maxLength, min, msie, object, openedState, optionClasses,
  25. optionStyles, parent, position, pref, push, random, remove, removeAttr, removeClass, removeData,
  26. safari, setEditableOption, styles, substring, text, trigger triggerChangeEvent, unbind, uneditable,
  27. useExistingOptions, val, value, valueIsEditable*/
  28. 'use strict';
  29. (function ($) {
  30. $.jEC = (function () {
  31. var pluginClass = 'jecEditableOption', cursorClass = 'hasCursor', options = {},
  32. values = {}, lastKeyCode, defaults, Validators, EventHandlers, Combobox,
  33. activeCombobox;
  34. defaults = {
  35. position: 0,
  36. ignoreOptGroups: false,
  37. maxLength: 255,
  38. classes: [],
  39. styles: {},
  40. optionClasses: [],
  41. optionStyles: {},
  42. triggerChangeEvent: false,
  43. focusOnNewOption: false,
  44. useExistingOptions: false,
  45. blinkingCursor: false,
  46. blinkingCursorInterval: 1000,
  47. ignoredKeys: [],
  48. acceptedKeys: [[32, 126], [191, 382]]
  49. };
  50. Validators = (function () {
  51. return {
  52. integer: function (value) {
  53. return typeof value === 'number' && Math.ceil(value) === Math.floor(value);
  54. },
  55. keyRange: function (value) {
  56. var min, max;
  57. if (typeof value === 'object' && !$.isArray(value)) {
  58. min = value.min;
  59. max = value.max;
  60. } else if ($.isArray(value) && value.length === 2) {
  61. min = value[0];
  62. max = value[1];
  63. }
  64. return Validators.integer(min) && Validators.integer(max) && min <= max;
  65. }
  66. };
  67. }());
  68. EventHandlers = (function () {
  69. var getKeyCode;
  70. getKeyCode = function (event) {
  71. var charCode = event.charCode;
  72. if (charCode !== undefined && charCode !== 0) {
  73. return charCode;
  74. } else {
  75. return event.keyCode;
  76. }
  77. };
  78. return {
  79. // focus event handler
  80. // enables blinking cursor
  81. focus: function (event) {
  82. var opt = options[Combobox.getId($(this))];
  83. if (opt.blinkingCursor && $.jECTimer === undefined) {
  84. activeCombobox = $(this);
  85. $.jECTimer = setInterval($.jEC.handleCursor, opt.blinkingCursorInterval);
  86. }
  87. },
  88. // blur event handler
  89. // disables blinking cursor
  90. blur: function (event) {
  91. if ($.jECTimer !== undefined) {
  92. clearInterval($.jECTimer);
  93. $.jECTimer = undefined;
  94. activeCombobox = undefined;
  95. Combobox.clearCursor($(this));
  96. }
  97. Combobox.openedState($(this), false);
  98. },
  99. // keydown event handler
  100. // handles keys pressed on select (backspace and delete must be handled
  101. // in keydown event in order to work in IE)
  102. keyDown: function (event) {
  103. var keyCode = getKeyCode(event), option, value;
  104. lastKeyCode = keyCode;
  105. switch (keyCode) {
  106. case 8: // backspace
  107. case 46: // delete
  108. option = $(this).find('option.' + pluginClass);
  109. if (option.val().length >= 1) {
  110. value = option.text().substring(0, option.text().length - 1);
  111. option.val(value).text(value).prop('selected', true);
  112. }
  113. return (keyCode !== 8);
  114. default:
  115. break;
  116. }
  117. },
  118. // keypress event handler
  119. // handles the rest of the keys (keypress event gives more informations
  120. // about pressed keys)
  121. keyPress: function (event) {
  122. var keyCode = getKeyCode(event), opt = options[Combobox.getId($(this))],
  123. option, value, specialKeys, exit = false, text;
  124. Combobox.clearCursor($(this));
  125. if (keyCode !== 9 && keyCode !== 13 && keyCode !== 27) {
  126. // special keys codes
  127. specialKeys = [37, 38, 39, 40, 46];
  128. // handle special keys
  129. $.each(specialKeys, function (i, val) {
  130. if (keyCode === val && keyCode === lastKeyCode) {
  131. exit = true;
  132. }
  133. });
  134. // don't handle ignored keys
  135. if (!exit && $.inArray(keyCode, opt.ignoredKeys) === -1) {
  136. // remove selection from all options
  137. $(this).find('option:selected').prop('selected',false);
  138. if ($.inArray(keyCode, opt.acceptedKeys) !== -1) {
  139. option = $(this).find('option.' + pluginClass).first();
  140. text = option.text();
  141. if (text.length < opt.maxLength) {
  142. value = text + String.fromCharCode(keyCode);
  143. option.val(value).text(value).attr('label',value);
  144. }
  145. option.prop('selected', true);
  146. }
  147. }
  148. return false;
  149. }
  150. },
  151. keyUp: function (event) {
  152. var opt = options[Combobox.getId($(this))];
  153. if (opt.triggerChangeEvent) {
  154. $(this).trigger('change');
  155. }
  156. },
  157. // change event handler
  158. // handles editable option changing based on a pre-existing values
  159. change: function () {
  160. var opt = options[Combobox.getId($(this))];
  161. if (opt.useExistingOptions) {
  162. Combobox.setEditableOption($(this));
  163. }
  164. },
  165. click: function () {
  166. if (!$.browser.safari) {
  167. Combobox.openedState($(this), !Combobox.openedState($(this)));
  168. }
  169. }
  170. };
  171. }());
  172. // Combobox
  173. Combobox = (function () {
  174. var Parameters, EditableOption, generateId, setup;
  175. // validates and set combobox parameters
  176. Parameters = (function () {
  177. var Set, Remove, Handle;
  178. Set = (function () {
  179. var parseKeys, Handles;
  180. parseKeys = function (value) {
  181. var keys = [];
  182. if ($.isArray(value)) {
  183. $.each(value, function (i, val) {
  184. var j, min, max;
  185. if (Validators.keyRange(val)) {
  186. if ($.isArray(val)) {
  187. min = val[0];
  188. max = val[1];
  189. } else {
  190. min = val.min;
  191. max = val.max;
  192. }
  193. for (j = min; j <= max; j += 1) {
  194. keys.push(j);
  195. }
  196. } else if (typeof val === 'number' && Validators.integer(val)) {
  197. keys.push(val);
  198. }
  199. });
  200. }
  201. return keys;
  202. };
  203. Handles = (function () {
  204. return {
  205. integer: function (elem, name, value) {
  206. var id = Combobox.getId(elem), opt = options[id];
  207. if (opt !== undefined && Validators.integer(value)) {
  208. opt[name] = value;
  209. return true;
  210. }
  211. return false;
  212. },
  213. bool: function (elem, name, value) {
  214. var id = Combobox.getId(elem), opt = options[id];
  215. if (opt !== undefined && typeof value === 'boolean') {
  216. opt[name] = value;
  217. return true;
  218. }
  219. return false;
  220. },
  221. array: function (elem, name, value) {
  222. if (typeof value === 'string') {
  223. value = [value];
  224. }
  225. var id = Combobox.getId(elem), opt = options[id];
  226. if (opt !== undefined && $.isArray(value)) {
  227. opt[name] = value;
  228. return true;
  229. }
  230. return false;
  231. },
  232. object: function (elem, name, value) {
  233. var id = Combobox.getId(elem), opt = options[id];
  234. if (opt !== undefined && value !== null &&
  235. typeof value === 'object' && !$.isArray(value)) {
  236. opt[name] = value;
  237. }
  238. },
  239. keys: function (elem, name, value) {
  240. var id = Combobox.getId(elem), opt = options[id];
  241. if (opt !== undefined && $.isArray(value)) {
  242. opt[name] = parseKeys(value);
  243. }
  244. }
  245. };
  246. }());
  247. return {
  248. position: function (elem, value) {
  249. if (Handles.integer(elem, 'position', value)) {
  250. var id = Combobox.getId(elem), opt = options[id], optionsCount;
  251. optionsCount =
  252. elem.find('option:not(.' + pluginClass + ')').length;
  253. if (value > optionsCount) {
  254. opt.position = optionsCount;
  255. }
  256. }
  257. },
  258. ignoreOptGroups: function (elem, value) {
  259. Handles.bool(elem, 'ignoreOptGroups', value);
  260. },
  261. maxLength: function (elem, value) {
  262. if (Handles.integer(elem, 'maxLength', value)) {
  263. var id = Combobox.getId(elem), opt = options[id];
  264. if (value < 0 || value > 255) {
  265. opt.maxLength = 255;
  266. }
  267. }
  268. },
  269. classes: function (elem, value) {
  270. Handles.array(elem, 'classes', value);
  271. },
  272. optionClasses: function (elem, value) {
  273. Handles.array(elem, 'optionClasses', value);
  274. },
  275. styles: function (elem, value) {
  276. Handles.object(elem, 'styles', value);
  277. },
  278. optionStyles: function (elem, value) {
  279. Handles.object(elem, 'optionStyles', value);
  280. },
  281. triggerChangeEvent: function (elem, value) {
  282. Handles.bool(elem, 'triggerChangeEvent', value);
  283. },
  284. focusOnNewOption: function (elem, value) {
  285. Handles.bool(elem, 'focusOnNewOption', value);
  286. },
  287. useExistingOptions: function (elem, value) {
  288. Handles.bool(elem, 'useExistingOptions', value);
  289. },
  290. blinkingCursor: function (elem, value) {
  291. Handles.bool(elem, 'blinkingCursor', value);
  292. },
  293. blinkingCursorInterval: function (elem, value) {
  294. Handles.integer(elem, 'blinkingCursorInterval', value);
  295. },
  296. ignoredKeys: function (elem, value) {
  297. Handles.keys(elem, 'ignoredKeys', value);
  298. },
  299. acceptedKeys: function (elem, value) {
  300. Handles.keys(elem, 'acceptedKeys', value);
  301. }
  302. };
  303. }());
  304. Remove = (function () {
  305. var removeClasses, removeStyles;
  306. removeClasses = function (elem, classes) {
  307. $.each(classes, function (i, val) {
  308. elem.removeClass(val);
  309. });
  310. };
  311. removeStyles = function (elem, styles) {
  312. $.each(styles, function (key, val) {
  313. elem.css(key, '');
  314. });
  315. };
  316. return {
  317. classes: function (elem) {
  318. var id = Combobox.getId(elem), opt = options[id];
  319. if (opt !== undefined) {
  320. removeClasses(elem, opt.classes);
  321. }
  322. },
  323. optionClasses: function (elem) {
  324. var id = Combobox.getId(elem), opt = options[id];
  325. if (opt !== undefined) {
  326. removeClasses(elem.find('option.' + pluginClass),
  327. opt.optionClasses);
  328. }
  329. },
  330. styles: function (elem) {
  331. var id = Combobox.getId(elem), opt = options[id];
  332. if (opt !== undefined) {
  333. removeStyles(elem, opt.styles);
  334. }
  335. },
  336. optionStyles: function (elem) {
  337. var id = Combobox.getId(elem), opt = options[id];
  338. if (opt !== undefined) {
  339. removeStyles(elem.find('option.' + pluginClass),
  340. opt.optionStyles);
  341. }
  342. },
  343. all: function (elem) {
  344. Remove.classes(elem);
  345. Remove.optionClasses(elem);
  346. Remove.styles(elem);
  347. Remove.optionStyles(elem);
  348. }
  349. };
  350. }());
  351. Handle = (function () {
  352. var setClasses, setStyles;
  353. setClasses = function (elem, classes) {
  354. $.each(classes, function (i, val) {
  355. elem.addClass(val);
  356. });
  357. };
  358. setStyles = function (elem, styles) {
  359. $.each(styles, function (key, val) {
  360. elem.css(key, val);
  361. });
  362. };
  363. return {
  364. position: function (elem) {
  365. var opt = options[Combobox.getId(elem)], option, uneditableOptions,
  366. container;
  367. option = elem.find('option.' + pluginClass);
  368. uneditableOptions = elem.find('option:not(.' + pluginClass + ')');
  369. if (opt.position < uneditableOptions.length) {
  370. container = uneditableOptions.eq(opt.position);
  371. if (!opt.ignoreOptGroups &&
  372. container.parent('optgroup').length > 0) {
  373. uneditableOptions.eq(opt.position).parent().before(option);
  374. } else {
  375. uneditableOptions.eq(opt.position).before(option);
  376. }
  377. } else {
  378. elem.append(option);
  379. }
  380. },
  381. classes: function (elem) {
  382. var id = Combobox.getId(elem), opt = options[id];
  383. if (opt !== undefined) {
  384. setClasses(elem, opt.classes);
  385. }
  386. },
  387. optionClasses: function (elem) {
  388. var id = Combobox.getId(elem), opt = options[id];
  389. if (opt !== undefined) {
  390. setClasses(elem.find('option.' + pluginClass),
  391. opt.optionClasses);
  392. }
  393. },
  394. styles: function (elem) {
  395. var id = Combobox.getId(elem), opt = options[id];
  396. if (opt !== undefined) {
  397. setStyles(elem, opt.styles);
  398. }
  399. },
  400. optionStyles: function (elem) {
  401. var id = Combobox.getId(elem), opt = options[id];
  402. if (opt !== undefined) {
  403. setStyles(elem.find('option.' + pluginClass),
  404. opt.optionStyles);
  405. }
  406. },
  407. focusOnNewOption: function (elem) {
  408. var id = Combobox.getId(elem), opt = options[id];
  409. if (opt !== undefined && opt.focusOnNewOption) {
  410. elem.find('option.' + pluginClass)
  411. .prop('selected', true);
  412. }
  413. },
  414. useExistingOptions: function (elem) {
  415. var id = Combobox.getId(elem), opt = options[id];
  416. if (opt !== undefined && opt.useExistingOptions) {
  417. Combobox.setEditableOption(elem);
  418. }
  419. },
  420. all: function (elem) {
  421. Handle.position(elem);
  422. Handle.classes(elem);
  423. Handle.optionClasses(elem);
  424. Handle.styles(elem);
  425. Handle.optionStyles(elem);
  426. Handle.focusOnNewOption(elem);
  427. Handle.useExistingOptions(elem);
  428. }
  429. };
  430. }());
  431. return {
  432. Set: Set,
  433. Remove: Remove,
  434. Handle: Handle
  435. };
  436. }());
  437. EditableOption = (function () {
  438. return {
  439. init: function (elem) {
  440. if (!elem.find('option.' + pluginClass).length) {
  441. var editableOption = $('<option>');
  442. editableOption.addClass(pluginClass);
  443. elem.append(editableOption);
  444. }
  445. elem.on('keydown', EventHandlers.keyDown);
  446. elem.on('keypress', EventHandlers.keyPress);
  447. elem.on('keyup', EventHandlers.keyUp);
  448. elem.on('change', EventHandlers.change);
  449. elem.on('focus', EventHandlers.focus);
  450. elem.on('blur', EventHandlers.blur);
  451. elem.on('click', EventHandlers.click);
  452. },
  453. destroy: function (elem) {
  454. elem.find('option.' + pluginClass).remove();
  455. elem.off('keydown', EventHandlers.keyDown);
  456. elem.off('keypress', EventHandlers.keyPress);
  457. elem.off('keyup', EventHandlers.keyUp);
  458. elem.off('change', EventHandlers.change);
  459. elem.off('focus', EventHandlers.focus);
  460. elem.off('blur', EventHandlers.blur);
  461. elem.off('click', EventHandlers.click);
  462. }
  463. };
  464. }());
  465. // generates unique identifier
  466. generateId = function () {
  467. while (true) {
  468. var random = Math.floor(Math.random() * 100000);
  469. if (options[random] === undefined) {
  470. return random;
  471. }
  472. }
  473. };
  474. // sets combobox
  475. setup = function (elem) {
  476. EditableOption.init(elem);
  477. Parameters.Handle.all(elem);
  478. };
  479. // Combobox public members
  480. return {
  481. // create editable combobox
  482. init: function (settings) {
  483. return $(this).filter(':uneditable').each(function () {
  484. var id = generateId(), elem = $(this);
  485. elem.data('jecId', id);
  486. // override passed default options
  487. options[id] = $.extend(true, {}, defaults);
  488. // parse keys
  489. Parameters.Set.ignoredKeys(elem, options[id].ignoredKeys);
  490. Parameters.Set.acceptedKeys(elem, options[id].acceptedKeys);
  491. if (typeof settings === 'object' && !$.isArray(settings)) {
  492. $.each(settings, function (key, val) {
  493. if (val !== undefined) {
  494. switch (key) {
  495. case 'position':
  496. Parameters.Set.position(elem, val);
  497. break;
  498. case 'ignoreOptGroups':
  499. Parameters.Set.ignoreOptGroups(elem, val);
  500. break;
  501. case 'maxLength':
  502. Parameters.Set.maxLength(elem, val);
  503. break;
  504. case 'classes':
  505. Parameters.Set.classes(elem, val);
  506. break;
  507. case 'optionClasses':
  508. Parameters.Set.optionClasses(elem, val);
  509. break;
  510. case 'styles':
  511. Parameters.Set.styles(elem, val);
  512. break;
  513. case 'optionStyles':
  514. Parameters.Set.optionStyles(elem, val);
  515. break;
  516. case 'triggerChangeEvent':
  517. Parameters.Set.triggerChangeEvent(elem, val);
  518. break;
  519. case 'focusOnNewOption':
  520. Parameters.Set.focusOnNewOption(elem, val);
  521. break;
  522. case 'useExistingOptions':
  523. Parameters.Set.useExistingOptions(elem, val);
  524. break;
  525. case 'blinkingCursor':
  526. Parameters.Set.blinkingCursor(elem, val);
  527. break;
  528. case 'blinkingCursorInterval':
  529. Parameters.Set.blinkingCursorInterval(elem, val);
  530. break;
  531. case 'ignoredKeys':
  532. Parameters.Set.ignoredKeys(elem, val);
  533. break;
  534. case 'acceptedKeys':
  535. Parameters.Set.acceptedKeys(elem, val);
  536. break;
  537. }
  538. }
  539. });
  540. }
  541. setup($(this));
  542. });
  543. },
  544. // creates editable combobox without using existing select elements
  545. initJS: function (options, settings) {
  546. var select, addOptions;
  547. select = $('<select>');
  548. addOptions = function (elem, options) {
  549. if ($.isArray(options)) {
  550. $.each(options, function (i, val) {
  551. if ($.isPlainObject(val)) {
  552. $.each(val, function (key, value) {
  553. if ($.isArray(value)) {
  554. var og = $('<optgroup>').attr('label', key);
  555. addOptions(og, value);
  556. og.appendTo(select);
  557. } else if (typeof value === 'number' ||
  558. typeof value === 'string') {
  559. $('<option>').text(value).prop('value', key)
  560. .appendTo(elem);
  561. }
  562. });
  563. } else if (typeof val === 'string' || typeof val === 'number') {
  564. $('<option>').text(val).prop('value', val).appendTo(elem);
  565. }
  566. });
  567. }
  568. };
  569. addOptions(select, options);
  570. return select.jec(settings);
  571. },
  572. // destroys editable combobox
  573. destroy: function () {
  574. return $(this).filter(':editable').each(function () {
  575. $(this).jecOff();
  576. $.removeData($(this).get(0), 'jecId');
  577. $.removeData($(this).get(0), 'jecCursorState');
  578. $.removeData($(this).get(0), 'jecOpenedState');
  579. });
  580. },
  581. // enable editablecombobox
  582. enable: function () {
  583. return $(this).filter(':editable').each(function () {
  584. var id = Combobox.getId($(this)), value = values[id];
  585. setup($(this));
  586. if (value !== undefined) {
  587. $(this).jecValue(value);
  588. }
  589. });
  590. },
  591. // disable editable combobox
  592. disable: function () {
  593. return $(this).filter(':editable').each(function () {
  594. var val = $(this).find('option.' + pluginClass).val();
  595. values[Combobox.getId($(this))] = val;
  596. Parameters.Remove.all($(this));
  597. EditableOption.destroy($(this));
  598. });
  599. },
  600. // gets or sets editable option's value
  601. value: function (value, setFocus) {
  602. if ($(this).filter(':editable').length > 0) {
  603. if (value === null || value === undefined) {
  604. // get value
  605. return $(this).find('option.' + pluginClass).val();
  606. } else if (typeof value === 'string' || typeof value === 'number') {
  607. // set value
  608. return $(this).filter(':editable').each(function () {
  609. var option = $(this).find('option.' + pluginClass);
  610. option.val(value).text(value);
  611. if (typeof setFocus !== 'boolean' || setFocus) {
  612. option.prop('selected', true);
  613. }
  614. });
  615. }
  616. }
  617. },
  618. // gets or sets editable option's preference
  619. pref: function (name, value) {
  620. if ($(this).filter(':editable').length > 0) {
  621. if (typeof name === 'string') {
  622. if (value === null || value === undefined) {
  623. // get preference
  624. return options[Combobox.getId($(this))][name];
  625. } else {
  626. // set preference
  627. return $(this).filter(':editable').each(function () {
  628. switch (name) {
  629. case 'position':
  630. Parameters.Set.position($(this), value);
  631. Parameters.Handle.position($(this));
  632. break;
  633. case 'classes':
  634. Parameters.Remove.classes($(this));
  635. Parameters.Set.classes($(this), value);
  636. Parameters.Handle.position($(this));
  637. break;
  638. case 'optionClasses':
  639. Parameters.Remove.optionClasses($(this));
  640. Parameters.Set.optionClasses($(this), value);
  641. Parameters.Set.optionClasses($(this));
  642. break;
  643. case 'styles':
  644. Parameters.Remove.styles($(this));
  645. Parameters.Set.styles($(this), value);
  646. Parameters.Set.styles($(this));
  647. break;
  648. case 'optionStyles':
  649. Parameters.Remove.optionStyles($(this));
  650. Parameters.Set.optionStyles($(this), value);
  651. Parameters.Handle.optionStyles($(this));
  652. break;
  653. case 'focusOnNewOption':
  654. Parameters.Set.focusOnNewOption($(this), value);
  655. Parameters.Handle.focusOnNewOption($(this));
  656. break;
  657. case 'useExistingOptions':
  658. Parameters.Set.useExistingOptions($(this), value);
  659. Parameters.Handle.useExistingOptions($(this));
  660. break;
  661. case 'blinkingCursor':
  662. Parameters.Set.blinkingCursor($(this), value);
  663. break;
  664. case 'blinkingCursorInterval':
  665. Parameters.Set.blinkingCursorInterval($(this), value);
  666. break;
  667. case 'ignoredKeys':
  668. Parameters.Set.ignoredKeys($(this), value);
  669. break;
  670. case 'acceptedKeys':
  671. Parameters.Set.acceptedKeys($(this), value);
  672. break;
  673. }
  674. });
  675. }
  676. }
  677. }
  678. },
  679. // sets editable option to the value of currently selected option
  680. setEditableOption: function (elem) {
  681. var value = elem.find('option:selected').text();
  682. elem.find('option.' + pluginClass).prop('value', elem.val())
  683. .text(value).prop('selected', true);
  684. },
  685. // get combobox id
  686. getId: function (elem) {
  687. return elem.data('jecId');
  688. },
  689. valueIsEditable: function (elem) {
  690. return elem.find('option.' + pluginClass).get(0) ===
  691. elem.find('option:selected').get(0);
  692. },
  693. clearCursor: function (elem) {
  694. $(elem).find('option.' + cursorClass).each(function () {
  695. var text = $(this).text();
  696. $(this).removeClass(cursorClass).text(text.substring(0, text.length - 1));
  697. });
  698. },
  699. cursorState: function (elem, state) {
  700. return elem.data('jecCursorState', state);
  701. },
  702. openedState: function (elem, state) {
  703. return elem.data('jecOpenedState', state);
  704. },
  705. //handles editable cursor
  706. handleCursor: function () {
  707. if (activeCombobox !== undefined && activeCombobox !== null) {
  708. if ($.browser.msie && Combobox.openedState(activeCombobox)) {
  709. return;
  710. }
  711. var state = Combobox.cursorState(activeCombobox), elem;
  712. if (state) {
  713. Combobox.clearCursor(activeCombobox);
  714. } else if (Combobox.valueIsEditable(activeCombobox)) {
  715. elem = activeCombobox.find('option:selected');
  716. elem.addClass(cursorClass).text(elem.text() + '|');
  717. }
  718. Combobox.cursorState(activeCombobox, !state);
  719. }
  720. }
  721. };
  722. }());
  723. // jEC public members
  724. return {
  725. init: Combobox.init,
  726. enable: Combobox.enable,
  727. disable: Combobox.disable,
  728. destroy: Combobox.destroy,
  729. value: Combobox.value,
  730. pref: Combobox.pref,
  731. initJS: Combobox.initJS,
  732. handleCursor: Combobox.handleCursor
  733. };
  734. }());
  735. // register functions
  736. $.fn.extend({
  737. jec: $.jEC.init,
  738. jecOn: $.jEC.enable,
  739. jecOff: $.jEC.disable,
  740. jecKill: $.jEC.destroy,
  741. jecValue: $.jEC.value,
  742. jecPref: $.jEC.pref
  743. });
  744. $.extend({
  745. jec: $.jEC.initJS
  746. });
  747. // register selectors
  748. $.extend($.expr[':'], {
  749. editable: function (a) {
  750. var data = $(a).data('jecId');
  751. return data !== null && data !== undefined;
  752. },
  753. uneditable: function (a) {
  754. var data = $(a).data('jecId');
  755. return data === null || data === undefined;
  756. }
  757. });
  758. }(jQuery));