Create Spark package. WIP

This commit is contained in:
Geoff Schmidt
2012-07-27 00:07:33 -07:00
parent 07029921bb
commit f61d15e3eb
2 changed files with 131 additions and 0 deletions

43
packages/spark/package.js Normal file
View File

@@ -0,0 +1,43 @@
Package.describe({
summary: "Toolkit for live-updating HTML interfaces",
internal: true
});
Package.on_use(function (api) {
api.use('livedata');
api.use(['underscore', 'session'], 'client');
// XXX Depends on jquery because we need a selector engine to resolve
// event maps. What would be nice is, if you've included jquery or
// zepto, use one of those; if not, ship our own copy of sizzle (but,
// you still want the event object normalization that jquery provides?)
api.use('jquery');
api.add_files('spark.js', 'client');
/*
api.add_files(['liveevents_w3c.js', 'liveevents_now3c.js'], 'client');
api.add_files(['liveevents.js'], 'client');
api.add_files(['livedocument.js'], 'client');
api.add_files(['liverange.js', 'liveui.js', 'innerhtml.js', 'patcher.js'],
'client');
*/
});
Package.on_test(function (api) {
/*
api.use(['tinytest', 'templating', 'htmljs']);
api.use(['liveui', 'test-helpers'], 'client');
api.add_files('form_responder.js', 'server');
api.add_files([
'livedocument_tests.js',
'liverange_test_helpers.js',
'liveui_tests.js',
'liveui_tests.html',
'liverange_tests.js',
'patcher_tests.js',
'liveevents_tests.js'
], 'client');
*/
});

88
packages/spark/spark.js Normal file
View File

@@ -0,0 +1,88 @@
// XXX figure out good liverange tag names. should they be symbolic constants?
// in liverange-land they should probably start with "_"?
Spark = Spark || {};
Spark._currentRenderer = new Meteor.EnvironmentVariable;
Spark._Renderer = function () {
// Map from annotation ID to an annotation function, which is called
// at render time and receives (startNode, endNode.)
this.annotations = {};
};
_.extend(Spark._Renderer.prototype, {
createId: function () {
var id = "";
var chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for (var i = 0; i < 8; i++) {
id += hexDigits.substr(Math.floor(Meteor.random() * 0x10), 1);
}
return id;
},
// what can be a function that takes a LiveRange, or just a set of
// attributes to add to the liverange.
annotate: function (html, tag, what) {
var id = tag + "-" + this.createId();
this.annotations[id] = function (start, end) {
var range = new Meteor.ui._LiveRange(tag, start, end);
if (what instanceof Function)
what(range);
else
_.extend(range, what);
}
return "<$" + id + ">" + html + "</$" + id + ">";
}
});
Spark.render = function (htmlFunc) {
var renderer = new Spark.Renderer;
var html = Spark.currentRenderer.withValue(renderer, function () {
return Spark.barrier(htmlFunc);
});
// XXX turn html into DOM and attach liveranges
// HERE
//
// - Move LiveRange to global scope
// - Create DomUtils package (or something like that)
// - First thing in DomUtils is htmlToFragment from innerhtml.js
// - Later, will add stuff from domutils.js
// - _rangeToHtml will go in LiveRange (possibly the test helpers)
};
Spark.setContext = function (html, context) {
var renderer = Spark._currentRenderer.get();
if (!renderer)
return html;
return renderer.annotate(html, "_context", { context: context });
};
Spark.getContext = function (node) {
var range = Meteor.ui._LiveRange.findRange("_context", node);
return range && range.context;
}
Spark.barrier = function (htmlFunc) {
var renderer = Spark._currentRenderer.get();
if (!renderer)
return htmlFunc();
var ctx = new Meteor.deps.Context;
var html =
renderer.annotate(ctx.run(htmlFunc), "_barrier", function (range) {
ctx.on_invalidate(function () {
// XXX update with patching
});
});
return html;
};