diff --git a/README.md b/README.md index d4904904..df341e7a 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,8 @@ To quickly spin up a Gun test server for your development team, utilize either [ [](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 diff --git a/as.js b/as.js index 0ce5b273..2609f974 100644 --- a/as.js +++ b/as.js @@ -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); + }) +}); \ No newline at end of file diff --git a/lib/doll.js b/lib/doll.js new file mode 100644 index 00000000..2a1b7f69 --- /dev/null +++ b/lib/doll.js @@ -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) } +}()); \ No newline at end of file diff --git a/lib/hot.js b/lib/hot.js new file mode 100644 index 00000000..794bbc57 --- /dev/null +++ b/lib/hot.js @@ -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('
Text
'); + } + }); + 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); + }); + } + }); +}()); \ No newline at end of file diff --git a/lib/meta.js b/lib/meta.js index a3337c67..c9fa3f9b 100644 --- a/lib/meta.js +++ b/lib/meta.js @@ -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(){} }); }()); -}); \ No newline at end of file +}()); \ No newline at end of file