Comparing version 0.0.0 to 0.0.1-pre0
190
core/app.js
var EventEmitter = require('events').EventEmitter; | ||
var inherits = require('inherits'); | ||
var dombie = require('dombie'); | ||
var http = require('http'); | ||
var render = require('./render'); | ||
var fetch = require('./fetch'); | ||
var Widget = require('../widgets').Widget; | ||
@@ -11,19 +13,2 @@ | ||
/// load the given file | ||
var fetch = function(path, cb) { | ||
// TODO(shtylman) I am sure there is some library doing this | ||
var xmlhttp = new XMLHttpRequest(); | ||
// TODO(shtylman) handle error | ||
xmlhttp.onreadystatechange=function() { | ||
if (xmlhttp.readyState==4 && xmlhttp.status==200) { | ||
return cb(null, xmlhttp.responseText); | ||
} | ||
} | ||
xmlhttp.open('GET', path, true); | ||
xmlhttp.send(); | ||
}; | ||
// load resources | ||
@@ -34,3 +19,2 @@ module.exports.init = function(opt) { | ||
var widgets = opt.widgets || {}; | ||
@@ -73,123 +57,113 @@ var files = opt.ui || {}; | ||
/// element or elements | ||
function wrap_element(element, ui) { | ||
var uiloader = this; | ||
module.exports.ui = function(name) { | ||
var ui = uis[name]; | ||
if (!ui) { | ||
throw new Error('no ui: ' + name); | ||
} | ||
if (Array.isArray(element)) { | ||
return function(parent) { | ||
if (parent instanceof Widget) { | ||
return ui(parent._elem); | ||
} | ||
// all of these will be run in order | ||
// to create the widget | ||
var funcs = []; | ||
return ui(parent); | ||
}; | ||
}; | ||
element.forEach(function(element) { | ||
funcs.push(wrap_element.bind(uiloader)(element, ui)); | ||
}); | ||
/// ui | ||
return function(widget) { | ||
// apply all of the loaded elements onto the widget | ||
funcs.forEach(function(func) { | ||
func(widget); | ||
}); | ||
var UiLoader = function(custom) { | ||
var self = this; | ||
self.custom_widgets = custom; | ||
}; | ||
return widget; | ||
}; | ||
} | ||
// return a function which can be called with a parent node | ||
// to create the dom | ||
function gen(parent, dom, custom_widgets, ui) { | ||
// document body is the parent if no parent specified | ||
parent = parent || document.body; | ||
switch (element.type) { | ||
case 'tag': | ||
return function(parent) { | ||
var maybe_class = element.attributes['data-widget']; | ||
var custom = uiloader.custom_widgets[maybe_class]; | ||
if (maybe_class && !custom) { | ||
console.error('no custom class for: ' + class_name); | ||
} | ||
var handler = function(parent, element, node) { | ||
if (custom) { | ||
var widget = new custom(parent); | ||
} | ||
else { | ||
var widget = new Widget(parent, element.name); | ||
} | ||
var maybe_class = element.attributes['data-widget']; | ||
var custom = custom_widgets[maybe_class]; | ||
if (maybe_class && !custom) { | ||
console.error('no custom class for: ' + maybe_class); | ||
} | ||
// apply attributes | ||
var widget_id = element.attributes['id']; | ||
var Class = custom || Widget; | ||
var widget = new Class(parent, node); | ||
// widgetid provides access to widget | ||
if (widget_id) { | ||
ui[widget_id] = widget; | ||
} | ||
// apply attributes | ||
var widget_id = element.attributes['id']; | ||
var attributes = element.attributes; | ||
Object.keys(attributes).forEach(function(name) { | ||
widget.attr(name, attributes[name]); | ||
}); | ||
// widgetid provides access to widget | ||
if (widget_id) { | ||
ui[widget_id] = widget; | ||
} | ||
element.children = element.children || []; | ||
// this widget is now the parent | ||
wrap_element.bind(uiloader)(element.children, ui)(widget); | ||
}; | ||
break; | ||
case 'text': | ||
return function(parent) { | ||
parent.text(element.data); | ||
}; | ||
break; | ||
case 'comment': | ||
break; | ||
return node; | ||
}; | ||
return function(parent) {}; | ||
}; | ||
dom.forEach(function(element) { | ||
// ui will contain .elements: [], .ids: {} | ||
render(element, parent, handler); | ||
}); | ||
module.exports.ui = function(name) { | ||
var ui = uis[name]; | ||
if (!ui) { | ||
throw new Error('no ui: ' + name); | ||
} | ||
return ui; | ||
}; | ||
/// ui | ||
var UiLoader = function(custom) { | ||
var self = this; | ||
self.custom_widgets = custom; | ||
}; | ||
var dombie = require('dombie'); | ||
UiLoader.prototype.load = function(name, content, cb) { | ||
var self = this; | ||
var custom_widgets = self.custom_widgets; | ||
// turn content into dom | ||
dombie(content, function(err, dom) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
// embedded ui elements are found within <script type="x-bamboo"> | ||
// and the name="" property will be the name | ||
var embedded_ui = {}; | ||
var setup = function(parent) { | ||
var ui = {}; | ||
var ui = { | ||
ui: embedded_ui | ||
}; | ||
// all of these will be run in order | ||
// to create the widget | ||
var funcs = []; | ||
return gen(parent, dom, custom_widgets, ui); | ||
}; | ||
dom.forEach(function(element) { | ||
funcs.push(wrap_element.bind(self)(element, ui)); | ||
}); | ||
// look for script tags in top level elements | ||
var count = 0; | ||
(function next() { | ||
if (count >= dom.length) { | ||
return cb(null, setup); | ||
} | ||
var widget = new Widget(parent); | ||
var element = dom[count++]; | ||
if (element.name === 'script' && | ||
element.attributes['type'] === 'x-bamboo') { | ||
// apply all of the loaded elements onto the widget | ||
funcs.forEach(function(func) { | ||
func(widget); | ||
}); | ||
return dombie(element.data, function(err, dom) { | ||
embedded_ui[element.attributes['name']] = function(parent) { | ||
var ui = {}; | ||
ui._ui = widget; | ||
var elem = parent; | ||
if (parent instanceof Widget) { | ||
elem = parent._elem; | ||
} | ||
return ui; | ||
}; | ||
return gen(elem, dom, custom_widgets, ui); | ||
}; | ||
cb(null, setup); | ||
next(); | ||
}); | ||
} | ||
next(); | ||
})(); | ||
}); | ||
}; | ||
{ | ||
"name": "bamboo", | ||
"version": "0.0.0", | ||
"version": "0.0.1-pre0", | ||
"description": "web app framework", | ||
@@ -13,4 +13,4 @@ "main": "index.js", | ||
"inherits": "1.0.0", | ||
"dombie": "0.0.0" | ||
"dombie": "0.0.1" | ||
} | ||
} |
var EventEmitter = require('events').EventEmitter; | ||
var inherits = require('inherits'); | ||
var Widget = function(parent, tag_name) { | ||
var Widget = function(parent, element) { | ||
var self = this; | ||
// if our widget wants to be something else, it should specify | ||
self.tag_name = tag_name || 'div'; | ||
element = element || document.createElement('div'); | ||
var elem = self._elem = element; | ||
var _elem = self._elem = self.create_element(); | ||
// TODO(shtylman) should have a top level parent widget | ||
// to do this | ||
if (!parent) { | ||
document.body.appendChild(_elem); | ||
return; | ||
} | ||
function bind_event(dom_name, emit_name) { | ||
_elem[dom_name] = function(ev) { | ||
elem[dom_name] = function(ev) { | ||
self.emit(emit_name, ev); | ||
@@ -29,2 +20,3 @@ }; | ||
bind_event('onkeypress', 'keypress'); | ||
bind_event('oncontextmenu', 'contextmenu'); | ||
@@ -38,5 +30,5 @@ // TODO(shtylman) context menu stuff | ||
// forms | ||
_elem.onsubmit = function(event) { | ||
// we don't submit our forms | ||
elem.onsubmit = function(event) { | ||
// prevent regular submitting | ||
// we are in the new era! | ||
event.preventDefault(); | ||
@@ -47,12 +39,17 @@ | ||
// we are now part of the parent in the dom | ||
parent.add_child(self); | ||
}; | ||
inherits(Widget, EventEmitter); | ||
if (!parent) { | ||
return; | ||
} | ||
Widget.prototype.create_element = function() { | ||
var self = this; | ||
// box the parent if it is not a widget | ||
if (!(parent instanceof Widget)) { | ||
parent.appendChild(elem); | ||
return; | ||
} | ||
return document.createElement(self.tag_name); | ||
if (!elem.parentNode) { | ||
parent.add_child(self); | ||
} | ||
}; | ||
inherits(Widget, EventEmitter); | ||
@@ -84,3 +81,3 @@ Widget.prototype.value = function(val) { | ||
self._elem.appendChild(document.createTextNode(text)); | ||
self._elem.textContent = text; | ||
@@ -90,2 +87,29 @@ return self; | ||
Widget.prototype.append_html = function(html) { | ||
var self = this; | ||
self._elem.insertAdjacentHTML('beforeend', html); | ||
return self; | ||
}; | ||
Widget.prototype.remove = function() { | ||
var self = this; | ||
self._elem.remove(); | ||
return self; | ||
}; | ||
Widget.prototype.empty = function() { | ||
var self = this; | ||
var elem = self._elem; | ||
while (elem.firstChild) { | ||
elem.removeChild(elem.firstChild); | ||
} | ||
return self; | ||
}; | ||
Widget.prototype.show = function() { | ||
@@ -97,4 +121,2 @@ var self = this; | ||
self.emit('show'); | ||
return self; | ||
@@ -101,0 +123,0 @@ }; |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
11238
14
359
0
+ Addeddombie@0.0.1(transitive)
- Removeddombie@0.0.0(transitive)
- Removedsax@0.4.2(transitive)
Updateddombie@0.0.1