Advertisement
Guest User

Untitled

a guest
May 24th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. (function(){
  2. var BranchesHelpers = {
  3. replaceAt : function(str, index, character){
  4. return str.substr(0, index) + character + str.substr(index+character.length);
  5. },
  6. debug : false
  7. }
  8.  
  9. var Branches = function(ele){
  10. this.ele = ele;
  11.  
  12. if(BranchesHelpers.debug) console.log('Debug: \n New instance with tag \n', ele.tagName);
  13. }
  14.  
  15. Branches.prototype = {
  16. txt : function(content){
  17. var txtNode = document.createTextNode(content);
  18. this.ele.appendChild(txtNode);
  19.  
  20. return this;
  21. },
  22. html : function(customHtml){
  23. this.ele.innerHTML = customHtml;
  24.  
  25. return this;
  26. },
  27. add : function(content){
  28. var self = this;
  29. content.forEach(function(val, ind){
  30. if(val == '[object NodeList]' || val == '[object HTMLCollection]'){
  31. for (var i = val.length - 1; i >= 0; i--) { self.ele.appendChild(val[i]) };
  32. }
  33. else self.ele.appendChild(val.ele);
  34.  
  35. })
  36.  
  37. return this;
  38. },
  39. repeat : function(amount, hasIterationCount){
  40. var temp = document.createElement('span');
  41. for (var i = amount - 1; i >= 0; i--) {
  42. var cln = this.ele.cloneNode(true);
  43. if(hasIterationCount){
  44. var dIndex = cln.textContent.indexOf('$');
  45. if(dIndex !== -1) cln.textContent = BranchesHelpers.replaceAt(cln.textContent, dIndex, i+1).replace(cln.textContent, '');
  46.  
  47. var thisAttrs = cln.attributes;
  48. for(var key=0; key<thisAttrs.length; key++){
  49. if('nodeValue' in thisAttrs[key]){
  50. var thisIndex = thisAttrs[key].nodeValue.indexOf('$');
  51. if(thisIndex !== -1){
  52. cln.setAttribute(thisAttrs[key].nodeName, thisAttrs[key].nodeValue.replace('$', i+1));
  53. }
  54. }
  55. }
  56. }
  57. temp.appendChild(cln);
  58. };
  59.  
  60. return temp.children;
  61. },
  62. repeatWith : function(arr, isObjectArray){
  63. arr = arr.reverse();
  64. var self = this;
  65. var temp = document.createElement('span');
  66. var thisAttrs = self.ele.attributes;
  67. // console.log(thisAttrs);
  68. if(isObjectArray){
  69. var vars = Object.keys(arr[0]);
  70. arr.forEach(function(val, ind){
  71. var cln = self.ele.cloneNode(true);
  72. vars.forEach(function(vVal, vInd){
  73. var re = new RegExp('{'+vVal+'}', 'g');
  74. cln.textContent = cln.textContent.replace(re, val[vVal]).replace(/\$/g, arr.length-ind);
  75. })
  76. for(var key=0; key<thisAttrs.length; key++){
  77. vars.forEach(function(vVal, vInd){
  78. var re = new RegExp('{'+vVal+'}', 'g');
  79. // console.log('Replace', thisAttrs[key].nodeName, thisAttrs[key].nodeValue.replace(re, val[vVal]));
  80. cln.setAttribute(thisAttrs[key].nodeName, thisAttrs[key].nodeValue.replace(re, val[vVal]).replace(/\$/g, arr.length-ind));
  81. })
  82. }
  83. temp.appendChild(cln);
  84. })
  85. }
  86. else{
  87. arr.forEach(function(val, ind){
  88. var cln = self.ele.cloneNode(true);
  89. cln.textContent = cln.textContent.replace(/\$/g, val);
  90. for(var key=0; key<thisAttrs.length; key++){
  91. cln.setAttribute(thisAttrs[key].nodeName, thisAttrs[key].nodeValue.replace(/\$/g, val));
  92. }
  93. temp.appendChild(cln);
  94. })
  95. }
  96.  
  97. return temp.children;
  98. },
  99. raw : function(){
  100. return this.ele.outerHTML;
  101. },
  102. get : function(){
  103. return this.ele;
  104. },
  105. frag : function(){
  106. var frag = document.createDocumentFragment();
  107. frag.appendChild(this.ele);
  108. return frag;
  109. }
  110. }
  111.  
  112. window.Branches = Branches;
  113. })();
  114.  
  115. String.prototype.$ = function(attr){
  116. var ele = document.createElement(this);
  117. if(attr) for(key in attr) ele.setAttribute(key, attr[key]);
  118.  
  119. var branchesInstance = new Branches(ele);
  120. return branchesInstance;
  121. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement