protodiv.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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) {
  40. for(key in obj) {
  41. re = new RegExp("__"+key+"__", "g")
  42. s = s.replace(re, obj[key])
  43. }
  44. return s
  45. }
  46. ProtoDiv.inject = function(id, obj) {
  47. var proto = ProtoDiv.elem(id)
  48. var i
  49. proto.innerHTML = ProtoDiv.substitute(proto.innerHTML, obj)
  50. for(i = 0; i < proto.attributes.length; i++) {
  51. a = proto.attributes[i]
  52. var x = a.textContent ? 'textContent' : 'value';
  53. a[x] = ProtoDiv.substitute(a[x], obj)
  54. }
  55. for(key in obj) {
  56. c = key.substring(1)
  57. 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(i = 0; i < l; i++) {
  92. obj = arr[i]
  93. e = proto.cloneNode(true)
  94. delete e.id
  95. mom.insertBefore(e, sib)
  96. ProtoDiv.inject(e, obj)
  97. }
  98. if(!keep)
  99. proto.style.display = "none"
  100. return proto
  101. }
  102. ProtoDiv.reset = function(id) {
  103. var proto = ProtoDiv.elem(id)
  104. if(proto.origSib !== undefined) {
  105. proto.style.display = proto.origDisplay
  106. while(proto.nextSibling !== proto.origSib) {
  107. proto.parentNode.removeChild(proto.nextSibling)
  108. }
  109. delete proto.origSib
  110. delete proto.origDisplay
  111. }
  112. return proto
  113. }