Commit 8ef62f12 by Иван Кубота

Add TextArea component, add Store persistent storage

parent c9ee2023
......@@ -151,6 +151,36 @@
D.h('span', {cls: "cmp-color--title"}, cfg.label)
: null)
);
});
D.Field.TextArea = D.declare('Field.TextArea', (cfg)=> {
let changeHandler = cfg.onchange || cfg.onclick;
var input = D.h('textarea', {cls:"text--input", type:"text", "aria-label":"Change value"});
if(cfg.bind){
cfg.bind.hook(val => {
input.value = val || '';
changeHandler && changeHandler(val || '');
});
};
var change = Store.debounce(function(e){
var val = input.value;
cfg.bind.set(val)
},5);
'input,change,click,mouseup'.split(',')
.map(a=>a.trim())
.forEach(evt => input.addEventListener(evt, change));
return D.h('label', {cls: clsBuilder("cmp-textarea", cfg), title: cfg.alt || "Change text", renderTo: cfg.renderTo},
(cfg.label ?
D.h('span', {cls: "cmp-text--title"}, cfg.label)
: null),
input
);
});
})();
\ No newline at end of file
......@@ -574,8 +574,11 @@ Store.SaveInterface.LocalStorage.prototype = {
var data;
try{
data = JSON.parse( localStorage.getItem( this.key ) );
}catch(e){}
cb && cb(false, data || {});
}catch(e){
cb && cb(true, {});
}
return data;
},
// Should support full save if arguments.length === 0
......@@ -584,7 +587,34 @@ Store.SaveInterface.LocalStorage.prototype = {
}
};
const StoreBinding = function(store, key){
let _saveID = 1;
Store.Persistent = function(data, key, storeInterface){
storeInterface = storeInterface || Store.SaveInterface.LocalStorage;
key = key || 'persistent_'+_saveID++;
var fakeStore = {_props: {}};
var ifaceInstance = new storeInterface(key, fakeStore);
ifaceInstance.load(function(err, loadedData){
var shouldSave = false;
if(err){
if(typeof data !== 'object'){
throw new Error('Persistent data is not an object');
}
shouldSave = true;
}else{
for(var k in loadedData){
data[k].set(loadedData[k]);
}
}
Store.sub(data, function(data){
for(var k in data){
fakeStore._props[k] = data[k];
}
ifaceInstance.save();
}, !shouldSave);
});
}
var StoreBinding = function(store, key){
this.store = store;
this.key = key;
};
......
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