Skip to content

Commit b24f43f

Browse files
author
Hsiaoming Yang
committed
add store
1 parent 100e215 commit b24f43f

File tree

5 files changed

+264
-0
lines changed

5 files changed

+264
-0
lines changed

store/dist/store-debug.js

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
define("gallery/store/1.3.5/store-debug", [], function(require, exports, module) {
2+
3+
/* Copyright (c) 2010-2012 Marcus Westin
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
;(function(){
25+
var store = {},
26+
win = window,
27+
doc = win.document,
28+
localStorageName = 'localStorage',
29+
globalStorageName = 'globalStorage',
30+
namespace = '__storejs__',
31+
storage
32+
33+
store.disabled = false
34+
store.set = function(key, value) {}
35+
store.get = function(key) {}
36+
store.remove = function(key) {}
37+
store.clear = function() {}
38+
store.transact = function(key, defaultVal, transactionFn) {
39+
var val = store.get(key)
40+
if (transactionFn == null) {
41+
transactionFn = defaultVal
42+
defaultVal = null
43+
}
44+
if (typeof val == 'undefined') { val = defaultVal || {} }
45+
transactionFn(val)
46+
store.set(key, val)
47+
}
48+
store.getAll = function() {}
49+
50+
store.serialize = function(value) {
51+
return JSON.stringify(value)
52+
}
53+
store.deserialize = function(value) {
54+
if (typeof value != 'string') { return undefined }
55+
try { return JSON.parse(value) }
56+
catch(e) { return value || undefined }
57+
}
58+
59+
// Functions to encapsulate questionable FireFox 3.6.13 behavior
60+
// when about.config::dom.storage.enabled === false
61+
// See https://github.com/marcuswestin/store.js/issues#issue/13
62+
function isLocalStorageNameSupported() {
63+
try { return (localStorageName in win && win[localStorageName]) }
64+
catch(err) { return false }
65+
}
66+
67+
function isGlobalStorageNameSupported() {
68+
try { return (globalStorageName in win && win[globalStorageName] && win[globalStorageName][win.location.hostname]) }
69+
catch(err) { return false }
70+
}
71+
72+
if (isLocalStorageNameSupported()) {
73+
storage = win[localStorageName]
74+
store.set = function(key, val) {
75+
if (val === undefined) { return store.remove(key) }
76+
storage.setItem(key, store.serialize(val))
77+
return val
78+
}
79+
store.get = function(key) { return store.deserialize(storage.getItem(key)) }
80+
store.remove = function(key) { storage.removeItem(key) }
81+
store.clear = function() { storage.clear() }
82+
store.getAll = function() {
83+
var ret = {}
84+
for (var i=0; i<storage.length; ++i) {
85+
var key = storage.key(i)
86+
ret[key] = store.get(key)
87+
}
88+
return ret
89+
}
90+
} else if (isGlobalStorageNameSupported()) {
91+
storage = win[globalStorageName][win.location.hostname]
92+
store.set = function(key, val) {
93+
if (val === undefined) { return store.remove(key) }
94+
storage[key] = store.serialize(val)
95+
return val
96+
}
97+
store.get = function(key) { return store.deserialize(storage[key] && storage[key].value) }
98+
store.remove = function(key) { delete storage[key] }
99+
store.clear = function() { for (var key in storage ) { delete storage[key] } }
100+
store.getAll = function() {
101+
var ret = {}
102+
for (var i=0; i<storage.length; ++i) {
103+
var key = storage.key(i)
104+
ret[key] = store.get(key)
105+
}
106+
return ret
107+
}
108+
109+
} else if (doc.documentElement.addBehavior) {
110+
var storageOwner,
111+
storageContainer
112+
// Since #userData storage applies only to specific paths, we need to
113+
// somehow link our data to a specific path. We choose /favicon.ico
114+
// as a pretty safe option, since all browsers already make a request to
115+
// this URL anyway and being a 404 will not hurt us here. We wrap an
116+
// iframe pointing to the favicon in an ActiveXObject(htmlfile) object
117+
// (see: http://msdn.microsoft.com/en-us/library/aa752574(v=VS.85).aspx)
118+
// since the iframe access rules appear to allow direct access and
119+
// manipulation of the document element, even for a 404 page. This
120+
// document can be used instead of the current document (which would
121+
// have been limited to the current path) to perform #userData storage.
122+
try {
123+
storageContainer = new ActiveXObject('htmlfile')
124+
storageContainer.open()
125+
storageContainer.write('<s' + 'cript>document.w=window</s' + 'cript><iframe src="/favicon.ico"></frame>')
126+
storageContainer.close()
127+
storageOwner = storageContainer.w.frames[0].document
128+
storage = storageOwner.createElement('div')
129+
} catch(e) {
130+
// somehow ActiveXObject instantiation failed (perhaps some special
131+
// security settings or otherwse), fall back to per-path storage
132+
storage = doc.createElement('div')
133+
storageOwner = doc.body
134+
}
135+
function withIEStorage(storeFunction) {
136+
return function() {
137+
var args = Array.prototype.slice.call(arguments, 0)
138+
args.unshift(storage)
139+
// See http://msdn.microsoft.com/en-us/library/ms531081(v=VS.85).aspx
140+
// and http://msdn.microsoft.com/en-us/library/ms531424(v=VS.85).aspx
141+
storageOwner.appendChild(storage)
142+
storage.addBehavior('#default#userData')
143+
storage.load(localStorageName)
144+
var result = storeFunction.apply(store, args)
145+
storageOwner.removeChild(storage)
146+
return result
147+
}
148+
}
149+
150+
// In IE7, keys may not contain special chars. See all of https://github.com/marcuswestin/store.js/issues/40
151+
var forbiddenCharsRegex = new RegExp("[!\"#$%&'()*+,/\\\\:;<=>?@[\\]^`{|}~]", "g")
152+
function ieKeyFix(key) {
153+
return key.replace(forbiddenCharsRegex, '___')
154+
}
155+
store.set = withIEStorage(function(storage, key, val) {
156+
key = ieKeyFix(key)
157+
if (val === undefined) { return store.remove(key) }
158+
storage.setAttribute(key, store.serialize(val))
159+
storage.save(localStorageName)
160+
return val
161+
})
162+
store.get = withIEStorage(function(storage, key) {
163+
key = ieKeyFix(key)
164+
return store.deserialize(storage.getAttribute(key))
165+
})
166+
store.remove = withIEStorage(function(storage, key) {
167+
key = ieKeyFix(key)
168+
storage.removeAttribute(key)
169+
storage.save(localStorageName)
170+
})
171+
store.clear = withIEStorage(function(storage) {
172+
var attributes = storage.XMLDocument.documentElement.attributes
173+
storage.load(localStorageName)
174+
for (var i=0, attr; attr=attributes[i]; i++) {
175+
storage.removeAttribute(attr.name)
176+
}
177+
storage.save(localStorageName)
178+
})
179+
store.getAll = withIEStorage(function(storage) {
180+
var attributes = storage.XMLDocument.documentElement.attributes
181+
storage.load(localStorageName)
182+
var ret = {}
183+
for (var i=0, attr; attr=attributes[i]; ++i) {
184+
ret[attr] = store.get(attr)
185+
}
186+
return ret
187+
})
188+
}
189+
190+
try {
191+
store.set(namespace, namespace)
192+
if (store.get(namespace) != namespace) { store.disabled = true }
193+
store.remove(namespace)
194+
} catch(e) {
195+
store.disabled = true
196+
}
197+
store.enabled = !store.disabled
198+
199+
if (typeof module != 'undefined' && typeof module != 'function') { module.exports = store }
200+
else if (typeof define === 'function' && define.amd) { define(store) }
201+
else { this.store = store }
202+
})();
203+
204+
205+
});

store/dist/store.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

store/package.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "store",
3+
"root": "gallery",
4+
"version": "1.3.5",
5+
"package": "https://raw.github.com/marcuswestin/store.js/master/package.json",
6+
"description": "A localStorage wrapper for all browsers without using cookies or flash. Uses localStorage, globalStorage, and userData behavior under the hood",
7+
"homepage": "https://github.com/marcuswestin/store.js",
8+
"author": "Marcus Westin <narcvs@gmail.com> (http://marcuswest.in)",
9+
"contributors": [
10+
"Matt Pizzimenti <mjpizz+github@gmail.com> (http://mjpizz.com)",
11+
"Long Ouyang (https://github.com/longouyang)",
12+
"Paul Irish (http://paulirish.com)",
13+
"Guillermo Rauch <rauchg@gmail.com> (https://github.com/guille)",
14+
"whitmer (https://github.com/whitmer)",
15+
"Steven Black <steveb@stevenblack.com> (https://github.com/StevenBlack)",
16+
"Marcus Tucker <info@marcustucker.com> (https://github.com/MarcusJT)",
17+
"Leonid Bugaev <leonsbox@gmail.com> (https://github.com/buger)",
18+
"Per Eckerdal <per.eckerdal@gmail.com> (https://github.com/pereckerdal)",
19+
"Fredrik Blomqvist (https://github.com/blq)"
20+
],
21+
"repository": {
22+
"type": "git",
23+
"url": "https://github.com/marcuswestin/store.js"
24+
},
25+
"bugs": {
26+
"url": "http://github.com/marcuswestin/store.js/issues"
27+
},
28+
"engines": {
29+
"browser": "*",
30+
"node": "*"
31+
},
32+
"licenses": [
33+
{
34+
"type": "MIT",
35+
"url": "https://raw.github.com/marcuswestin/store.js/master/store.js"
36+
}
37+
],
38+
"main": "store",
39+
"directories": {
40+
"lib": "."
41+
},
42+
"output": {
43+
"store.js": "."
44+
}
45+
}

store/spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
define(function(require) {
2+
var module = require('./dist/store');
3+
describe('store', function() {
4+
it('should not be disabled', function() {
5+
expect(module.disabled).to.not.be.ok();
6+
});
7+
});
8+
});

store/src/store.transport

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
define(function(require, exports, module) {
2+
3+
// @include https://raw.github.com/marcuswestin/store.js/v{{version}}/store.js
4+
5+
});

0 commit comments

Comments
 (0)