update soda.js

This commit is contained in:
dntzhang 2017-04-19 09:18:27 +08:00
parent df923383ee
commit 8c5cfbb0e1
4 changed files with 323 additions and 275 deletions

View File

@ -77,7 +77,7 @@
key: 'render',
value: function render() {
return '<div>\n <hello soda-if="show" data-name="{{name}}"></hello>\n <div soda-repeat="item in items" soda-if="item.show">\n {{$index}}- {{item.text}}\n </div>\n </div>';
return '<div>\n <hello o-repeat="item in items" o-if="item.show" data-name="{{item.text}}"></hello>\n <div o-repeat="item in items" o-if="item.show">\n {{$index}}- {{item.text}}\n <ul>\n <li o-repeat="subItem in item.arr by $subIndex">\n <div>parent index: {{$index}}</div>\n <div>parent item text:{{item.text}}</div>\n <div>{{$subIndex}}{{subItem}}</div>\n </li>\n </ul>\n </div>\n </div>';
}
}]);
@ -87,7 +87,7 @@
_indexSoda2['default'].render(new List({
show: true,
name: 'Omi',
items: [{ text: 'Omi', show: true }, { text: 'dntzhang', show: true }, { text: 'AlloyTeam' }]
items: [{ text: 'Omi', show: true, arr: ['a', 'b', 'c'] }, { text: 'dntzhang', show: true }, { text: 'AlloyTeam' }]
}), "body", true);
/***/ },
@ -114,6 +114,8 @@
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
_soda2['default'].prefix('o');
_omi2['default'].template = _soda2['default'];
_omi2['default'].Store = _store2['default'];
@ -480,7 +482,7 @@
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
/**
* sodajs v0.4.0 by dorsywang
* sodajs v0.4.1 by dorsywang
* Light weight but powerful template engine for JavaScript
* Github: https://github.com/AlloyTeam/sodajs
* MIT License
@ -489,6 +491,9 @@
;(function () {
var valueoutReg = /\{\{([^\}]*)\}\}/g;
var prefix = 'soda';
var prefixReg = new RegExp('^' + prefix + '-');
var classNameRegExp = function classNameRegExp(className) {
return new RegExp('(^|\\s+)' + className + '(\\s+|$)', 'g');
};
@ -534,7 +539,7 @@
var attr = attrStr.substr(0, dotIndex);
attrStr = attrStr.substr(dotIndex + 1);
// <EFBFBD><EFBFBD>?查attrStr是否属<E590A6>?<3F>变量并转换
// ??<3F><>attrStr<74>Ƿ<EFBFBD><C7B7><EFBFBD>???<3F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>
if (typeof _data[attr] !== "undefined" && CONST_REG.test(attr)) {
attr = _data[attr];
}
@ -552,12 +557,12 @@
data: eventData
}, eventData);
// 如果还有
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return "";
}
} else {
// <EFBFBD><EFBFBD>?查attrStr是否属<E590A6>?<3F>变量并转换
// ??<3F><>attrStr<74>Ƿ<EFBFBD><C7B7><EFBFBD>???<3F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>
if (typeof _data[attrStr] !== "undefined" && CONST_REG.test(attrStr)) {
attrStr = _data[attrStr];
}
@ -586,10 +591,10 @@
return _getValue(_data, _attrStr);
};
// 注释node
// ע<EFBFBD><EFBFBD>node
var commentNode = function commentNode(node) {};
// 标识<EFBFBD><EFBFBD>?
// <EFBFBD><EFBFBD>ʶ??
var IDENTOR_REG = /[a-zA-Z_\$]+[\w\$]*/g;
var STRING_REG = /"([^"]*)"|'([^']*)'/g;
var NUMBER_REG = /\d+|\d*\.\d+/g;
@ -617,7 +622,7 @@
};
var parseSodaExpression = function parseSodaExpression(str, scope) {
// 对filter进行处理
// <EFBFBD><EFBFBD>filter<EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD>
str = str.replace(OR_REG, OR_REPLACE).split("|");
for (var i = 0; i < str.length; i++) {
@ -627,7 +632,7 @@
var expr = str[0] || "";
var filters = str.slice(1);
// 将字符常量保存下<EFBFBD><EFBFBD>?
// <EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>??
expr = expr.replace(STRING_REG, function (r, $1, $2) {
var key = getRandom();
scope[key] = $1 || $2;
@ -637,13 +642,13 @@
while (ATTR_REG.test(expr)) {
ATTR_REG.lastIndex = 0;
//对expr预处<EFBFBD><EFBFBD>?
//<EFBFBD><EFBFBD>exprԤ<EFBFBD><EFBFBD>??
expr = expr.replace(ATTR_REG, function (r, $1) {
var key = getAttrVarKey();
// <EFBFBD>?<3F><EFBFBD><E5908D>? 为字符常<E7ACA6><E5B8B8>?
// <EFBFBD><EFBFBD>???<3F><>?? Ϊ<>ַ<EFBFBD><D6B7><EFBFBD>??
var attrName = parseSodaExpression($1, scope);
// 给一个特殊的前缀 表示是属性变<E680A7><E58F98>?
// <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ׺ <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD>??
scope[key] = attrName;
@ -668,7 +673,7 @@
var stringReg = /^'.*'$|^".*"$/;
for (var i = 0; i < args.length; i++) {
//这里根据类型进行判断
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
if (OBJECT_REG.test(args[i])) {
args[i] = "getValue(scope,'" + args[i] + "')";
} else {}
@ -702,30 +707,30 @@
}
};
// 解析指令
// 解析attr
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>attr
var compileNode = function compileNode(node, scope) {
// 如果只是文本
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>
if (node.nodeType === 3) {
node.nodeValue = node.nodeValue.replace(valueoutReg, function (item, $1) {
/*
var id = hashTable.getRandId();
hashTable.id2Expression[id] = {
expression: $1,
el: child
};
hashTable.expression2id[$1] = {
id: id,
el: child
};
*/
var id = hashTable.getRandId();
hashTable.id2Expression[id] = {
expression: $1,
el: child
};
hashTable.expression2id[$1] = {
id: id,
el: child
};
*/
return parseSodaExpression($1, scope);
});
}
if (node.attributes) {
// 指令处理
// ָ<EFBFBD><EFBFBD><EFBFBD>
sodaDirectiveArr.map(function (item) {
var name = item.name;
@ -736,12 +741,12 @@
}
});
// 处理输出 包含 soda-*
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> prefix-*
[].map.call(node.attributes, function (attr) {
// 如果dirctiveMap有的就跳过不再处<EFBFBD><EFBFBD>?
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>dirctiveMap<EFBFBD>еľ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٴ<EFBFBD>??
if (!sodaDirectiveMap[attr.name]) {
if (/^soda-/.test(attr.name)) {
var attrName = attr.name.replace(/^soda-/, '');
if (prefixReg.test(attr.name)) {
var attrName = attr.name.replace(prefixReg, '');
if (attrName) {
var attrValue = attr.value.replace(valueoutReg, function (item, $1) {
@ -751,7 +756,7 @@
node.setAttribute(attrName, attrValue);
}
// 对其他属性里含expr 处理
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ﺬexpr <20><><EFBFBD><EFBFBD>
} else {
attr.value = attr.value.replace(valueoutReg, function (item, $1) {
return parseSodaExpression($1, scope);
@ -773,7 +778,7 @@
var sodaDirectiveArr = [];
var sodaDirective = function sodaDirective(name, func) {
var name = 'soda-' + name;
var name = prefix + '-' + name;
sodaDirectiveMap[name] = func();
sodaDirectiveArr.push({
@ -799,7 +804,7 @@
priority: 10,
compile: function compile(scope, el, attrs) {},
link: function link(scope, el, attrs) {
var opt = el.getAttribute('soda-repeat');
var opt = el.getAttribute(prefix + '-repeat');
var itemName;
var valueName;
@ -836,13 +841,13 @@
trackName = trackName || '$index';
// 这里要处理一<EFBFBD><EFBFBD>?
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ??
var repeatObj = getValue(scope, valueName) || [];
var repeatFunc = function repeatFunc(i) {
var itemNode = el.cloneNode(true);
// 这里创建<EFBFBD><EFBFBD>?个新的scope
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>??<3F><><EFBFBD>µ<EFBFBD>scope
var itemScope = {};
itemScope[trackName] = i;
@ -850,11 +855,11 @@
itemScope.__proto__ = scope;
itemNode.removeAttribute('soda-repeat');
itemNode.removeAttribute(prefix + '-repeat');
el.parentNode.insertBefore(itemNode, el);
// 这里是新加的dom, 要单独编<E78BAC><E7BC96>?
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼ӵ<EFBFBD>dom, Ҫ<><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>??
compileNode(itemNode, itemScope);
};
@ -879,7 +884,7 @@
return {
priority: 9,
link: function link(scope, el, attrs) {
var opt = el.getAttribute('soda-if');
var opt = el.getAttribute(prefix + '-if');
var expressFunc = parseSodaExpression(opt, scope);
@ -894,7 +899,7 @@
sodaDirective('class', function () {
return {
link: function link(scope, el, attrs) {
var opt = el.getAttribute("soda-class");
var opt = el.getAttribute(prefix + "-class");
var expressFunc = parseSodaExpression(opt, scope);
@ -908,7 +913,7 @@
sodaDirective('src', function () {
return {
link: function link(scope, el, attrs) {
var opt = el.getAttribute("soda-src");
var opt = el.getAttribute(prefix + "-src");
var expressFunc = opt.replace(valueoutReg, function (item, $1) {
return parseSodaExpression($1, scope);
@ -924,7 +929,7 @@
sodaDirective('bind-html', function () {
return {
link: function link(scope, el, attrs) {
var opt = el.getAttribute("soda-bind-html");
var opt = el.getAttribute(prefix + "-bind-html");
var expressFunc = parseSodaExpression(opt, scope);
if (expressFunc) {
@ -941,7 +946,7 @@
sodaDirective('html', function () {
return {
link: function link(scope, el, attrs) {
var opt = el.getAttribute("soda-html");
var opt = el.getAttribute(prefix + "-html");
var expressFunc = parseSodaExpression(opt, scope);
if (expressFunc) {
@ -958,7 +963,7 @@
sodaDirective("style", function () {
return {
link: function link(scope, el, attrs) {
var opt = el.getAttribute("soda-style");
var opt = el.getAttribute(prefix + "-style");
var expressFunc = parseSodaExpression(opt, scope);
var getCssValue = function getCssValue(name, value) {
@ -1002,14 +1007,14 @@
});
var sodaRender = function sodaRender(str, data) {
// 对directive进行排序
// <EFBFBD><EFBFBD>directive<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
sodaDirectiveArr.sort(function (b, a) {
return Number(a.opt.priority || 0) - Number(b.opt.priority || 0);
});
//console.log(sodaDirectiveArr);
// 解析模板DOM
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD>DOM
var div = document.createElement("div");
div.innerHTML = str;
@ -1024,24 +1029,24 @@
// frament.innerHTML = div.innerHTML;
/*
frament.update = function(newData){
//checkingDirtyData(data, d);
var diff = DeepDiff.noConflict();
var diffResult = diff(data, newData);
console.log(diffResult);
var dirtyData = ['a'];
for(var i = 0; i < dirtyData.length; i ++){
var item = dirtyData[i];
var id = hashTable.expression2id[item];
var nowValue = parseSodaExpression(item, newData);
//console.log(nowValue);
if(id.el){
id.el.nodeValue = nowValue;
}
}
console.log(hashTable);
};
*/
frament.update = function(newData){
//checkingDirtyData(data, d);
var diff = DeepDiff.noConflict();
var diffResult = diff(data, newData);
console.log(diffResult);
var dirtyData = ['a'];
for(var i = 0; i < dirtyData.length; i ++){
var item = dirtyData[i];
var id = hashTable.expression2id[item];
var nowValue = parseSodaExpression(item, newData);
//console.log(nowValue);
if(id.el){
id.el.nodeValue = nowValue;
}
}
console.log(hashTable);
};
*/
var child;
while (child = div.childNodes[0]) {
@ -1060,8 +1065,6 @@
eventPool[type].push(func);
};
sodaRender.author = "dorsy";
var triggerEvent = function triggerEvent(type, e, data) {
var events = eventPool[type] || [];
@ -1073,11 +1076,30 @@
sodaRender.filter = sodaFilter;
sodaRender.prefix = function (newPrefix) {
for (var key in sodaDirectiveMap) {
if (sodaDirectiveMap.hasOwnProperty(key)) {
sodaDirectiveMap[key.replace(prefix, newPrefix)] = sodaDirectiveMap[key];
delete sodaDirectiveMap[key];
}
}
var i = 0,
len = sodaDirectiveArr.length;
for (; i < len; i++) {
sodaDirectiveArr[i].name = sodaDirectiveArr[i].name.replace(prefix, newPrefix);
}
prefix = newPrefix;
prefixReg = new RegExp('^' + prefix + '-');
};
if (( false ? 'undefined' : _typeof(exports)) === 'object' && ( false ? 'undefined' : _typeof(module)) === 'object') module.exports = sodaRender;else if (true) !(__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_RESULT__ = function () {
return sodaRender;
}.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));else if ((typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) === 'object') exports["soda"] = sodaRender;else window.soda = sodaRender;
// 监听数据异常情况
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
})();
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(4)(module)))

View File

@ -10,9 +10,16 @@ class List extends Omi.Component {
render(){
return `<div>
<hello soda-if="show" data-name="{{name}}"></hello>
<div soda-repeat="item in items" soda-if="item.show">
<hello o-repeat="item in items" o-if="item.show" data-name="{{item.text}}"></hello>
<div o-repeat="item in items" o-if="item.show">
{{$index}}- {{item.text}}
<ul>
<li o-repeat="subItem in item.arr by $subIndex">
<div>parent index: {{$index}}</div>
<div>parent item text:{{item.text}}</div>
<div>{{$subIndex}}{{subItem}}</div>
</li>
</ul>
</div>
</div>`
}
@ -23,7 +30,7 @@ Omi.render(new List({
show:true,
name:'Omi',
items: [
{ text: 'Omi', show: true},
{ text: 'Omi', show: true ,arr:['a','b','c']},
{ text: 'dntzhang', show: true},
{ text: 'AlloyTeam'}
]

View File

@ -3,6 +3,8 @@ import soda from './soda.js'
import Component from './component.js'
import Store from './store.js'
soda.prefix('o')
Omi.template = soda
Omi.Store = Store

View File

@ -1,67 +1,70 @@
/**
* sodajs v0.4.0 by dorsywang
* sodajs v0.4.1 by dorsywang
* Light weight but powerful template engine for JavaScript
* Github: https://github.com/AlloyTeam/sodajs
* MIT License
*/
;(function(){
;(function() {
var valueoutReg = /\{\{([^\}]*)\}\}/g;
var classNameRegExp = function(className) {
var prefix = 'soda';
var prefixReg = new RegExp('^' + prefix + '-')
var classNameRegExp = function (className) {
return new RegExp('(^|\\s+)' + className + '(\\s+|$)', 'g');
};
var addClass = function(el, className){
if(! el.className){
var addClass = function (el, className) {
if (!el.className) {
el.className = className;
return;
}
if(el.className.match(classNameRegExp(className))){
}else{
el.className += " " + className;
if (el.className.match(classNameRegExp(className))) {
} else {
el.className += " " + className;
}
};
var removeClass = function(el, className){
var removeClass = function (el, className) {
el.className = el.className.replace(classNameRegExp(className), "");
};
var getValue = function(_data, _attrStr){
var getValue = function (_data, _attrStr) {
CONST_REGG.lastIndex = 0;
var realAttrStr = _attrStr.replace(CONST_REGG, function(r){
if(typeof _data[r] === "undefined"){
var realAttrStr = _attrStr.replace(CONST_REGG, function (r) {
if (typeof _data[r] === "undefined") {
return r;
}else{
} else {
return _data[r];
}
});
if(_attrStr === 'true'){
if (_attrStr === 'true') {
return true;
}
if(_attrStr === 'false'){
if (_attrStr === 'false') {
return false;
}
var _getValue = function(data, attrStr){
var _getValue = function (data, attrStr) {
var dotIndex = attrStr.indexOf(".");
if(dotIndex > -1){
if (dotIndex > -1) {
var attr = attrStr.substr(0, dotIndex);
attrStr = attrStr.substr(dotIndex + 1);
// æ£?查attrStr是å<C2AF>¦å±žæ?§å<C2A7>˜é‡<C3A9>并转æ<C2AC>¢
if(typeof _data[attr] !== "undefined" && CONST_REG.test(attr)){
// ??查attrStr是否属???变量并转换
if (typeof _data[attr] !== "undefined" && CONST_REG.test(attr)) {
attr = _data[attr];
}
if(typeof data[attr] !== "undefined"){
if (typeof data[attr] !== "undefined") {
return _getValue(data[attr], attrStr);
}else{
} else {
var eventData = {
name: realAttrStr,
data: _data
@ -72,20 +75,20 @@
data: eventData
}, eventData);
// 如果还有
// 如果还有
return "";
}
}else{
} else {
// æ£?查attrStr是å<C2AF>¦å±žæ?§å<C2A7>˜é‡<C3A9>并转æ<C2AC>¢
if(typeof _data[attrStr] !== "undefined" && CONST_REG.test(attrStr)){
// ??查attrStr是否属???变量并转换
if (typeof _data[attrStr] !== "undefined" && CONST_REG.test(attrStr)) {
attrStr = _data[attrStr];
}
var rValue;
if(typeof data[attrStr] !== "undefined"){
if (typeof data[attrStr] !== "undefined") {
rValue = data[attrStr];
}else{
} else {
var eventData = {
name: realAttrStr,
data: _data
@ -106,11 +109,11 @@
return _getValue(_data, _attrStr);
};
// 注释node
var commentNode = function(node){
// 注释node
var commentNode = function (node) {
};
// 标识�
// 标识??
var IDENTOR_REG = /[a-zA-Z_\$]+[\w\$]*/g;
var STRING_REG = /"([^"]*)"|'([^']*)'/g
var NUMBER_REG = /\d+|\d*\.\d+/g;
@ -125,46 +128,46 @@
var OR_REPLACE = "OR_OPERATOR\x1E";
var getRandom = function(){
return "$$" + ~~ (Math.random() * 1E6);
var getRandom = function () {
return "$$" + ~~(Math.random() * 1E6);
};
var CONST_PRIFIX = "_$C$_";
var CONST_REG = /^_\$C\$_/;
var CONST_REGG = /_\$C\$_[^\.]+/g;
var getAttrVarKey = function(){
return CONST_PRIFIX + ~~ (Math.random() * 1E6);
var getAttrVarKey = function () {
return CONST_PRIFIX + ~~(Math.random() * 1E6);
};
var parseSodaExpression = function(str, scope){
// 对filterè¿è¡Œå¤„ç<EFBFBD>
var parseSodaExpression = function (str, scope) {
// 对filter进行处理
str = str.replace(OR_REG, OR_REPLACE).split("|");
for(var i = 0; i < str.length; i ++){
for (var i = 0; i < str.length; i++) {
str[i] = (str[i].replace(new RegExp(OR_REPLACE, 'g'), "||") || '').trim();
}
var expr = str[0] || "";
var filters = str.slice(1);
// 将字符常é‡<EFBFBD>ä¿<EFBFBD>å­˜ä¸æ<EFBFBD>?
expr = expr.replace(STRING_REG, function(r, $1, $2){
// 将字符常量保存下??
expr = expr.replace(STRING_REG, function (r, $1, $2) {
var key = getRandom();
scope[key] = $1 || $2;
return key;
});
while(ATTR_REG.test(expr)){
while (ATTR_REG.test(expr)) {
ATTR_REG.lastIndex = 0;
//对expr预处ç<EFBFBD>?
expr = expr.replace(ATTR_REG, function(r, $1){
//对expr预处??
expr = expr.replace(ATTR_REG, function (r, $1) {
var key = getAttrVarKey();
// 属æ?§å<C2A7><C3A5>å­? 为字符常é‡?
// 属???名?? 为字符常??
var attrName = parseSodaExpression($1, scope);
// 给一个特殊的å‰<EFBFBD>ç¼€ 表示是属性å<C2A7>˜é‡?
// 给一个特殊的前缀 表示是属性变??
scope[key] = attrName;
@ -172,15 +175,15 @@
});
}
expr = expr.replace(OBJECT_REG, function(value){
expr = expr.replace(OBJECT_REG, function (value) {
return "getValue(scope,'" + value.trim() + "')";
});
var parseFilter = function(){
var parseFilter = function () {
var filterExpr = filters.shift();
if(! filterExpr){
if (!filterExpr) {
return;
}
@ -189,15 +192,15 @@
var name = filterExpr[0] || "";
var stringReg = /^'.*'$|^".*"$/;
for(var i = 0; i < args.length; i ++){
//这里根æ<EFBFBD>®ç±»åžè¿è¡Œåˆ¤æ­
if(OBJECT_REG.test(args[i])){
args[i] = "getValue(scope,'" + args[i] + "')";
}else{
for (var i = 0; i < args.length; i++) {
//这里根据类型进行判断
if (OBJECT_REG.test(args[i])) {
args[i] = "getValue(scope,'" + args[i] + "')";
} else {
}
}
if(sodaFilterMap[name]){
if (sodaFilterMap[name]) {
args.unshift(expr);
args = args.join(",");
@ -216,95 +219,91 @@
};
var hashTable = {
id2Expression: {
},
id2Expression: {},
expression2id: {
},
expression2id: {},
getRandId: function(){
return 'soda' + ~~ (Math.random() * 1E5);
getRandId: function () {
return 'soda' + ~~(Math.random() * 1E5);
}
};
// 解æž<EFBFBD>指令
// 解æž<EFBFBD>attr
var compileNode = function(node, scope){
// å¦æžœå<EFBFBD>ªæ˜¯æ‡æœ¬
if(node.nodeType === 3){
node.nodeValue = node.nodeValue.replace(valueoutReg, function(item, $1){
// 解析指令
// 解析attr
var compileNode = function (node, scope) {
// 如果只是文本
if (node.nodeType === 3) {
node.nodeValue = node.nodeValue.replace(valueoutReg, function (item, $1) {
/*
var id = hashTable.getRandId();
var id = hashTable.getRandId();
hashTable.id2Expression[id] = {
expression: $1,
el: child
};
hashTable.id2Expression[id] = {
expression: $1,
el: child
};
hashTable.expression2id[$1] = {
id: id,
el: child
};
*/
hashTable.expression2id[$1] = {
id: id,
el: child
};
*/
return parseSodaExpression($1, scope);
return parseSodaExpression($1, scope);
});
}
if(node.attributes){
// 指令处ç<EFBFBD>
sodaDirectiveArr.map(function(item){
if (node.attributes) {
// 指令处理
sodaDirectiveArr.map(function (item) {
var name = item.name;
var opt = item.opt;
if(node.getAttribute(name) && node.parentNode){
if (node.getAttribute(name) && node.parentNode) {
opt.link(scope, node, node.attributes);
}
});
// 处ç<EFBFBD>†è¾“出 包å<E280A6>« soda-*
[].map.call(node.attributes, function(attr){
// å¦æžœdirctiveMap有的就跳过ä¸<EFBFBD>å†<EFBFBD>处ç<EFBFBD>?
if(! sodaDirectiveMap[attr.name]){
if(/^soda-/.test(attr.name)){
var attrName = attr.name.replace(/^soda-/, '');
// 处理输出 包含 prefix-*
[].map.call(node.attributes, function (attr) {
// 如果dirctiveMap有的就跳过不再处??
if (!sodaDirectiveMap[attr.name]) {
if (prefixReg.test(attr.name)) {
var attrName = attr.name.replace(prefixReg, '');
if(attrName){
var attrValue = attr.value.replace(valueoutReg, function(item, $1){
return parseSodaExpression($1, scope);
if (attrName) {
var attrValue = attr.value.replace(valueoutReg, function (item, $1) {
return parseSodaExpression($1, scope);
});
node.setAttribute(attrName, attrValue);
}
// 对其ä»å±žæ€§é‡Œå<C592>«expr 处ç<E2809E>
}else{
attr.value = attr.value.replace(valueoutReg, function(item, $1){
return parseSodaExpression($1, scope);
// 对其他属性里含expr 处理
} else {
attr.value = attr.value.replace(valueoutReg, function (item, $1) {
return parseSodaExpression($1, scope);
});
}
}
}
});
}
}
[].map.call([].slice.call(node.childNodes, []), function(child){
[].map.call([].slice.call(node.childNodes, []), function (child) {
compileNode(child, scope);
});
};
var sodaDirectiveMap = {
};
var sodaDirectiveMap = {};
var sodaFilterMap = {
};
var sodaFilterMap = {};
var sodaDirectiveArr = [];
var sodaDirective = function(name, func){
var name = 'soda-' + name;
var sodaDirective = function (name, func) {
var name = prefix + '-' + name;
sodaDirectiveMap[name] = func();
sodaDirectiveArr.push({
@ -313,34 +312,34 @@
});
};
var sodaFilter = function(name, func){
var sodaFilter = function (name, func) {
sodaFilterMap[name] = func;
};
sodaFilter.get = function(name){
sodaFilter.get = function (name) {
return sodaFilterMap[name];
};
sodaFilter("date", function(input, lenth){
sodaFilter("date", function (input, lenth) {
return lenth;
});
sodaDirective('repeat', function(){
sodaDirective('repeat', function () {
return {
priority: 10,
compile: function(scope, el, attrs){
compile: function (scope, el, attrs) {
},
link: function(scope, el, attrs){
var opt = el.getAttribute('soda-repeat');
link: function (scope, el, attrs) {
var opt = el.getAttribute(prefix + '-repeat');
var itemName;
var valueName;
var trackReg = /\s+by\s+([^\s]+)$/;
var trackName;
opt = opt.replace(trackReg, function(item, $1){
if($1){
opt = opt.replace(trackReg, function (item, $1) {
if ($1) {
trackName = ($1 || '').trim();
}
@ -351,32 +350,32 @@
var inReg = /([^\s]+)\s+in\s+([^\s]+)|\(([^,]+)\s*,\s*([^)]+)\)\s+in\s+([^\s]+)/;
var r = inReg.exec(opt);
if(r){
if(r[1] && r[2]){
if (r) {
if (r[1] && r[2]) {
itemName = (r[1] || '').trim();
valueName = (r[2] || '').trim();
if(! (itemName && valueName)){
if (!(itemName && valueName)) {
return;
}
}else if(r[3] && r[4] && r[5]){
} else if (r[3] && r[4] && r[5]) {
trackName = (r[3] || '').trim();
itemName = (r[4] || '').trim();
valueName = (r[5] || '').trim();
}
}else{
} else {
return;
}
trackName = trackName || '$index';
// 这里è¦<EFBFBD>处ç<EFBFBD>†ä¸€ä¸?
// 这里要处理一??
var repeatObj = getValue(scope, valueName) || [];
var repeatFunc = function(i){
var repeatFunc = function (i) {
var itemNode = el.cloneNode(true);
// 这里创建�个新的scope
// 这里创建??个新的scope
var itemScope = {};
itemScope[trackName] = i;
@ -384,22 +383,22 @@
itemScope.__proto__ = scope;
itemNode.removeAttribute('soda-repeat');
itemNode.removeAttribute(prefix + '-repeat');
el.parentNode.insertBefore(itemNode, el);
// 这里是æ°åŠ çš„dom, è¦<C3A8>å<EFBFBD>•ç¬ç¼è¯?
// 这里是新加的dom, 要单独编??
compileNode(itemNode, itemScope);
};
if('length' in repeatObj){
for(var i = 0; i < repeatObj.length; i ++){
if ('length' in repeatObj) {
for (var i = 0; i < repeatObj.length; i++) {
repeatFunc(i);
}
}else{
for(var i in repeatObj){
if(repeatObj.hasOwnProperty(i)){
} else {
for (var i in repeatObj) {
if (repeatObj.hasOwnProperty(i)) {
repeatFunc(i);
}
}
@ -411,16 +410,16 @@
};
});
sodaDirective('if', function(){
sodaDirective('if', function () {
return {
priority: 9,
link: function(scope, el, attrs){
var opt = el.getAttribute('soda-if');
link: function (scope, el, attrs) {
var opt = el.getAttribute(prefix + '-if');
var expressFunc = parseSodaExpression(opt, scope);
if(expressFunc){
}else{
if (expressFunc) {
} else {
// el.setAttribute("removed", "removed");
el.parentNode && el.parentNode.removeChild(el);
}
@ -428,45 +427,45 @@
};
});
sodaDirective('class', function(){
sodaDirective('class', function () {
return {
link: function(scope, el, attrs){
var opt = el.getAttribute("soda-class");
link: function (scope, el, attrs) {
var opt = el.getAttribute(prefix + "-class");
var expressFunc = parseSodaExpression(opt, scope);
if(expressFunc){
if (expressFunc) {
addClass(el, expressFunc);
}else{
} else {
}
}
};
});
sodaDirective('src', function(){
sodaDirective('src', function () {
return {
link: function(scope, el, attrs){
var opt = el.getAttribute("soda-src");
link: function (scope, el, attrs) {
var opt = el.getAttribute(prefix + "-src");
var expressFunc = opt.replace(valueoutReg, function(item, $1){
return parseSodaExpression($1, scope);
var expressFunc = opt.replace(valueoutReg, function (item, $1) {
return parseSodaExpression($1, scope);
});
if(expressFunc){
if (expressFunc) {
el.setAttribute("src", expressFunc);
}else{
} else {
}
}
};
});
sodaDirective('bind-html', function(){
sodaDirective('bind-html', function () {
return {
link: function(scope, el, attrs){
var opt = el.getAttribute("soda-bind-html");
link: function (scope, el, attrs) {
var opt = el.getAttribute(prefix + "-bind-html");
var expressFunc = parseSodaExpression(opt, scope);
if(expressFunc){
if (expressFunc) {
el.innerHTML = expressFunc;
return {
@ -477,13 +476,13 @@
};
});
sodaDirective('html', function(){
sodaDirective('html', function () {
return {
link: function(scope, el, attrs){
var opt = el.getAttribute("soda-html");
link: function (scope, el, attrs) {
var opt = el.getAttribute(prefix + "-html");
var expressFunc = parseSodaExpression(opt, scope);
if(expressFunc){
if (expressFunc) {
el.innerHTML = expressFunc;
return {
@ -494,30 +493,30 @@
};
});
sodaDirective("style", function(){
sodaDirective("style", function () {
return {
link: function(scope, el, attrs){
var opt = el.getAttribute("soda-style");
link: function (scope, el, attrs) {
var opt = el.getAttribute(prefix + "-style");
var expressFunc = parseSodaExpression(opt, scope);
var getCssValue = function(name, value){
var getCssValue = function (name, value) {
var numberWithoutpx = /opacity|z-index/;
if(numberWithoutpx.test(name)){
if (numberWithoutpx.test(name)) {
return parseFloat(value);
}
if(isNaN(value)){
if (isNaN(value)) {
return value;
}else{
} else {
return value + "px";
}
};
if(expressFunc){
if (expressFunc) {
var stylelist = [];
for(var i in expressFunc){
if(expressFunc.hasOwnProperty(i)){
for (var i in expressFunc) {
if (expressFunc.hasOwnProperty(i)) {
var provalue = getCssValue(i, expressFunc[i]);
stylelist.push([i, provalue].join(":"));
@ -525,10 +524,10 @@
}
var style = el.style;
for(var i = 0; i < style.length; i ++){
for (var i = 0; i < style.length; i++) {
var name = style[i];
if(expressFunc[name]){
}else{
if (expressFunc[name]) {
} else {
stylelist.push([name, style[name]].join(":"));
}
}
@ -541,83 +540,81 @@
};
});
var sodaRender = function(str, data){
// 对directiveè¿è¡ŒæŽåº<EFBFBD>
sodaDirectiveArr.sort(function(b, a){
var sodaRender = function (str, data) {
// 对directive进行排序
sodaDirectiveArr.sort(function (b, a) {
return (Number(a.opt.priority || 0) - Number(b.opt.priority || 0));
});
//console.log(sodaDirectiveArr);
// 解æž<EFBFBD>模æ<EFBFBD>¿DOM
// 解析模板DOM
var div = document.createElement("div");
div.innerHTML = str;
[].map.call([].slice.call(div.childNodes, []), function(child){
[].map.call([].slice.call(div.childNodes, []), function (child) {
compileNode(child, data);
});
return div.innerHTML;
// var frament = document.createDocumentFragment();
// frament.innerHTML = div.innerHTML;
// var frament = document.createDocumentFragment();
// frament.innerHTML = div.innerHTML;
/*
frament.update = function(newData){
//checkingDirtyData(data, d);
var diff = DeepDiff.noConflict();
frament.update = function(newData){
//checkingDirtyData(data, d);
var diff = DeepDiff.noConflict();
var diffResult = diff(data, newData);
var diffResult = diff(data, newData);
console.log(diffResult);
console.log(diffResult);
var dirtyData = ['a'];
var dirtyData = ['a'];
for(var i = 0; i < dirtyData.length; i ++){
var item = dirtyData[i];
for(var i = 0; i < dirtyData.length; i ++){
var item = dirtyData[i];
var id = hashTable.expression2id[item];
var id = hashTable.expression2id[item];
var nowValue = parseSodaExpression(item, newData);
//console.log(nowValue);
var nowValue = parseSodaExpression(item, newData);
//console.log(nowValue);
if(id.el){
id.el.nodeValue = nowValue;
}
}
if(id.el){
id.el.nodeValue = nowValue;
}
}
console.log(hashTable);
console.log(hashTable);
};
*/
};
*/
var child;
while(child = div.childNodes[0]){
while (child = div.childNodes[0]) {
frament.appendChild(child);
}
return frament;
};
var eventPool = {};
sodaRender.addEventListener = function(type, func){
if(eventPool[type]){
}else{
sodaRender.addEventListener = function (type, func) {
if (eventPool[type]) {
} else {
eventPool[type] = [];
}
eventPool[type].push(func);
};
sodaRender.author = "dorsy";
var triggerEvent = function(type, e, data){
var triggerEvent = function (type, e, data) {
var events = eventPool[type] || [];
for(var i = 0; i < events.length; i ++){
for (var i = 0; i < events.length; i++) {
var eventFunc = events[i];
eventFunc && eventFunc(e, data);
}
@ -625,16 +622,36 @@
sodaRender.filter = sodaFilter;
if(typeof exports === 'object' && typeof module === 'object')
sodaRender.prefix = function (newPrefix) {
for(var key in sodaDirectiveMap){
if(sodaDirectiveMap.hasOwnProperty(key)){
sodaDirectiveMap[key.replace(prefix,newPrefix)] = sodaDirectiveMap[key];
delete sodaDirectiveMap[key];
}
}
var i = 0,
len = sodaDirectiveArr.length;
for(;i<len;i++) {
sodaDirectiveArr[i].name = sodaDirectiveArr[i].name.replace(prefix, newPrefix);
}
prefix = newPrefix
prefixReg = new RegExp('^' + prefix + '-')
}
if (typeof exports === 'object' && typeof module === 'object')
module.exports = sodaRender;
else if(typeof define === 'function' && define.amd)
define([], function(){
else if (typeof define === 'function' && define.amd)
define([], function () {
return sodaRender;
});
else if(typeof exports === 'object')
else if (typeof exports === 'object')
exports["soda"] = sodaRender;
else
window.soda = sodaRender;
// çå<E28098>¬æ•°æ<C2B0>®å¼å¸¸æƒ…况
// 监听数据异常情况
})();