1 /*
  2  * JBoss, Home of Professional Open Source
  3  * Copyright 2013, Red Hat, Inc. and individual contributors
  4  * by the @authors tag. See the copyright.txt in the distribution for a
  5  * full listing of individual contributors.
  6  *
  7  * This is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU Lesser General Public License as
  9  * published by the Free Software Foundation; either version 2.1 of
 10  * the License, or (at your option) any later version.
 11  *
 12  * This software is distributed in the hope that it will be useful,
 13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15  * Lesser General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Lesser General Public
 18  * License along with this software; if not, write to the Free
 19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21  */
 22 
 23 // RichFaces.utils
 24 // RichFaces.utils.cache
 25 (function ($, rf) {
 26     rf.utils = rf.utils || {};
 27 
 28     rf.utils.Cache = function (key, items, values, useCache) {
 29         this.key = key.toLowerCase();
 30         this.cache = {};
 31         this.cache[this.key] = items || [];
 32         this.originalValues = typeof values == "function" ? values(items) : values || this.cache[this.key];
 33         this.values = processValues(this.originalValues);
 34         this.useCache = useCache || checkValuesPrefix.call(this);
 35     };
 36 
 37     var processValues = function (values) {
 38         var processedValues = [];
 39         for (var i = 0; i < values.length; i++) {
 40             processedValues.push(values[i].toLowerCase());
 41         }
 42         return processedValues;
 43     };
 44 
 45     var checkValuesPrefix = function () {
 46         var result = true;
 47         for (var i = 0; i < this.values.length; i++) {
 48             if (this.values[i].indexOf(this.key) != 0) {
 49                 result = false;
 50                 break;
 51             }
 52         }
 53         return result;
 54     };
 55 
 56     var getItems = function (key, filterFunction) {
 57         key = key.toLowerCase();
 58         var newCache = [];
 59 
 60         if (key.length < this.key.length) {
 61             return newCache;
 62         }
 63 
 64         if (this.cache[key]) {
 65             newCache = this.cache[key];
 66         } else {
 67             var useCustomFilterFunction = typeof filterFunction == "function";
 68             var itemsCache = this.cache[this.key];
 69             for (var i = 0; i < this.values.length; i++) {
 70                 var value = this.values[i];
 71                 if (useCustomFilterFunction && filterFunction(key, value)) {
 72                     newCache.push(itemsCache[i]);
 73                 } else {
 74                     var p = value.indexOf(key);
 75                     if (p == 0) {
 76                         newCache.push(itemsCache[i]);
 77                     }
 78                 }
 79             }
 80 
 81             if ((!this.lastKey || key.indexOf(this.lastKey) != 0) && newCache.length > 0) {
 82                 this.cache[key] = newCache;
 83                 if (newCache.length == 1) {
 84                     this.lastKey = key;
 85                 }
 86             }
 87         }
 88 
 89         return newCache;
 90     };
 91 
 92     var getItemValue = function (item) {
 93         return this.originalValues[this.cache[this.key].index(item)];
 94     };
 95 
 96     var isCached = function (key) {
 97         key = key.toLowerCase();
 98         return this.cache[key] || this.useCache && key.indexOf(this.key) == 0;
 99     };
100 
101     $.extend(rf.utils.Cache.prototype, (function () {
102         return  {
103             getItems: getItems,
104             getItemValue: getItemValue,
105             isCached: isCached
106         };
107     })());
108 
109 })(RichFaces.jQuery, RichFaces);