Commit e15162cf by talequale

merge

parents 05ea816e e4732f08
...@@ -113,6 +113,9 @@ NS.apply = function(a,b) { ...@@ -113,6 +113,9 @@ NS.apply = function(a,b) {
if(typeof type === 'function'){ if(typeof type === 'function'){
return type(cfg, [].slice.call(arguments, 2)); return type(cfg, [].slice.call(arguments, 2));
} }
if(typeof type === 'object' && type.hook){
return type.hook(cfg, [].slice.call(arguments, 2));
}
cfg = cfg || {}; cfg = cfg || {};
var cls = cfg.cls || cfg['class'] || cfg.className, var cls = cfg.cls || cfg['class'] || cfg.className,
style = cfg.style, style = cfg.style,
...@@ -143,6 +146,8 @@ NS.apply = function(a,b) { ...@@ -143,6 +146,8 @@ NS.apply = function(a,b) {
if( cls ){ if( cls ){
if(typeof cls === 'function'){ if(typeof cls === 'function'){
cls(setters.cls(el)); cls(setters.cls(el));
}else if(typeof cls === 'object' && cls.hook){
return cls.hook(setters.cls(el));
}else{ }else{
setters.cls(el)(cls); setters.cls(el)(cls);
} }
...@@ -156,6 +161,8 @@ NS.apply = function(a,b) { ...@@ -156,6 +161,8 @@ NS.apply = function(a,b) {
if(attr.hasOwnProperty( i )){ if(attr.hasOwnProperty( i )){
if( typeof attr[ i ] === 'function' ){ if( typeof attr[ i ] === 'function' ){
attr[ i ]( setters.attr( el, i ) ); attr[ i ]( setters.attr( el, i ) );
}else if(typeof attr[ i ] === 'object' && attr[ i ].hook){
return attr[ i ].hook(setters.attr(el, i));
}else{ }else{
setters.attr( el, i )( attr[ i ] ); setters.attr( el, i )( attr[ i ] );
} }
...@@ -239,6 +246,7 @@ NS.apply = function(a,b) { ...@@ -239,6 +246,7 @@ NS.apply = function(a,b) {
return ; return ;
} }
if( type !== 'object' ){ if( type !== 'object' ){
// TODO : add hook
if( type === 'function' ){ if( type === 'function' ){
//var tmp = D.Text();//( {cls: 'zero-wrapper'} ); //var tmp = D.Text();//( {cls: 'zero-wrapper'} );
//el.appendChild( tmp ); //el.appendChild( tmp );
...@@ -278,11 +286,11 @@ NS.apply = function(a,b) { ...@@ -278,11 +286,11 @@ NS.apply = function(a,b) {
var dpID = 1; var dpID = 1;
var DataPiece = function(id){this.id = id;}; var DataPiece = function(id){this.id = id;};
DataPiece.prototype = {value: void 0, update: function(){}}; DataPiece.prototype = {value: void 0, update: function(){}};
var DataPieceFactory = function(refs, fn) { var DataPieceFactory = function(refs, fn, scope) {
var id = dpID++; var id = dpID++;
var dp = new DataPiece(id); var dp = new DataPiece(id);
refs.push(dp); refs.push(dp);
fn(function(val) { fn.call(scope, function(val) {
dp.value = val; dp.value = val;
dp.update(); dp.update();
}); });
...@@ -312,7 +320,9 @@ NS.apply = function(a,b) { ...@@ -312,7 +320,9 @@ NS.apply = function(a,b) {
out.push( token ); out.push( token );
}else if(typeof token === 'object'){ }else if(typeof token === 'object'){
if(token instanceof DataPiece){ if(token instanceof DataPiece){
token.value && out.push(token.value); token.value && out.push( token.value );
}else if ( token.hook ){
args[i] = DataPieceFactory(refs, token.hook, token);
}else if(Array.isArray(token)){ }else if(Array.isArray(token)){
tmp = D._cls(token, refs, depth+1); tmp = D._cls(token, refs, depth+1);
// TODO check for push tmp // TODO check for push tmp
...@@ -323,6 +333,8 @@ NS.apply = function(a,b) { ...@@ -323,6 +333,8 @@ NS.apply = function(a,b) {
token[key].value && out.push(key); token[key].value && out.push(key);
}else if(typeof token[key] === 'function'){ }else if(typeof token[key] === 'function'){
token[ key ] = DataPieceFactory(refs, token[ key ]); token[ key ] = DataPieceFactory(refs, token[ key ]);
}else if(typeof token[key] === 'object' && token[key].hook){
token[key] = DataPieceFactory(refs, token[ key ].hook, token[key])
}else{ }else{
token[ key ] && out.push( key ); token[ key ] && out.push( key );
} }
......
...@@ -33,7 +33,7 @@ async function subscribe() { ...@@ -33,7 +33,7 @@ async function subscribe() {
}); });
}catch(e){ }catch(e){
console.error(e) console.error(e)
} }
//showMessage(message); //showMessage(message);
// Call subscribe() again to get the next message // Call subscribe() again to get the next message
......
...@@ -190,11 +190,16 @@ Store.AGGREGATE = function(fn) { ...@@ -190,11 +190,16 @@ Store.AGGREGATE = function(fn) {
}; };
for(let i = 0; i < _i; i++){ for(let i = 0; i < _i; i++){
// set backward callback // set backward callback
args[i]( val => { let hook = val => {
// update item corresponding value and check condition // update item corresponding value and check condition
vals[i] = val; vals[ i ] = val;
check(); check();
}) };
if(args[i] instanceof HookPrototype){
args[i].hook(hook)
}else{
args[ i ]( hook )
}
} }
}; };
...@@ -220,12 +225,64 @@ Store.OR = Store.AGGREGATE(function(values, length) { ...@@ -220,12 +225,64 @@ Store.OR = Store.AGGREGATE(function(values, length) {
Store.IF = function(cfg, children){ Store.IF = function(cfg, children){
return function( update ){ return function( update ){
if( 'condition' in cfg ) if( 'condition' in cfg )
cfg.condition( function( cond ){ var hook = function( cond ){
update( cond ? children : null ); update( cond ? children : null );
} ); };
if(cfg.condition instanceof HookPrototype){
cfg.condition.hook( hook );
}else{
cfg.condition( hook );
}
} }
}; };
Store.NOT = Store.AGGREGATE(function(values, length) { Store.NOT = Store.AGGREGATE(function(values, length) {
return !values[0]; return !values[0];
}); });
var HookPrototype = function() {};
HookPrototype.prototype = {
setter: function(val) { return val; },
//getter: function(val) { return val; },
set: function(val) {
val = this.setter(val);
var oldVal = this.get();
if(!this.equal(oldVal, val)){
this.data = val;
this._emit(oldVal, val);
}
},
equal: function(oldVal, newVal){
return newVal === oldVal;
},
get: function() {
return this.data;
},
hook: function(fn) { this.subscribers.push(fn); fn(this.get()); },
_emit: function(oldVal, val) {
var subscribers = this.subscribers;
for( var i = 0, _i = subscribers.length; i < _i; i++ ){
subscribers[ i ](val);
}
}
};
var HookFactory = function(accessor) {
var Hook = function(cfg) {
this.data = {};
this.subscribers = [];
this.set(cfg);
};
Hook.prototype = Object.assign( new HookPrototype(), accessor );
return Hook;
};
Store.Value = {
Boolean: new HookFactory({
setter: function(val) { return !!val; },
toggle: function() { this.set(!this.get()); }
})
};
Store.HookPrototype = HookPrototype;
typeof module === 'object' && (module.exports = Store); typeof module === 'object' && (module.exports = Store);
const store = new Store({ let latest;
const store = new Store(latest = {
commit: window.CommitID, commit: window.CommitID,
'navigation': { 'navigation': {
current: 'login' current: 'Login'
}, },
'account': { 'account': {
userID: 5, userID: 5,
...@@ -16,19 +17,27 @@ const store = new Store({ ...@@ -16,19 +17,27 @@ const store = new Store({
try{ try{
var data = JSON.parse( localStorage.getItem( 'store' ) ); var data = JSON.parse( localStorage.getItem( 'store' ) );
window.addEventListener && console.log(data) window.addEventListener && console.log(data)
if(window.CommitID !== store.get('commit')){ if(data.commit !== store.get('commit')){
console.warn('STORE: warn outdated:'); console.warn('STORE: warn outdated:');
console.warn('\t\tCurrent data: '+ store.get('commit'), store._props); console.warn('\t\tCurrent data: '+ store.get('commit'), store._props);
console.warn('\t\tSaved data: '+ window.CommitID, data); console.warn('\t\tSaved data: '+ window.CommitID, data);
console.warn('\tRun restoreStore() for restore saved state'); console.warn('\tRun Store.restore() for restore saved state');
window.restoreStore = function() { console.warn('\tRun Store.update() for load latest default state');
Store.restore = function() {
for( var k in data ){ for( var k in data ){
if(k !== 'commit'){ if(k !== 'commit'){
store.set( k, data[ k ] ); store.set( k, data[ k ] );
} }
} }
}; };
Store.update = function() {
for( var k in latest ){
if(k !== 'commit'){
store.set( k, latest[ k ] );
}
}
};
}else{ }else{
for( var k in data ){ for( var k in data ){
store.set( k, data[ k ] ); store.set( k, data[ k ] );
......
...@@ -7,16 +7,22 @@ import { Page } from "../../page/Page"; ...@@ -7,16 +7,22 @@ import { Page } from "../../page/Page";
const {IF, NOT} = Store; const {IF, NOT} = Store;
export default D.declare('view.block.Header', () => { export default D.declare('view.block.Header', () => {
const tempPageMenuHidden = new Store.Value.Boolean(true);
return <header class={D.cls( return <header class={D.cls(
"page-header", { "page-header", {
"page-header--inner": (NOT(store.valEqual( 'navigation.current', 'login'))) "page-header--inner": (NOT(store.valEqual( 'navigation.current', 'login')))
})} > })} >
<div class="button-temp"> <div class="button-temp">
{ <button type={"button"} onClick={()=>tempPageMenuHidden.toggle()}>
<IF condition={tempPageMenuHidden}>+</IF>
<IF condition={NOT(tempPageMenuHidden)}></IF>
</button>
<div cls={D.cls({'hidden': tempPageMenuHidden})}>{
Object.keys(Page).map(name=> Object.keys(Page).map(name=>
<button type={'button'} onClick={() => store.set('navigation.current', name)}>{name}</button> <button class="temp-button" type={'button'} onClick={() => store.set('navigation.current', name)}>{name}</button>
) )
} }</div>
</div> </div>
<IF condition={NOT(store.valEqual( 'navigation.current', 'login'))}> <IF condition={NOT(store.valEqual( 'navigation.current', 'login'))}>
<div className="page-header__wrapper"> <div className="page-header__wrapper">
......
...@@ -130,4 +130,14 @@ ...@@ -130,4 +130,14 @@
top: 0; top: 0;
left: 0; left: 0;
z-index: 1; z-index: 1;
.hidden {
display: none;
}
.temp-button {
display: block;
margin: 10px;
width: 160px;
padding: 8px 0;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment