123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- /*
- Copyright 2011 Sleepless Software Inc. All rights reserved.
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to
- deal in the Software without restriction, including without limitation the
- rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- sell copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- IN THE SOFTWARE.
- */
- ProtoDiv = {}
- ProtoDiv.elem = function(v) {
- return (typeof v === "string") ? document.getElementById(v) : v
- }
- ProtoDiv.reduce = function(list, cb) {
- var i, l = list.length
- for(i = 0; i < l; i++)
- cb(list[i])
- }
- ProtoDiv.map = function(node, list, cb) {
- if(node.hasChildNodes()) {
- var kids = node.childNodes
- for(var i = 0; i < kids.length; i++) {
- var kid = kids[i]
- if(cb(kid))
- list.push(kid)
- ProtoDiv.map(kid, list, cb)
- }
- }
- }
- ProtoDiv.substitute = function(s, obj) {
- for(key in obj) {
- re = new RegExp("__"+key+"__", "g")
- s = s.replace(re, obj[key])
- }
- return s
- }
- ProtoDiv.inject = function(id, obj) {
- var proto = ProtoDiv.elem(id)
- var i
- proto.innerHTML = ProtoDiv.substitute(proto.innerHTML, obj)
- for(i = 0; i < proto.attributes.length; i++) {
- a = proto.attributes[i]
- var x = a.textContent ? 'textContent' : 'value';
- a[x] = ProtoDiv.substitute(a[x], obj)
- }
- for(key in obj) {
- c = key.substring(1)
- list = []
- switch(key.substring(0,1)) {
- case "#":
- ProtoDiv.map(proto, list, function(e) {
- return e.id == c
- })
- ProtoDiv.reduce(list, function(e) {
- e.innerHTML = obj[key]
- })
- break
- case ".":
- ProtoDiv.map(proto, list, function(e) {
- return e.className == c
- })
- ProtoDiv.reduce(list, function(e) {
- e.innerHTML = obj[key]
- })
- break
- }
- }
- return proto
- }
- ProtoDiv.replicate = function(id, arr, keep) {
- var proto = ProtoDiv.elem(id)
- var sib = proto.nextSibling // might be null
- var mom = proto.parentNode
- if(!(arr instanceof Array))
- arr = [arr]
- var l = arr.length
- var obj
-
- if(proto.origSib === undefined) {
- proto.origSib = sib
- proto.origDisplay = proto.style.display
- }
- for(i = 0; i < l; i++) {
- obj = arr[i]
- e = proto.cloneNode(true)
- delete e.id
- mom.insertBefore(e, sib)
- ProtoDiv.inject(e, obj)
- }
- if(!keep)
- proto.style.display = "none"
- return proto
- }
- ProtoDiv.reset = function(id) {
- var proto = ProtoDiv.elem(id)
- if(proto.origSib !== undefined) {
- proto.style.display = proto.origDisplay
- while(proto.nextSibling !== proto.origSib) {
- proto.parentNode.removeChild(proto.nextSibling)
- }
- delete proto.origSib
- delete proto.origDisplay
- }
- return proto
- }
|