omi v.1.4.0 - add omi soda version

This commit is contained in:
dntzhang 2017-04-18 12:02:29 +08:00
parent fe314212a0
commit 049c12602f
14 changed files with 6704 additions and 7 deletions

2
dist/omi.js vendored
View File

@ -1,5 +1,5 @@
/*!
* Omi v1.3.2 By dntzhang
* Omi v1.4.0 By dntzhang
* Github: https://github.com/AlloyTeam/omi
* MIT Licensed.
*/

2
dist/omi.lite.js vendored
View File

@ -1,5 +1,5 @@
/*!
* Omi v1.3.2 By dntzhang
* Omi v1.4.0 By dntzhang
* Github: https://github.com/AlloyTeam/omi
* MIT Licensed.
*/

View File

@ -1,5 +1,5 @@
/*!
* Omi v1.3.2 By dntzhang
* Omi v1.4.0 By dntzhang
* Github: https://github.com/AlloyTeam/omi
* MIT Licensed.
*/

2
dist/omi.min.js vendored
View File

@ -1,5 +1,5 @@
/*!
* Omi v1.3.2 By dntzhang
* Omi v1.4.0 By dntzhang
* Github: https://github.com/AlloyTeam/omi
* MIT Licensed.
*/

2977
dist/omi.soda.js vendored Normal file

File diff suppressed because it is too large Load Diff

7
dist/omi.soda.min.js vendored Normal file

File diff suppressed because one or more lines are too long

29
example/es5/soda.html Normal file
View File

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="../../dist/omi.soda.js"></script>
<script>
var List = Omi.create('List', {
render:function(){
return '<ul>\
<li soda-repeat="item in items" soda-if="item.show">\
{{$index}}- {{item.text}}\
</li>\
</ul>'
}
});
Omi.render(new List({
items: [
{ text: 'Omi', show: true},
{ text: 'dntzhang', show: true},
{ text: 'AlloyTeam'}
]
}),"body",true);
</script>
</body>
</html>

3004
example/soda/bundler.js Normal file

File diff suppressed because it is too large Load Diff

14
example/soda/index.html Normal file
View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="test"></div>
<a href="https://github.com/AlloyTeam/omi" target="_blank" style="position: absolute; right: 0; top: 0;">
<img src="../../asset/github.png" alt="" />
</a>
<script src="bundler.js"></script>
</body>
</html>

25
example/soda/main.js Normal file
View File

@ -0,0 +1,25 @@
import Omi from '../../src/index.soda.js';
class List extends Omi.Component {
constructor(data) {
super(data);
}
render(){
return `<ul>
<li soda-repeat="item in items" soda-if="item.show">
{{$index}}- {{item.text}}
</li>
</ul>`
}
}
Omi.render(new List({
items: [
{ text: 'Omi', show: true},
{ text: 'dntzhang', show: true},
{ text: 'AlloyTeam'}
]
}),"body",true);

View File

@ -1,6 +1,6 @@
{
"name": "omi",
"version": "1.3.2",
"version": "1.4.0",
"description": "Open and modern framework for building user interfaces.",
"main": "dist/omi.js",
"types": "index.d.ts",
@ -15,6 +15,7 @@
"textarea": "webpack -w",
"svg": "webpack -w",
"simple": "webpack -w",
"soda": "webpack -w",
"group-data": "webpack -w",
"jsx-like-event":"webpack -w",
"loop": "webpack -w",

15
src/index.soda.js Normal file
View File

@ -0,0 +1,15 @@
import Omi from './omi.js'
import soda from './soda.js'
import Component from './component.js'
import Store from './store.js'
Omi.template = soda
Omi.Store = Store
Omi.Component = Component
if(window.Omi){
module.exports = window.Omi
}else {
window.Omi = Omi
module.exports = Omi
}

623
src/soda.js Normal file
View File

@ -0,0 +1,623 @@
/**
* sodajs v0.4.0 by dorsywang
* Light weight but powerful template engine for JavaScript
* Github: https://github.com/AlloyTeam/sodajs
* MIT License
*/
;(function(){
var valueoutReg = /\{\{([^\}]*)\}\}/g;
var classNameRegExp = function(className) {
return new RegExp('(^|\\s+)' + className + '(\\s+|$)', 'g');
};
var addClass = function(el, className){
if(! el.className){
el.className = className;
return;
}
if(el.className.match(classNameRegExp(className))){
}else{
el.className += " " + className;
}
};
var removeClass = function(el, className){
el.className = el.className.replace(classNameRegExp(className), "");
};
var getValue = function(_data, _attrStr){
CONST_REGG.lastIndex = 0;
var realAttrStr = _attrStr.replace(CONST_REGG, function(r){
if(typeof _data[r] === "undefined"){
return r;
}else{
return _data[r];
}
});
if(_attrStr === 'true'){
return true;
}
if(_attrStr === 'false'){
return false;
}
var _getValue = function(data, attrStr){
var dotIndex = attrStr.indexOf(".");
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)){
attr = _data[attr];
}
if(typeof data[attr] !== "undefined"){
return _getValue(data[attr], attrStr);
}else{
var eventData = {
name: realAttrStr,
data: _data
};
triggerEvent("nullvalue", {
type: "nullattr",
data: eventData
}, eventData);
// 如果还有
return "";
}
}else{
// æ£?查attrStr是å<C2AF>¦å±žæ?§å<C2A7>˜é‡<C3A9>并转æ<C2AC>¢
if(typeof _data[attrStr] !== "undefined" && CONST_REG.test(attrStr)){
attrStr = _data[attrStr];
}
var rValue;
if(typeof data[attrStr] !== "undefined"){
rValue = data[attrStr];
}else{
var eventData = {
name: realAttrStr,
data: _data
};
triggerEvent("nullvalue", {
type: "nullvalue",
data: eventData
}, eventData);
rValue = '';
}
return rValue;
}
};
return _getValue(_data, _attrStr);
};
// 注释node
var commentNode = function(node){
};
// 标识�
var IDENTOR_REG = /[a-zA-Z_\$]+[\w\$]*/g;
var STRING_REG = /"([^"]*)"|'([^']*)'/g
var NUMBER_REG = /\d+|\d*\.\d+/g;
var OBJECT_REG = /[a-zA-Z_\$]+[\w\$]*(?:\s*\.\s*(?:[a-zA-Z_\$]+[\w\$]*|\d+))*/g;
var ATTR_REG = /\[([^\[\]]*)\]/g;
var ATTR_REG_DOT = /\.([a-zA-Z_\$]+[\w\$]*)/g;
var NOT_ATTR_REG = /[^\.|]([a-zA-Z_\$]+[\w\$]*)/g;
var OR_REG = /\|\|/g;
var OR_REPLACE = "OR_OPERATOR\x1E";
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 parseSodaExpression = function(str, scope){
// 对filterè¿è¡Œå¤„ç<E2809E>
str = str.replace(OR_REG, OR_REPLACE).split("|");
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);
// 将字符常é‡<C3A9>ä¿<C3A4>å­˜ä¸æ<E280B9>?
expr = expr.replace(STRING_REG, function(r, $1, $2){
var key = getRandom();
scope[key] = $1 || $2;
return key;
});
while(ATTR_REG.test(expr)){
ATTR_REG.lastIndex = 0;
//对expr预处ç<E2809E>?
expr = expr.replace(ATTR_REG, function(r, $1){
var key = getAttrVarKey();
// 属æ?§å<C2A7><C3A5>å­? 为字符常é‡?
var attrName = parseSodaExpression($1, scope);
// 给一个特殊的å‰<C3A5>ç¼€ 表示是属性å<C2A7>˜é‡?
scope[key] = attrName;
return "." + key;
});
}
expr = expr.replace(OBJECT_REG, function(value){
return "getValue(scope,'" + value.trim() + "')";
});
var parseFilter = function(){
var filterExpr = filters.shift();
if(! filterExpr){
return;
}
var filterExpr = filterExpr.split(":");
var args = filterExpr.slice(1) || [];
var name = filterExpr[0] || "";
var stringReg = /^'.*'$|^".*"$/;
for(var i = 0; i < args.length; i ++){
//这里根æ<C2B9>®ç±»åžè¿è¡Œåˆ¤æ­
if(OBJECT_REG.test(args[i])){
args[i] = "getValue(scope,'" + args[i] + "')";
}else{
}
}
if(sodaFilterMap[name]){
args.unshift(expr);
args = args.join(",");
expr = "sodaFilterMap['" + name + "'](" + args + ")";
}
parseFilter();
};
parseFilter();
var evalFunc = new Function("getValue", "sodaFilterMap", "return function sodaExp(scope){ return " + expr + "}")(getValue, sodaFilterMap);
return evalFunc(scope);
};
var hashTable = {
id2Expression: {
},
expression2id: {
},
getRandId: function(){
return 'soda' + ~~ (Math.random() * 1E5);
}
};
// 解æž<C3A6>指令
// 解æž<C3A6>attr
var compileNode = function(node, scope){
// å¦æžœå<C593>ªæ˜¯æ‡æœ¬
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
};
*/
return parseSodaExpression($1, scope);
});
}
if(node.attributes){
// 指令处ç<E2809E>
sodaDirectiveArr.map(function(item){
var name = item.name;
var opt = item.opt;
if(node.getAttribute(name) && node.parentNode){
opt.link(scope, node, node.attributes);
}
});
// 处ç<E2809E>†è¾“出 包å<E280A6>« soda-*
[].map.call(node.attributes, function(attr){
// å¦æžœdirctiveMap有的就跳过ä¸<C3A4>å†<C3A5>处ç<E2809E>?
if(! sodaDirectiveMap[attr.name]){
if(/^soda-/.test(attr.name)){
var attrName = attr.name.replace(/^soda-/, '');
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);
});
}
}
});
}
[].map.call([].slice.call(node.childNodes, []), function(child){
compileNode(child, scope);
});
};
var sodaDirectiveMap = {
};
var sodaFilterMap = {
};
var sodaDirectiveArr = [];
var sodaDirective = function(name, func){
var name = 'soda-' + name;
sodaDirectiveMap[name] = func();
sodaDirectiveArr.push({
name: name,
opt: sodaDirectiveMap[name]
});
};
var sodaFilter = function(name, func){
sodaFilterMap[name] = func;
};
sodaFilter.get = function(name){
return sodaFilterMap[name];
};
sodaFilter("date", function(input, lenth){
return lenth;
});
sodaDirective('repeat', function(){
return {
priority: 10,
compile: function(scope, el, attrs){
},
link: function(scope, el, attrs){
var opt = el.getAttribute('soda-repeat');
var itemName;
var valueName;
var trackReg = /\s+by\s+([^\s]+)$/;
var trackName;
opt = opt.replace(trackReg, function(item, $1){
if($1){
trackName = ($1 || '').trim();
}
return '';
});
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]){
itemName = (r[1] || '').trim();
valueName = (r[2] || '').trim();
if(! (itemName && valueName)){
return;
}
}else if(r[3] && r[4] && r[5]){
trackName = (r[3] || '').trim();
itemName = (r[4] || '').trim();
valueName = (r[5] || '').trim();
}
}else{
return;
}
trackName = trackName || '$index';
// 这里è¦<C3A8>处ç<E2809E>†ä¸€ä¸?
var repeatObj = getValue(scope, valueName) || [];
var repeatFunc = function(i){
var itemNode = el.cloneNode(true);
// 这里创建ä¸?个æ°çš„scope
var itemScope = {};
itemScope[trackName] = i;
itemScope[itemName] = repeatObj[i];
itemScope.__proto__ = scope;
itemNode.removeAttribute('soda-repeat');
el.parentNode.insertBefore(itemNode, el);
// 这里是æ°åŠ çš„dom, è¦<C3A8>å<EFBFBD>•ç¬ç¼è¯?
compileNode(itemNode, itemScope);
};
if('length' in repeatObj){
for(var i = 0; i < repeatObj.length; i ++){
repeatFunc(i);
}
}else{
for(var i in repeatObj){
if(repeatObj.hasOwnProperty(i)){
repeatFunc(i);
}
}
}
el.parentNode.removeChild(el);
}
};
});
sodaDirective('if', function(){
return {
priority: 9,
link: function(scope, el, attrs){
var opt = el.getAttribute('soda-if');
var expressFunc = parseSodaExpression(opt, scope);
if(expressFunc){
}else{
// el.setAttribute("removed", "removed");
el.parentNode && el.parentNode.removeChild(el);
}
}
};
});
sodaDirective('class', function(){
return {
link: function(scope, el, attrs){
var opt = el.getAttribute("soda-class");
var expressFunc = parseSodaExpression(opt, scope);
if(expressFunc){
addClass(el, expressFunc);
}else{
}
}
};
});
sodaDirective('src', function(){
return {
link: function(scope, el, attrs){
var opt = el.getAttribute("soda-src");
var expressFunc = opt.replace(valueoutReg, function(item, $1){
return parseSodaExpression($1, scope);
});
if(expressFunc){
el.setAttribute("src", expressFunc);
}else{
}
}
};
});
sodaDirective('bind-html', function(){
return {
link: function(scope, el, attrs){
var opt = el.getAttribute("soda-bind-html");
var expressFunc = parseSodaExpression(opt, scope);
if(expressFunc){
el.innerHTML = expressFunc;
return {
command: "childDone"
};
}
}
};
});
sodaDirective("style", function(){
return {
link: function(scope, el, attrs){
var opt = el.getAttribute("soda-style");
var expressFunc = parseSodaExpression(opt, scope);
var getCssValue = function(name, value){
var numberWithoutpx = /opacity|z-index/;
if(numberWithoutpx.test(name)){
return parseFloat(value);
}
if(isNaN(value)){
return value;
}else{
return value + "px";
}
};
if(expressFunc){
var stylelist = [];
for(var i in expressFunc){
if(expressFunc.hasOwnProperty(i)){
var provalue = getCssValue(i, expressFunc[i]);
stylelist.push([i, provalue].join(":"));
}
}
var style = el.style;
for(var i = 0; i < style.length; i ++){
var name = style[i];
if(expressFunc[name]){
}else{
stylelist.push([name, style[name]].join(":"));
}
}
var styleStr = stylelist.join(";");
el.setAttribute("style", styleStr);
}
}
};
});
var sodaRender = function(str, data){
// 对directiveè¿è¡ŒæŽåº<C3A5>
sodaDirectiveArr.sort(function(b, a){
return (Number(a.opt.priority || 0) - Number(b.opt.priority || 0));
});
//console.log(sodaDirectiveArr);
// 解æž<C3A6>模æ<C2A1>¿DOM
var div = document.createElement("div");
div.innerHTML = str;
[].map.call([].slice.call(div.childNodes, []), function(child){
compileNode(child, data);
});
return div.innerHTML;
// var frament = document.createDocumentFragment();
// 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);
};
*/
var child;
while(child = div.childNodes[0]){
frament.appendChild(child);
}
return frament;
};
var eventPool = {};
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 events = eventPool[type] || [];
for(var i = 0; i < events.length; i ++){
var eventFunc = events[i];
eventFunc && eventFunc(e, data);
}
};
sodaRender.filter = sodaFilter;
if(typeof exports === 'object' && typeof module === 'object')
module.exports = sodaRender;
else if(typeof define === 'function' && define.amd)
define([], function(){
return sodaRender;
});
else if(typeof exports === 'object')
exports["soda"] = sodaRender;
else
window.soda = sodaRender;
// çå<E28098>¬æ•°æ<C2B0>®å¼å¸¸æƒ…况
})();

View File

@ -45,7 +45,8 @@ if(ENV === 'build'||ENV === 'build-min'){
config = {
entry: {
omi: './src/index.js',
'omi.lite': './src/index.lite.js'
'omi.lite': './src/index.lite.js',
'omi.soda': './src/index.soda.js'
},
output: {
// path: __dirname,
@ -87,7 +88,8 @@ if(ENV === 'build'||ENV === 'build-min'){
config.plugins.push(new webpack.optimize.UglifyJsPlugin());
config.entry = {
'omi.min': './src/index.js',
'omi.lite.min': './src/index.lite.js'
'omi.lite.min': './src/index.lite.js',
'omi.soda.min': './src/index.soda.js'
};
}
}else if(ENV==='todomvc'){