elem-tool.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var FeHelper = window.FeHelper || {};
  2. FeHelper.elemTool = {
  3. /*******************************************************************************
  4. Usage A:
  5. elemTool.elm('div',{'id':'hello','event':['click',function(){alert('hi');}]},[
  6. elemTool.txt('text inside block element'),
  7. elemTool.elm('hr',{'style':'clear:both;'})
  8. ],document.body);
  9. Usage B:
  10. var myelm = elemTool.elm('div',{'id':'hello','event':['click',function(){alert('hi');}]},[
  11. elemTool.txt('text inside block element'),
  12. elemTool.elm('hr',{'style':'clear:both;'})
  13. ]);
  14. document.body.appendChild(myelm);
  15. OR
  16. elemTool.insertNode(myelm, document.body);
  17. Creates:
  18. <div id="hello">
  19. text inside block element
  20. <hr style="clear:both;">
  21. </div>
  22. Where clicking the text or hr displays 'hi'
  23. Pattern:
  24. elemTool.elm('div',{'attribute':'one'},[elemTool.txt('children')],document.body);
  25. <body><div attribute="one">children</div></body>
  26. Conclusions: you may nest elemTool.elm calls in exactly the same way
  27. as you would nest HTML elements.
  28. Parameters:
  29. nodeType
  30. node type such as 'img' 'div' or 'a'
  31. attributes an object {} that contains attributes. Once special attribute
  32. 'events' may be used to specify events as follows:
  33. {'href':'#','events':[['mouseover',callfn,false],['mouseout',callfn2]]}
  34. to ensure these listeners are attached see appendTo below.
  35. the format for events is [eventType,callback,useCapture], you may also
  36. specify a single event. See appendTo below for event attachment info
  37. 'loadevents' may be used to specify early attach events as follows:
  38. {'loadevents':[['load',loadedFn,false],['load',loadedFn2]],'src':'img.png'}
  39. load events are attached immediately in the order they are processed. If
  40. you wish load events to be attached before src is defiend to counter an IE
  41. but where cached images load event fires immediately,
  42. then sepecify loadevents before src
  43. addchilds an array [] containing nodes to be appended as children, could contain
  44. an array of calls to elemTool.elm which create this array of nodes.
  45. appnedTo should ONLY be specified on the last element that needs to be created
  46. which means the TOP level element (or the final parameter on the first
  47. or outter most call to elemTool.elm).
  48. Empty Patteren:
  49. elemTool.elm('div',{},[],document.body);
  50. *******************************************************************************/
  51. elm : function(nodeType,attributes,addchilds,appnedTo){
  52. var ne=document.createElement(nodeType),i,l;
  53. if(attributes){
  54. if( attributes.event || attributes.events ){
  55. var lev=attributes.event || attributes.events;
  56. if(typeof(lev[0])=='string') ne.addEventListener(lev[0],lev[1],lev[2]);
  57. else if(lev.length)
  58. for(i=0,l=lev.length;i<l;i++)
  59. ne.addEventListener(lev[i][0],lev[i][1],lev[i][2]);
  60. }
  61. }
  62. for( i in attributes ){
  63. if( i.substring(0,5) == 'event' ){
  64. //handled earlier
  65. }else if( i == 'checked' || i == 'selected'){
  66. if(attributes[i])ne.setAttribute(i,i);
  67. }else ne.setAttribute(i,attributes[i]);
  68. }
  69. if(addchilds){
  70. for( i=0,l=addchilds.length;i<l;i++ ){
  71. if(addchilds[i])ne.appendChild(addchilds[i]);//you probably forgot a comma when calling the function
  72. }
  73. }
  74. if(appnedTo){
  75. this.insertNode(ne, appnedTo);
  76. }
  77. return ne;//identifier unexpected error pointing here means you're missing a comma on the row before inside an array of nodes addchilds
  78. },
  79. /*elemTool.txt creates text nodes, does not support HTML entiteis */
  80. txt : function(textContent){
  81. return document.createTextNode(textContent);
  82. },
  83. /*elemTool.ent creates text nodes that may or may not contain HTML entities. From a
  84. single entity to many entities interspersed with text are all supported by this */
  85. ent : function(textContent){
  86. return document.createTextNode(this.unescapeHtml(textContent));
  87. },
  88. /*elemTool.paragraphs creates an array of nodes that may or may not contain HTML entities.*/
  89. paragraphs : function(textContent){
  90. var textPieces=textContent.split("\n");
  91. var elmArray=[];
  92. for(var i=0,l=textPieces.length;i<l;i++){
  93. elmArray.push(elemTool.elm('p',{},[elemTool.ent(textPieces[i])]));
  94. }
  95. return elmArray;
  96. },
  97. insertNode : function(newNode, parentElem, optionalInsertBefore){
  98. if(!parentElem)parentElem=document.body;
  99. if(optionalInsertBefore && optionalInsertBefore.parentNode == parentElem){
  100. parentElem.insertBefore(newNode,optionalInsertBefore);
  101. }else{
  102. parentElem.appendChild(newNode);
  103. }
  104. },
  105. insertNodes : function(newNodes, parentElem, optionalInsertBefore){
  106. if(typeof(newNodes)!='array')
  107. this.insertNode(newNodes, parentElem, optionalInsertBefore);
  108. else{
  109. for(var i=0,l=newNodes.length;i<l;i++){
  110. this.insertNode(newNodes[i], parentElem, optionalInsertBefore, true);
  111. }
  112. }
  113. },
  114. empty : function(node){
  115. while(node.lastChild)node.removeChild(node.lastChild);
  116. },
  117. unescapeHtml : function(str) { //trick used to make HTMLentiites work inside textNodes
  118. if(str.length < 1)return str;
  119. var temp = document.createElement("div");
  120. str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '');
  121. temp.innerHTML = str;
  122. var result = temp.childNodes[0].nodeValue;
  123. this.empty(temp);
  124. return result;
  125. }
  126. }