Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // +---------------------------------------------------------------------------+
- // | This file is part of a Revolution Linux project. |
- // | Copyright (C) Revolution Linux |
- // | |
- // | For the full copyright and license information, please view the LICENSE |
- // | file that was distributed with this source code. |
- // +---------------------------------------------------------------------------+
- var RSuggestListBox = RSuggestTextBox.extend({
- /**
- * @var object The current listbox, select element.
- */
- select : null,
- /**
- * @var object The hidden field which contains the value.
- */
- hidden : null,
- /**
- * @var object The class options.
- */
- options : {
- // onMatch : $empty
- // onMatchs : $empty
- // onMissed : $empty
- button : null,
- },
- /**
- * Constructior. Set the listbox, select element which will be replaced
- * by the new listbox.
- * @param object The select object.
- * @param object The class options.
- * @return void
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- initialize : function(select, options) {
- this.values = [];
- this.select = $(select);
- this.setOptions(options);
- // create the hidden field which will contains the value
- this.hidden = new Element('input');
- this.hidden.setProperty('type', 'hidden');
- this.hidden.setProperty('name', this.select.name);
- this.hidden.setProperty('value', '');
- // create the textbox which will replace the select
- this.textbox = new Element('input');
- this.textbox.setProperty('type', 'text');
- this.textbox.setProperty('name', 'temp');
- this.textbox.setProperty('class', 'listbox');
- // replace the select with the textbox
- this.select.replaceWith(this.textbox);
- this.hidden.injectAfter(this.textbox);
- // load the values and sort them
- for (var i = 0; i < this.select.options.length; i++) {
- this.values.push({
- label : this.select.options[i].getText(),
- value : this.select.options[i].getProperty('value'),
- });
- }
- // create the listbox which will contains
- this.listbox = new RListBox();
- this.listbox.hide();
- this.listbox.injectAfter(this.textbox);
- this.listbox.addEvent('keyup', this.suggest.bind(this));
- this.listbox.addEvent('keydown', this.choose.bind(this));
- this.textbox.addEvent('keyup', this.suggest.bind(this));
- this.textbox.addEvent('keydown', this.choose.bind(this));
- },
- /**
- * Put a suggestion into the textbox. This method will be automatically
- * called by the textbox keyup event.
- * @param object The event object.
- * @return void
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- suggest : function(event) {
- var len = this.textbox.value.length;
- if (len > 0) {
- // filter the correct keys
- if ((event.keyCode >= this.KEY_0 && event.keyCode <= this.KEY_9) ||
- (event.keyCode >= this.KEY_A && event.keyCode <= this.KEY_Z) ||
- (event.keyCode == this.KEY_BACKSPACE)) {
- // look for keys from 0 to 9 and a to z. only then we will try
- // to find a suggestion for the current input. we make sure the
- // list gets updated when the user hit teh backspace key
- this.listbox.hide();
- this.listbox.clear();
- var results = this.getSuggestions();
- // pick up the suggestions and add each one into the list
- if (results.length > 0) {
- for (var i = 0; i < results.length; i++) {
- // add an event so the user will be able to make a
- // selection by clicking on an item.
- this.listbox.addItem(results[i].label, results[i].value, this.listbox).addEvent('click', function() {
- var item = this.listbox.getSelectedItem();
- this.textbox.value = item.getLabel();
- this.hidden.value = item.getValue();
- this.listbox.unselectAll();
- this.listbox.hide();
- }.bind(this));
- }
- // select the first item in the list and display it
- this.listbox.select(0);
- this.listbox.show('block');
- }
- } else {
- // manually handle all the other keys
- switch (event.keyCode) {
- case 13 :
- // get the current item and set the textbox and hidden
- // field content that match the choosed item
- var item = this.listbox.getSelectedItem();
- this.textbox.value = item.getLabel();
- this.hidden.value = item.getValue();
- this.listbox.unselectAll();
- this.listbox.hide();
- break;
- }
- }
- this.parent(event);
- } else {
- this.listbox.hide();
- this.listbox.clear();
- }
- },
- /**
- *
- * @return string A single suggestion.
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- choose : function(event) {
- switch (event.keyCode) {
- case 38 : this.listbox.selectPrev(); break;
- case 40 : this.listbox.selectNext(); break;
- }
- },
- /**
- * Return a single suggestion string from the array of possible values.
- * @return string A single suggestion.
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- getSuggestion : function() {
- var value = this.textbox.value.toLowerCase();
- if (value.length) {
- this.values.each(function(elem) {
- if (elem.label.toLowerCase().indexOf(value) > -1) return elem;
- });
- }
- return null;
- },
- /**
- * Return an array of suggestions that match the current input.
- * @return array An array of suggestions.
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- getSuggestions : function() {
- console.log('NEW METHOD');
- var items = [];
- var value = this.textbox.value.toLowerCase();
- if (value.length) {
- this.values.each(function(elem) {
- if (elem.label.toLowerCase().indexOf(value) > -1) items.push(elem);
- });
- }
- return items;
- },
- /**
- * Insert a suggestion in the textbox.
- * @param string The suggestion.
- * @return void
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- setSuggestion : function(item) {
- var len = this.textbox.value.length;
- this.hidden.value = item.value;
- this.textbox.value = item.label;
- this.selectRange(len, item.label.length);
- }
- });
- // +---------------------------------------------------------------------------+
- // | This file is part of a Revolution Linux project. |
- // | Copyright (C) Revolution Linux |
- // | |
- // | For the full copyright and license information, please view the LICENSE |
- // | file that was distributed with this source code. |
- // +---------------------------------------------------------------------------+
- /**
- * RSuggestTextBox is a class which will put suggestions inside a textbox based
- * on the user input. The array of possibilities will be stored inside the object.
- * @package core
- * @subpackage form
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @copyright Revolution Linux
- * @since 1.0.0
- * @version 1.0.0
- */
- var RSuggestTextBox = RPlugin.extend({
- /**
- * @var int Some key constats.
- */
- KEY_0 : 48, KEY_9 : 57, KEY_A : 65, KEY_Z : 90, KEY_BACKSPACE : 9, KEY_ENTER : 13,
- /**
- * @var object The textbox.
- */
- textbox : null,
- /**
- * @var array The possible values.
- */
- values : null,
- /**
- * @var object The class options.
- */
- options : {
- // onMatch : $empty
- // onMatchs : $empty
- // onMissed : $empty
- },
- /**
- * Constructor. Set the textbox and the values which will be used to make
- * a suggestion based on the textbox value.
- * @param object The textbox.
- * @param array The possible values.
- * @return void
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- initialize : function(textbox, values, options) {
- this.values = values.sort();
- this.textbox = $(textbox);
- this.textbox.addEvent('keyup', this.suggest.bind(this));
- this.setOptions(options);
- },
- /**
- * Put a suggestion into the textbox. This method will be automatically
- * called by the textbox keyup event.
- * @param object The event object.
- * @return void
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- suggest : function(event) {
- var len = this.textbox.value.length;
- if (len > 0) {
- // look for keys from 0 to 9 and a to z. only then we will try
- // to find a suggestion for the current input
- if ((event.keyCode >= this.KEY_0 && event.keyCode <= this.KEY_9) ||
- (event.keyCode >= this.KEY_A && event.keyCode <= this.KEY_Z) ||
- (event.keyCode == this.KEY_BACKSPACE)) {
- var results = this.getSuggestions();
- switch (results.length) {
- case 0 : this.fireEvent('onMissed'); break;
- case 1 : this.fireEvent('onMatch'); break;
- default : this.fireEvent('onMatchs'); break;
- }
- if (results.length) {
- // place the suggestion inside the textbox
- this.setSuggestion(results[0]);
- }
- } else {
- switch (event.keyCode) {
- case 13 :
- // enter, fill the current selection
- this.selectRange(len, len);
- return;
- }
- }
- }
- },
- /**
- * Return a single suggestion string from the array of possible values.
- * @return string A single suggestion.
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- getSuggestion : function() {
- var value = this.textbox.value.toLowerCase();
- if (value.length) {
- this.values.each(function(elem) {
- if (elem.toLowerCase().indexOf(value) > -1) return elem;
- });
- }
- return null;
- },
- /**
- * Return an array of suggestions that match the current input.
- * @return array An array of suggestions.
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- getSuggestions : function() {
- console.log('OLD METHOD');
- var items = [];
- var value = this.textbox.value.toLowerCase();
- if (value.length) {
- this.values.each(function(elem) {
- if (elem.toLowerCase().indexOf(value) > -1) items.push(elem);
- });
- }
- return items;
- },
- /**
- * Insert a suggestion in the textbox.
- * @param string The suggestion.
- * @return void
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- setSuggestion : function(suggestion) {
- var len = this.textbox.value.length;
- this.textbox.value = suggestion;
- this.selectRange(len, suggestion.length);
- },
- /**
- * Create a selected range in the current textbox.
- * @param int The start index.
- * @param in The range length.
- * @return void
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- selectRange : function(start, length) {
- if (this.textbox.createTextRange) {
- // create a range in internet explorer
- var r = this.textbox.createTextRange();
- r.moveStart('character', start);
- r.moveEnd('character', length - this.textbox.value.length);
- } else if (this.textbox.setSelectionRange) {
- // create a range in firefox
- this.textbox.setSelectionRange(start, length);
- }
- this.textbox.focus();
- }
- });
Add Comment
Please, Sign In to add comment