Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function(){
- var BranchesHelpers = {
- replaceAt : function(str, index, character){
- return str.substr(0, index) + character + str.substr(index+character.length);
- },
- debug : false
- }
- var Branches = function(ele){
- this.ele = ele;
- if(BranchesHelpers.debug) console.log('Debug: \n New instance with tag \n', ele.tagName);
- }
- Branches.prototype = {
- txt : function(content){
- var txtNode = document.createTextNode(content);
- this.ele.appendChild(txtNode);
- return this;
- },
- html : function(customHtml){
- this.ele.innerHTML = customHtml;
- return this;
- },
- add : function(content){
- var self = this;
- content.forEach(function(val, ind){
- if(val == '[object NodeList]' || val == '[object HTMLCollection]'){
- for (var i = val.length - 1; i >= 0; i--) { self.ele.appendChild(val[i]) };
- }
- else self.ele.appendChild(val.ele);
- })
- return this;
- },
- repeat : function(amount, hasIterationCount){
- var temp = document.createElement('span');
- for (var i = amount - 1; i >= 0; i--) {
- var cln = this.ele.cloneNode(true);
- if(hasIterationCount){
- var dIndex = cln.textContent.indexOf('$');
- if(dIndex !== -1) cln.textContent = BranchesHelpers.replaceAt(cln.textContent, dIndex, i+1).replace(cln.textContent, '');
- var thisAttrs = cln.attributes;
- for(var key=0; key<thisAttrs.length; key++){
- if('nodeValue' in thisAttrs[key]){
- var thisIndex = thisAttrs[key].nodeValue.indexOf('$');
- if(thisIndex !== -1){
- cln.setAttribute(thisAttrs[key].nodeName, thisAttrs[key].nodeValue.replace('$', i+1));
- }
- }
- }
- }
- temp.appendChild(cln);
- };
- return temp.children;
- },
- repeatWith : function(arr, isObjectArray){
- arr = arr.reverse();
- var self = this;
- var temp = document.createElement('span');
- var thisAttrs = self.ele.attributes;
- // console.log(thisAttrs);
- if(isObjectArray){
- var vars = Object.keys(arr[0]);
- arr.forEach(function(val, ind){
- var cln = self.ele.cloneNode(true);
- vars.forEach(function(vVal, vInd){
- var re = new RegExp('{'+vVal+'}', 'g');
- cln.textContent = cln.textContent.replace(re, val[vVal]).replace(/\$/g, arr.length-ind);
- })
- for(var key=0; key<thisAttrs.length; key++){
- vars.forEach(function(vVal, vInd){
- var re = new RegExp('{'+vVal+'}', 'g');
- // console.log('Replace', thisAttrs[key].nodeName, thisAttrs[key].nodeValue.replace(re, val[vVal]));
- cln.setAttribute(thisAttrs[key].nodeName, thisAttrs[key].nodeValue.replace(re, val[vVal]).replace(/\$/g, arr.length-ind));
- })
- }
- temp.appendChild(cln);
- })
- }
- else{
- arr.forEach(function(val, ind){
- var cln = self.ele.cloneNode(true);
- cln.textContent = cln.textContent.replace(/\$/g, val);
- for(var key=0; key<thisAttrs.length; key++){
- cln.setAttribute(thisAttrs[key].nodeName, thisAttrs[key].nodeValue.replace(/\$/g, val));
- }
- temp.appendChild(cln);
- })
- }
- return temp.children;
- },
- raw : function(){
- return this.ele.outerHTML;
- },
- get : function(){
- return this.ele;
- },
- frag : function(){
- var frag = document.createDocumentFragment();
- frag.appendChild(this.ele);
- return frag;
- }
- }
- window.Branches = Branches;
- })();
- String.prototype.$ = function(attr){
- var ele = document.createElement(this);
- if(attr) for(key in attr) ele.setAttribute(key, attr[key]);
- var branchesInstance = new Branches(ele);
- return branchesInstance;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement