hot as a meta doll

This commit is contained in:
Mark Nadal 2019-07-04 16:40:22 -07:00
parent e52496ae76
commit 0c718c8658
5 changed files with 234 additions and 6 deletions

View File

@ -208,6 +208,8 @@ To quickly spin up a Gun test server for your development team, utilize either [
[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/amark/gun)
> Heroku deletes your data every 15 minutes, one way to fix this is by adding [cheap storage](https://gun.eco/docs/Using-Amazon-S3-for-Storage).
Or:
```bash

95
as.js
View File

@ -1,7 +1,73 @@
;(function(){
function as(el, gun, cb){
function as(el, gun, cb, opt){
el = $(el);
if(gun === as.gui && as.el && as.el.is(el)){ return }
opt = opt || {};
opt.match = opt.match || '{{ ';
opt.end = opt.end || ' }}';
;(function(){ // experimental
function nest(t, s,e, r, i,tmp,u){
if(r && !r.length){ return t||'' }
if(!t){ return [] }
e = e || s;
i = t.indexOf(s, i||0);
if(0 > i){ return [] }
tmp = t.indexOf(e, i+1);
if(!r){ return [t.slice(i+s.length, tmp)].concat(nest(t, s,e, r, tmp,tmp,u)) }
return t.slice(0,i)+r[0]+nest(t.slice(tmp+e.length), s,e, r.slice(1), 0,tmp,u);
}
/* experimental */
function template(tag, attr){
var html = (tag = $(tag))[0].outerHTML, sub, tmp;
if(html && (0 > html.indexOf(opt.match))){ return }
if(!attr){
$.each(tag[0].attributes, function(i,v){
if(!v){ return }
if(!nest(v.value, opt.match, opt.end).length){ return }
template(tag, v.name)
});
if((sub = tag.children()).length){
return sub.each(function(){ template(this) });
}
}
var data = [], plate = attr? tag.attr(attr) : tag.html();
tmp = nest(plate, opt.match, opt.end);
if(!tmp.length){ return }
$.each(tmp, function(pos, match){
var expr = match.split(' ');
var path = (expr[0]).split('.');
if(expr = expr.slice(1).join(' ')){
expr = new Function("_", "b", "return (_)" + expr);
}
var val = (expr && expr('')) || '';
data.push(val);
if(!attr){ tag.text(val) }
var ref = gun, sup = [], tmp;
if(tmp = tag.attr('name')){ sup.push(tmp) }
tag.parents("[name]").each(function(){
sup.push($(this).attr('name'));
});
$.each(path = sup.reverse().concat(path), function(i,v){
ref = ref.get(v);
});
ref.on(function(v){
v = data[pos] = expr? expr(v) : v;
var tmp = nest(plate, opt.match, opt.end, data);
if(attr){
tag.attr(attr, tmp);
} else {
tag.text(tmp);
}
});
});
}
template(el);
}());
as.gui = gun;
as.el = el;
if(el.data('as')){
@ -62,7 +128,9 @@
tmp? up.insertAfter(tmp) : up.prependTo(up.parent());
}
if(as.lock === gui){ return }
(ui[0] && u === ui[0].value)? ui.text(data) : ui.val(data);
if(!(data && data instanceof Object)){
(ui[0] && u === ui[0].value)? ui.text(data) : ui.val(data);
}
ui.data('was', data);
if(cb){
cb(data, key, ui);
@ -89,7 +157,7 @@
as.lock = g;
g.put(data);
}, 99));
$(document).on('submit', 'form', function(e){ e.preventDefault() });
//$(document).on('submit', 'form', function(e){ e.preventDefault() });
var u;
window.as = as;
$.as = as;
@ -141,4 +209,23 @@
;$(function(){
$('.page').not(':first').hide();
$.as.route(location.hash.slice(1));
});
var opt = window.CONFIG || {};
opt.peers = opt.peers || ($('body').attr('peers') || (function(){
(console.warn || console.log)('Warning: No peer provided, defaulting to DEMO peer. Do not run in production, or your data will be regularly wiped, reset, or deleted. For more info, check https://github.com/eraeco/joydb#peers !');
return 'https://gunjs.herokuapp.com/gun';
}())).replace(', ', ',').split(',');
$.as(document, window.gun = window.gun || Gun(opt), null, opt);
var joy = window.JOY = function(){};
joy.auth = function(a,b,cb,o){
if(!o){ o = cb ; cb = 0 }
if(o === true){
gun.user().create(a, b);
return;
}
gun.user().auth(a,b, cb,o);
}
gun.on('auth', function(ack){
console.log("Your namespace is", ack.soul);
})
});

44
lib/doll.js Normal file
View File

@ -0,0 +1,44 @@
;(function(){ // jQuery shim
if(window.$){ return }
(($ = window.$ = function(q, tag){
if(!(this instanceof $)){ return new $(q, tag) }
this.tags = (q && q.tags) || (('string' != typeof q)?
(q?[q]:[]) : (tag||document).querySelectorAll(q));
return this;
}).fn = $.prototype).each = function(cb){ return $.each(this.tags, cb), this }
$.each = function(o, cb){ Object.keys(o).forEach(function(k){ cb(k, o[k]) }) }
$.fn.get = function(i, l, u){ return l = this.tags, (i === u)? l : l[i] }
$.fn.on = function(eve, cb){ return this.each(function(i, tag){ tag.addEventListener(eve, cb) })}
$.fn.is = function(q, b){ return this.each(function(i, tag){ b = b || tag.matches(q) }), b }
$.fn.css = function(obj){ return this.each(function(i, tag){ $.each(obj, function(k,v){ tag.style[k] = v }) })}
$.fn.text = function(text, key, f, u){
text = (text === u)? '' : (f = 1) && text;
key = key || 'textContent';
this.each(function(i, tag){
if(f){ tag[key] = text }
else { text += (tag[key]||'') }
});
return f? this : text;
}
$.fn.html = function(html){ return this.text(html, 'innerHTML') }
$.fn.find = function(q){
var I = $(), l = I.tags;
this.each(function(i, tag){
$(q, tag).each(function(i, tag){
if(0 > l.indexOf(tag)){ l.push(tag) }
});
});
return I;
}
$.fn.add = function(html, div){
(div = document.createElement('div')).innerHTML = html;
this.tags = [].slice.call(this.tags).concat([].slice.call(div.childNodes));
return this;
}
$.fn.append = function(html, op, f){ return this.each(function(i, tag){
(('<' === html[0])? $().add(html) : $(html)).each(function(i, node){
(f? node : tag)[op || 'appendChild'](f? tag : node);
})
})}
$.fn.appendTo = function(html){ return this.append(html, 0, 1) }
}());

95
lib/hot.js Normal file
View File

@ -0,0 +1,95 @@
;(function(){
// on fires when shortcut keydowns or on touch after command selected and then touchdown
var m = meta;
m.edit({name: "Add", combo: ['A']});
m.edit({name: "Row", combo: ['A', 'R'],
on: function(eve){
m.tap().append('<div class="hold center" style="min-height: 9em; padding: 2%;">');
}
});
m.edit({name: "Columns", combo: ['A','C'],
on: function(eve){
var on = m.tap(), tmp, c;
var html = '<div class="unit col" style="min-height: 9em; padding: 2%;"></div>';
if(!on.children('.col').length){ html += html }
c = (tmp = on.append(html).children('.col')).length;
tmp.each(function(){
$(this).css('width', (100/c)+'%');
})
}
});
m.edit({name: "Text", combo: ['A','T'],
on: function(eve){
m.tap().append('<p contenteditable="true">Text</p>');
}
});
m.edit({name: "Drag", combo: ['D']});
;(function(){
$(document).on('click', function(){
var tmp = $('.m-on');
if(!tmp.length){ return }
tmp.removeClass('m-on');
})
m.edit({combo: [38], // up
on: function(eve){
var on = m.tap().removeClass('m-on');
on = on.prev().or(on.parent()).or(on);
on.addClass('m-on');
}, up: function(){
}
});
m.edit({combo: [40], // down
on: function(eve){
var on = m.tap().removeClass('m-on');
on = on.next().or(on.children().first()).or(on);
on.addClass('m-on');
}, up: function(){
}
});
m.edit({combo: [39], // right
on: function(eve){
var on = m.tap().removeClass('m-on');
on = on.children().first().or(on.next()).or(on.parent()).or(on);
on.addClass('m-on');
}, up: function(){
}
});
m.edit({combo: [37], // left
on: function(eve){
var on = m.tap().removeClass('m-on');
on = on.parent().or(on);
on.addClass('m-on');
}, up: function(){
}
});
}());
m.edit({name: "Turn", combo: ['T']});
m.edit({name: "Size", combo: ['S']});
m.edit({name: "X", combo: ['S','X'],
on: function(eve){
var on = m.tap(), was = on.width();
$(document).on('mousemove.tmp', function(eve){
var be = was + ((eve.pageX||0) - was);
on.css({'max-width': be, width: '100%'});
})
}, up: function(){ $(document).off('mousemove.tmp') }
});
m.edit({name: "Y", combo: ['S','Y'],
on: function(eve){
var on = m.tap(), was = on.height();
$(document).on('mousemove.tmp', function(eve){
var be = was + ((eve.pageY||0) - was);
on.css({'min-height': be});
})
}, up: function(){ $(document).off('mousemove.tmp') }
});
m.edit({name: "Fill", combo: ['F'],
on: function(eve){
var on = m.tap();
m.ask('Color name, code, or URL?', function(color){
var css = on.closest('p').length? 'color' : 'background';
on.css(css, color);
});
}
});
}());

View File

@ -1,4 +1,4 @@
$(function(){
;(function(){
var noop = function(){}, u;
var m = window.meta = {edit:[]};
var k = m.key = {};
@ -380,4 +380,4 @@ $(function(){
up: function(){}
});
}());
});
}());