⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selectbox.js

📁 Javascript code for developing the virtual keyboard
💻 JS
📖 第 1 页 / 共 3 页
字号:
   *  Removes selection from all available options
   *
   *  @scope public
   */
  this.unselectAllOptions = function () /* :void */ {
    for (var i=0, nL=node.options.length; i<nL; i++) {
//         node.options[i].selected = false;
         options[node.options[i].__idx].selected = false;
    }
    node.selectedIndex = -1;
  }
  /**
   *  Selects all matching options
   *
   *  @param {String} needle search phrase
   *  @param {String} match match needle from: 'start', 'end', 'exact', 'any'. Default is 'start'. Has no meaning, when regexp is supplied.
   *  @param {String} attr attribute to do the check against, either 'text' or 'value'
   *  @return {Number} number of unselected options 
   *  @scope public
   */
  this.unselectMatchingOptions = function (needle /* :String */, match /* :String */, attr /* :String */) /* :Number */ {
    return __setOptionsState (needle, 'selected', false, true, match, attr);
  }
  /**
   *  Removes selection from all matching options and set for others
   *
   *  @param {String} needle search phrase
   *  @param {String} match match needle from: 'start', 'end', 'exact', 'any'. Default is 'start'. Has no meaning, when regexp is supplied.
   *  @param {String} attr attribute to do the check against, either 'text' or 'value'
   *  @return {Number} number of selected options 
   *  @scope public
   */
  this.unselectOnlyMatchingOptions = function (needle /* :String */, match /* :String */, attr /* :String */) /* :Number */ {
    return __setOptionsState (needle, 'selected', false, false, match, attr);
  }
  //-------------------------------------------
  //  FIXATION OPERATION
  //-------------------------------------------
  /**
   *  Fix positions of all matching options
   *
   *  @param {String} needle search phrase
   *  @param {String} match match needle from: 'start', 'end', 'exact', 'any'. Default is 'start'. Has no meaning, when regexp is supplied.
   *  @param {String} attr attribute to do the check against, either 'text' or 'value'
   *  @return {Number} number of fixed options 
   *  @scope public
   */
  this.fixMatchingOptions = function (needle /* :String */, match /* :String */, attr /* :String */) /* :Number */ {
    return __setOptionsState (needle, 'fixed', true, true, match, attr);
  }
  /**
   *  Fix positions of all matching options and reset selection for others
   *
   *  @param {String} needle search phrase
   *  @param {String} match match needle from: 'start', 'end', 'exact', 'any'. Default is 'start'. Has no meaning, when regexp is supplied.
   *  @param {String} attr attribute to do the check against, either 'text' or 'value'
   *  @return {Number} number of fixed options 
   *  @scope public
   */
  this.fixOnlyMatchingOptions = function (needle /* :String */, match /* :String */, attr /* :String */) /* :Number */ {
    return __setOptionsState (needle, 'fixed', true, false, match, attr);
  }
  /**
   *  Unfixes positions of all matching options
   *
   *  @param {String} needle search phrase
   *  @param {String} match match needle from: 'start', 'end', 'exact', 'any'. Default is 'start'. Has no meaning, when regexp is supplied.
   *  @param {String} attr attribute to do the check against, either 'text' or 'value'
   *  @return {Number} number of unfixed options 
   *  @scope public
   */
  this.unfixMatchingOptions = function (needle /* :String */, match /* :String */, attr /* :String */) /* :Number */ {
    return __setOptionsState (needle, 'fixed', false, true, match, attr);
  }
  /**
   *  Unfixes positions of all matching options and set for others
   *
   *  @param {String} needle search phrase
   *  @param {String} match match needle from: 'start', 'end', 'exact', 'any'. Default is 'start'. Has no meaning, when regexp is supplied.
   *  @param {String} attr attribute to do the check against, either 'text' or 'value'
   *  @return {Number} number of unfixed options 
   *  @scope public
   */
  this.unfixOnlyMatchingOptions = function (needle /* :String */, match /* :String */, attr /* :String */) /* :Number */ {
    return __setOptionsState (needle, 'fixed', false, false, match, attr);
  }
  //-------------------------------------------
  //  VISIBILITY OPERATION
  //-------------------------------------------
  /**
   *  Shows all matching options
   *
   *  @param {String} needle search phrase
   *  @param {String} match match needle from: 'start', 'end', 'exact', 'any'. Default is 'start'. Has no meaning, when regexp is supplied.
   *  @param {String} attr attribute to do the check against, either 'text' or 'value'
   *  @return {Number} number of shown options 
   *  @scope public
   */
  this.showMatchingOptions = function (needle /* :String */, match /* :String */, attr /* :String */) /* :Number */ {
    return __setOptionsState (needle, 'visible', true, true, match, attr);
  }
  /**
   *  Shows all matching options and hide others
   *
   *  @param {String} needle search phrase
   *  @param {String} match match needle from: 'start', 'end', 'exact', 'any'. Default is 'start'. Has no meaning, when regexp is supplied.
   *  @param {String} attr attribute to do the check against, either 'text' or 'value'
   *  @return {Number} number of shown options 
   *  @scope public
   */
  this.showOnlyMatchingOptions = function (needle /* :String */, match /* :String */, attr /* :String */) /* :Number */ {
    return __setOptionsState (needle, 'visible', true, false, match, attr);
  }
  /**
   *  Hides all matching options
   *
   *  @param {String} needle search phrase
   *  @param {String} match match needle from: 'start', 'end', 'exact', 'any'. Default is 'start'. Has no meaning, when regexp is supplied.
   *  @param {String} attr attribute to do the check against, either 'text' or 'value'
   *  @return {Number} number of hidden options 
   *  @scope public
   */
  this.hideMatchingOptions = function (needle /* :String */, match /* :String */, attr /* :String */) /* :Number */ {
    return __setOptionsState (needle, 'visible', false, true, match, attr);
  }
  /**
   *  Hides all matching options and show others
   *
   *  @param {String} needle search phrase
   *  @param {String} match match needle from: 'start', 'end', 'exact', 'any'. Default is 'start'. Has no meaning, when regexp is supplied.
   *  @param {String} attr attribute to do the check against, either 'text' or 'value'
   *  @return {Number} number of hidden options 
   *  @scope public
   */
  this.hideOnlyMatchingOptions = function (needle /* :String */, match /* :String */, attr /* :String */) /* :Number */ {
    return __setOptionsState (needle, 'visible', false, false, match, attr);
  }
  //-------------------------------------------
  //  MISC FUNCTIONS
  //-------------------------------------------
  /**
   *  Sorts the options
   *
   *  @scope public
   */
  this.sort = function ()/* :void */{
    options = options.sort();
    this.updateOptions();
  }
  //-------------------------------------------
  //  GETTERS
  //-------------------------------------------
  /**
   *  Returns current selectbox value
   * 
   *  @return {String}
   *  @scope public 
   */
  this.getValue = function () {
      return node.value;
  }
  /**
   *  Return visible option or its property, if exists
   * 
   *  @param {Number} id option id
   *  @param {Object} p optional property name
   *  @scope public 
   */
  this.getOption = function(id /* :Number */, p /* :String */) /* :String,Array */ {
      if (!isNumeric(id) || id<0 || !node.options[id]) return "";
      return isString(p)?node.options[id][p]:[text.value,node.options[id].value];
  }
  /**
   *  Returns a list of the selected options
   * 
   *  @return {Array}
   *  @scope public
   */
  this.getSelectedOptions = function () /* :Array */ {
      var res = [];
      for (var i=0, oL=node.options.length; i<oL; i++) {
          if (node.options[i].selected) {
              res[res.length] = [node.options[i].text, node.options[i].value];
          }
      }
      return res;      
  }
  /**
   *  Returns the number of options in the box
   * 
   *  @return {Number}
   *  @scope public 
   */
  this.getOptionsLength = function () /* :Number */ {
      return node.options.length; 
  }
  /**
   *  Returns the selected index
   * 
   *  @return {Number}
   *  @scope public
   */
  this.getSelectedIndex = function () /* :Number */ {
      return node.selectedIndex;
  }  
  /**
   *  Returns ID of the element, it is bound to
   * 
   *  @return {String} 
   *  @scope public
   */
  this.getId = function () /* :String */ {
      return id;
  }
  /**
   *  Returns the element, it is bound to
   * 
   *  @return {HTMLSelectElement}
   *  @scope public
   */
  this.getEl = function () /* :HTMLSelectElement */ {
      return node;
  }
  /**
   *  Returns arary of options title text
   * 
   *  @param {Array} ids optional list of ids to get names of  
   *  @return {Array} list of options names
   *  @scope public
   */
  this.getOptionsNames = function (ids /* :Array */) /* :Array */ {
      if (ids) ids = new RegExp("^("+RegExp.escape(ids)+")$","i");
      var tmp = [];
      for (var i=0, oL=options.length; i<oL; i++) {
          if (isUndefined(ids) || (isRegExp(ids) && ids.test(options[i].value))) {
              tmp[tmp.length] = options[i].text;
          }
      }
      return tmp;
  }
  /**
   * Returns requested options track
   * 
   * @param {String} type one of 'all', 'added', 'deleted' 
   * @param {String} part optional 'text' or 'value'
   * @param {String} j optional list joiner, has no meaning if 'part' is not specified
   * @return {String} joined options track
   * @scope public
   */
  this.getOptionsTrack = function ( type /* :String */
                                   ,part /* :String */
                                   ,j /* :String */) /* :String */{
    var tmp = [];
    if ('text' != part && 'value' != part) part = false;
    if ('all' == String(type).toLowerCase()) {
        for (var i=0, tL=options.length; i<tL; i++) {
            tmp[tmp.length] = part?options[i][part]:options[i];
        }
    } else if (trackers[String(type).toLowerCase()]) {
        for (var i=0, tL=trackers[type].length; i<tL; i++) {
            tmp[tmp.length] = part?trackers[type][i][part]:trackers[type][i]; 
        }
    }
    return j&&part?tmp.join(j):tmp;
  }
  //-------------------------------------------
  //  OPTIONS MANIPULATION
  //-------------------------------------------
  /**
   *  Removes all matching options, preserving the selection
   *
   *  @param {String} needle search phrase
   *  @param {String} match match needle from: 'start', 'end', 'exact', 'any'. Default is 'start'. Has no meaning, when regexp is supplied.
   *  @param {String} attr attribute to do the check against, either 'text' or 'value'
   *  @scope public
   */
  this.removeMatchingOptions = function (needle /* :String */, match /* :String */, attr /* :String */) {
      var tmp = self.getSelectedOptions().flatten([0]);
      self.selectOnlyMatchingOptions(needle, match, attr);
      self.removeSelectedOptions();
      if (tmp.length>0) self.selectOnlyMatchingOptions(tmp, 'exact');
  }
  /**
   *  Removes all selected options from the list
   *
   *  @return {Array} list of deleted options
   *  @scope public
   */
  this.removeSelectedOptions = function () /* :Array */{
    var res = [], tmp;
    for (var i=(options.length-1); i>=0; i--) { 
      if (options[i].selected && (tmp = self.removeOption (i, false)))
          res[res.length] = tmp;
    }
    if (res) this.updateOptions();
    node.selectedIndex = -1;
    return res;
  }
  /**
   *  Removes all the options
   *
   *  @return {Array} list of deleted options
   *  @scope public
   */
  this.removeAllOptions = function () /* :Array */{
    var res = [];  
    for (var i=(options.length-1); i>=0; i--) { 
        __saveOptionsTrack ('deleted', options[i].text, options[i].value);
        res[res.length] = options[i];
    }
    node.selectedIndex = -1;
    node.options.length = options.length = 0;
    return res;
  }
  /**
   *  Removes the option with specified index
   *
   *  @param {Number} idx option index
   *  @param {Boolean} update after removal
   *  @return {Boolean} removal state
   *  @scope public
   */
  this.removeOption = function ( idx /* :Number */
                                ,update /* :Boolean */) /* :Boolean */{
    if (!options[idx]) return false;
    /*
    * update the track 
    */
    __saveOptionsTrack ('deleted', options[idx].text, options[idx].value);
    /*
    * and delete 
    */
    var res = options[idx]; 
    options.splice(idx,1);
    if (false !== update) this.updateOptions();
    return res;
  }
  /**
   *  Adds options to the list
   *

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -