1 /* 2 Copyright (c) 2011, Alexandru Bularca <alexandru.bularca@1and1.ro> 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions are met: 7 * Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 * Redistributions in binary form must reproduce the above copyright 10 notice, this list of conditions and the following disclaimer in the 11 documentation and/or other materials provided with the distribution. 12 * Neither the name of the <organization> nor the 13 names of its contributors may be used to endorse or promote products 14 derived from this software without specific prior written permission. 15 16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY 20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 define(['core-components/lib/amplify.store'], function (driver) { 29 var storageTypes = { 30 'persistent': [ 31 'localStorage', 32 'globalStorage', 33 'userData' 34 ], 35 transient: [ 36 'sessionStorage', 37 'memory' 38 ] 39 }; 40 41 function getStorage(type) { 42 var type = (type) ? 'transient' : 'persistent'; 43 44 for (var i in storageTypes[type]) { 45 var storage = storageTypes[type][i]; 46 47 if(storage in driver.store.types) { 48 return driver.store.types[storage]; 49 } 50 } 51 52 return driver.store; 53 } 54 55 function ClientStorage (viewContext) { 56 this.context = viewContext.instanceId; 57 } 58 59 /** 60 * Set the value of key (add it if key doesn't exist) into storage 61 * 62 * @param {String} key 63 * @param {Object} value 64 * @param {Boolean} isTransient whether to use persistent storage or transient storage (defaults to false) 65 * @throws {Error} if client storage is not supported 66 */ 67 ClientStorage.prototype.set = function (key, value, isTransient) { 68 var storage = getStorage(isTransient); 69 70 storage(key, value, {expires: false}); 71 }; 72 73 /** 74 * Retrieves the value of key from storage 75 * @param {String} key 76 * @param {Boolean} isTransient whether to use persistent storage or transient storage (defaults to false) 77 * @returns {String|Boolean} the value of key or null on failure 78 * @throws {Error} if client storage is not supported 79 */ 80 ClientStorage.prototype.get = function (key, isTransient) { 81 var storage = getStorage(isTransient); 82 83 value = storage(key); 84 85 return value; 86 }; 87 88 /** 89 * Remove the key from storage 90 * 91 * @param {String} key 92 * @param {Boolean} isTransient whether to use persistent storage or transient storage (defaults to false) 93 * @throws {Error} if client storage is not supported 94 */ 95 ClientStorage.prototype.remove = function (key, isTransient) { 96 var storage = getStorage(isTransient); 97 98 storage(key, null); 99 }; 100 101 ClientStorage.prototype.addListener = function (callback) { 102 // TODO: code here later 103 }; 104 105 ClientStorage.prototype.removeListener = function (callback) { 106 // TODO: code here 107 }; 108 109 return ClientStorage; 110 }); 111