protodiv.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Copyright 2011 Sleepless Software Inc. All rights reserved.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to
  5. deal in the Software without restriction, including without limitation the
  6. rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. sell copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  16. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  17. IN THE SOFTWARE.
  18. */
  19. ProtoDiv = {}
  20. ProtoDiv.elem = function(v) {
  21. return (typeof v === "string") ? document.getElementById(v) : v
  22. }
  23. ProtoDiv.reduce = function(list, cb) {
  24. var i, l = list.length
  25. for(i = 0; i < l; i++)
  26. cb(list[i])
  27. }
  28. ProtoDiv.map = function(node, list, cb) {
  29. if(node.hasChildNodes()) {
  30. var kids = node.childNodes
  31. for(var i = 0; i < kids.length; i++) {
  32. var kid = kids[i]
  33. if(cb(kid))
  34. list.push(kid)
  35. ProtoDiv.map(kid, list, cb)
  36. }
  37. }
  38. }
  39. ProtoDiv.substitute = function(s, obj, i) {
  40. if(i !== undefined)
  41. s = s.replace(/__i__/g, i);
  42. for(var key in obj) {
  43. re = new RegExp("__"+key+"__", "g")
  44. s = s.replace(re, obj[key])
  45. }
  46. return s
  47. }
  48. ProtoDiv.inject = function(id, obj, n) {
  49. var proto = ProtoDiv.elem(id)
  50. proto.innerHTML = ProtoDiv.substitute(proto.innerHTML, obj, n)
  51. for(var i = 0; i < proto.attributes.length; i++) {
  52. var a = proto.attributes[i]
  53. a.textContent = ProtoDiv.substitute(a.textContent, obj, n)
  54. }
  55. for(var key in obj) {
  56. var c = key.substring(1)
  57. var list = []
  58. switch(key.substring(0,1)) {
  59. case "#":
  60. ProtoDiv.map(proto, list, function(e) {
  61. return e.id == c
  62. })
  63. ProtoDiv.reduce(list, function(e) {
  64. e.innerHTML = obj[key]
  65. })
  66. break
  67. case ".":
  68. ProtoDiv.map(proto, list, function(e) {
  69. return e.className == c
  70. })
  71. ProtoDiv.reduce(list, function(e) {
  72. e.innerHTML = obj[key]
  73. })
  74. break
  75. }
  76. }
  77. return proto
  78. }
  79. ProtoDiv.replicate = function(id, arr, keep) {
  80. var proto = ProtoDiv.elem(id)
  81. var sib = proto.nextSibling // might be null
  82. var mom = proto.parentNode
  83. if(!(arr instanceof Array))
  84. arr = [arr]
  85. var l = arr.length
  86. var obj
  87. if(proto.origSib === undefined) {
  88. proto.origSib = sib
  89. proto.origDisplay = proto.style.display
  90. }
  91. for(var i = 0; i < l; i++) {
  92. obj = arr[i]
  93. var e = proto.cloneNode(true)
  94. delete e.id
  95. mom.insertBefore(e, sib)
  96. ProtoDiv.inject(e, obj, i)
  97. if(ProtoDiv.each)
  98. ProtoDiv.each(e, i, obj, mom)
  99. }
  100. if(!keep)
  101. proto.style.display = "none"
  102. return proto
  103. }
  104. ProtoDiv.reset = function(id) {
  105. var proto = ProtoDiv.elem(id)
  106. if(proto.origSib !== undefined) {
  107. proto.style.display = proto.origDisplay
  108. while(proto.nextSibling !== proto.origSib) {
  109. proto.parentNode.removeChild(proto.nextSibling)
  110. }
  111. delete proto.origSib
  112. delete proto.origDisplay
  113. }
  114. return proto
  115. }