UNPKG

ignite-ui

Version:

Ignite UI for jQuery by Infragistics

1 lines 5.09 kB
"use strict";(function(factory){if(typeof define==="function"&&define.amd){define(["jquery","./infragistics.util","./infragistics.util.jquery","./infragistics.templating.js"],factory)}else{factory(jQuery)}})(function($){$.ig=$.ig||{};$.extend($.ig,{_tokenizeDirectives:function(template){var tmpl='var result = "";',tokens=$.ig.regExp.block.exec(template),temp;if(template.indexOf(tokens[0])>0||template.length!==tokens[0].length){temp=template.split(tokens[0]);if(temp[0]&&temp[0].length>0){$.ig.args.push(temp[0]);tmpl+="result += args["+$.ig.i+++"];"}}tmpl+=$.ig._handleCompleteBlock(tokens);if(temp&&temp.length>0&&temp[1].length>0){$.ig.args.push(temp[1]);tmpl+="result += args["+$.ig.i+++"];"}tmpl+="return result;";return tmpl},_handleCompleteBlock:function(tokens){var tmpl=tokens[0],template="",blocks=[],i,temp;tmpl=tmpl.replace("{{"+tokens[1],$.ig._directives[tokens[1]].start);if(tokens[1]==="each"){blocks.push(tokens[0].split(tokens[3]));blocks.push(tokens[2].split(tokens[3]));for(i=0;i<blocks[1].length;i++){if(blocks[0][i].indexOf("{{each")>0){temp=blocks[0][i].split("{{each")[0];$.ig.args.push(temp);template+="result += args["+$.ig.i+++"];";blocks[0][i]=blocks[0][i].substr(blocks[0][i].indexOf("{{each"));temp=$.ig.regExp.blockDirective.exec(blocks[1][i]);blocks[1][i]=blocks[1][i].substr(blocks[1][i].indexOf(temp[0])+temp[0].length)}blocks[0][i]=blocks[0][i].replace("{{"+tokens[1],$.ig._directives[tokens[1]].start);template+=$.ig._handleEach(blocks[0][i]+"{{/each}}",[blocks[0][i]+"{{/each}}","each",blocks[1][i],"{{/each}}"])}}else if(tokens[1]==="if"){template+=$.ig._handleIfElse(tmpl,tokens)}return template},_handleEach:function(template,tokens){var tmpl=template,eachVar,body,forSub,sub,expr;eachVar=$.ig.regExp.sub.exec(tmpl);tmpl=tmpl.replace(eachVar[0],"");tmpl=tmpl.replace("$data",eachVar[0]);body=tokens[2];if(/\$data/.test(body)){body=body.replace(/\$data/g,'" + '+eachVar[0]+'[ i ] + "');$.ig.args.push(eachVar[0]);$.ig.i++}forSub=$.ig.regExp.forSub.exec(body);while(forSub){body=body.replace(new RegExp("\\$\\{"+forSub[1]+"\\}","g"),'" + '+eachVar[0]+"[ i ]"+forSub[1].substr(forSub[1].indexOf("."))+' + "');forSub=$.ig.regExp.forSub.exec(body)}body=body.replace(/\$index/g,'" + i + "');tmpl=tmpl.replace(tokens[2],'result += "'+body+'"');tmpl=tmpl.replace(/\}\}/,$.ig._directives[tokens[1]].close);tmpl=tmpl.replace(tokens[3],$.ig._directives[tokens[1]].end);sub=$.ig.regExp.sub.exec(tmpl);while(sub){expr=new RegExp("\\$\\{"+sub[1]+"\\}","g");tmpl=tmpl.replace(expr,"args["+$.ig.i+++"]");$.ig.args.push(sub[0]);sub=$.ig.regExp.sub.exec(tmpl)}return tmpl},_handleIfElse:function(template,tokens){var tmpl=template,i=0,htmlStrings,sub,inner,index,tmplArr=[];tmpl=tmpl.replace(/\}\}/,$.ig._directives[tokens[1]].close);index=tmpl.lastIndexOf(tokens[3]);tmpl=tmpl.substr(0,index)+tmpl.slice(index+tokens[3].length-1);if($.ig.regExp.block.test(tmpl)){inner=$.ig.regExp.block.exec(tmpl);tmpl=tmpl.replace(inner[0],$.ig._handleCompleteBlock(inner))}htmlStrings=tokens[2].split($.ig.regExp.blockDirective);tmplArr.push(tmpl.slice(0,tmpl.indexOf(") {")+3));tmplArr.push(tmpl.slice(tmpl.indexOf(") {")+3));for(i;i<htmlStrings.length;i++){if(htmlStrings[i]&&htmlStrings[i].length&&htmlStrings[i].length>0){tmplArr[1]=tmplArr[1].replace(htmlStrings[i],"result += args["+$.ig.i+++"];");$.ig.args.push(htmlStrings[i])}}tmpl=tmplArr.join("");tokens=$.ig.regExp.blockCont.exec(tmpl);while(tokens){tmpl=tmpl.replace("{{"+tokens[1],$.ig._directives[tokens[1]].start);tmpl=tmpl.replace(/\}\}/,$.ig._directives[tokens[1]].close);tokens=$.ig.regExp.blockCont.exec(tmpl)}sub=$.ig.regExp.sub.exec(tmpl);while(sub){tmpl=tmpl.replace(new RegExp("\\$\\{"+sub[1]+"\\}","g"),"args["+$.ig.i+++"]");$.ig.args.push(sub[0]);sub=$.ig.regExp.sub.exec(tmpl)}return tmpl},_compileTemplate:function(template,data){var i,j,k,result="",temp,tempArgs=[],arg="",f;if($.ig.util.getType(data)!=="array"){for(j=0;j<$.ig.args.length;j++){arg=$.ig.args[j];for(i=0;i<$.ig.tokens.length;i++){if(arg===$.ig.tokens[i][0]){arg=$.ig._getArgumentValue(data,$.ig.tokens[i],arg);break}else if(typeof arg==="string"){arg=$.ig._populateArgumentValue(data,$.ig.tokens[i],arg)}}if(arg===undefined){throw new Error($.ig._getLocaleString("undefinedArgument")+$.ig.tokens[i][0])}if(typeof arg==="string"){arg=arg.replace($.ig.regExp.index,0)}tempArgs.push(arg)}template=template.replace(/\$i/g,0);result=new Function("args",template).call(this,tempArgs)||""}else{temp=template.replace($.ig.regExp.index,"args["+$.ig.args.length+"]");f=new Function("args",temp);for(j=0;j<data.length;j++){tempArgs=[];for(k=0;k<$.ig.args.length;k++){arg=$.ig.args[k];for(i=0;i<$.ig.tokens.length;i++){if(arg===$.ig.tokens[i][0]){arg=$.ig._getArgumentValue(data[j],$.ig.tokens[i],arg);break}else if(typeof arg==="string"){arg=$.ig._populateArgumentValue(data[j],$.ig.tokens[i],arg)}}if(arg===undefined){throw new Error($.ig._getLocaleString("undefinedArgument")+$.ig.tokens[i][0])}if(typeof arg==="string"){arg=arg.replace($.ig.regExp.index,j)}tempArgs.push(arg)}tempArgs.push(j);result+=f.call(this,tempArgs)||""}}return result}})});