author | Clipperz <info@clipperz.com> | 2013-01-09 09:03:53 (UTC) |
---|---|---|
committer | Clipperz <info@clipperz.com> | 2013-01-09 09:03:53 (UTC) |
commit | 644891059e1f37d0748bc34b9ca78c05fa748214 (patch) (side-by-side diff) | |
tree | a746ee125d503088dc53dac823474e4295560947 /frontend/gamma/js/Zepto/data.js | |
parent | a9974c63bdcef65f009420dce095d1e39da7e3b8 (diff) | |
download | clipperz-644891059e1f37d0748bc34b9ca78c05fa748214.zip clipperz-644891059e1f37d0748bc34b9ca78c05fa748214.tar.gz clipperz-644891059e1f37d0748bc34b9ca78c05fa748214.tar.bz2 |
Added JQTouch and Zepto libraries
JQTouch and Zepto are tentatively used for the mobile version of Clipperz.
No final commitment has been made, though.
Diffstat (limited to 'frontend/gamma/js/Zepto/data.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/gamma/js/Zepto/data.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/frontend/gamma/js/Zepto/data.js b/frontend/gamma/js/Zepto/data.js new file mode 100644 index 0000000..b4c289f --- a/dev/null +++ b/frontend/gamma/js/Zepto/data.js @@ -0,0 +1,67 @@ +// Zepto.js +// (c) 2010-2012 Thomas Fuchs +// Zepto.js may be freely distributed under the MIT license. + +// The following code is heavily inspired by jQuery's $.fn.data() + +;(function($) { + var data = {}, dataAttr = $.fn.data, camelize = $.camelCase, + exp = $.expando = 'Zepto' + (+new Date()) + + // Get value from node: + // 1. first try key as given, + // 2. then try camelized key, + // 3. fall back to reading "data-*" attribute. + function getData(node, name) { + var id = node[exp], store = id && data[id] + if (name === undefined) return store || setData(node) + else { + if (store) { + if (name in store) return store[name] + var camelName = camelize(name) + if (camelName in store) return store[camelName] + } + return dataAttr.call($(node), name) + } + } + + // Store value under camelized key on node + function setData(node, name, value) { + var id = node[exp] || (node[exp] = ++$.uuid), + store = data[id] || (data[id] = attributeData(node)) + if (name !== undefined) store[camelize(name)] = value + return store + } + + // Read all "data-*" attributes from a node + function attributeData(node) { + var store = {} + $.each(node.attributes, function(i, attr){ + if (attr.name.indexOf('data-') == 0) + store[camelize(attr.name.replace('data-', ''))] = + $.zepto.deserializeValue(attr.value) + }) + return store + } + + $.fn.data = function(name, value) { + return value === undefined ? + // set multiple values via object + $.isPlainObject(name) ? + this.each(function(i, node){ + $.each(name, function(key, value){ setData(node, key, value) }) + }) : + // get value from first element + this.length == 0 ? undefined : getData(this[0], name) : + // set value on all elements + this.each(function(){ setData(this, name, value) }) + } + + $.fn.removeData = function(names) { + if (typeof names == 'string') names = names.split(/\s+/) + return this.each(function(){ + var id = this[exp], store = id && data[id] + if (store) $.each(names, function(){ delete store[camelize(this)] }) + }) + } +})(Zepto) |