Permalink
Browse files

Added template functionality

  • Loading branch information...
1 parent 17b8ba2 commit f3dc7a7abc09eb0e8ca48e6924a0944259f6bb19 @DamonOehlman committed Mar 12, 2012
Showing with 73 additions and 31 deletions.
  1. +2 −26 examples/template.html
  2. +23 −2 fui.js
  3. +1 −1 fui.min.js
  4. +23 −2 src/fui.js
  5. +24 −0 templates/doodle.js
@@ -7,32 +7,8 @@
</style>
<script src="https://raw.github.com/ded/qwery/master/qwery.min.js"></script>
<script src="../fui.js"></script>
+ <script src="../templates/doodle.js"></script>
<script>
- var doodler = fui.template()
- .filter('canvas')
- .relative()
- .each(function(target) {
- this.args.unshift(target.getContext('2d'));
- })
- .down(function(context, target, x, y) {
- this.state.down = true;
-
- context.beginPath();
- context.moveTo(x, y);
- })
- .move(function(context, target, x, y) {
- if (this.lastTarget && target !== this.lastTarget) {
- context.moveTo(x, y);
- }
- else if (this.state.down) {
- context.lineTo(x, y);
- context.stroke();
- }
- })
- .up(function(context, target, x, y) {
- this.state.down = false;
- });
-
- fui().pipe(doodler);
+ fui().pipe(fui.templates.doodle);
</script>
</body>
View
@@ -168,12 +168,33 @@
// initialise the different source handlers
fui.sources = {};
+ // initialise the templates hash
+ fui.templates = {};
+
// create a template function, which is useful for creating chains not automatically bound to an event source
- fui.template = function(opts) {
+ fui.define = function(name, opts) {
+ var chain;
+
+ // if we don't have a name specified, but have been passed options
+ // then remap arguments
+ if (typeof name == 'object' && (! name instanceof String)) {
+ opts = name;
+ name = '';
+ }
+
+ // initialise options
opts = opts || {};
opts.source = 'none';
- return fui(opts);
+ // create a chain
+ chain = fui(opts);
+
+ // if we have a name specified, then add to the template
+ if (name) {
+ fui.templates[name] = chain;
+ }
+
+ return chain;
};
function EventSource() {
View
@@ -1 +1 @@
-(function(a){function g(a){this._index=0,this._relative=!1,this._steps=[],this.state={},this.lastTarget}function h(a){var d=b[b.length]=new g(a),e;a=a||{},a.source=a.source||("ontouchstart"in window?"touch":"mouse"),e=c[a.souce];if(!e){var f=h.sources[a.source];f&&(e=c[a.source]=new f(document))}return e&&e.add(d),d}function i(){this.chains=[]}var b=[],c={},d=typeof window.attachEvent!="undefined",e=d?function(a,b,c){a.attachEvent("on"+b,c)}:function(a,b,c){a.addEventListener(b,c,!0)},f=d?function(a,b,c){a.detachEvent("on"+b,c)}:function(a,b,c){a.removeEventListener(b,c,!0)};g.prototype={_next:function(a){var b=this._steps[this._index++];if(typeof b=="function"){var c=b.apply(this,this.args);return typeof c=="undefined"||c}return!1},_on:function(a,b){return this._step(function(){if(this.event.name===a)return b.apply(this,arguments)})},_step:function(a){return this._steps[this._steps.length]=a,this},each:function(a){return this._step(a)},filter:function(a){return this._step(function(b){return qwery.is(b,a)})},relative:function(){return this._step(function(a,b,c){if(!this._relative){if(a.offsetParent)do b-=a.offsetLeft,c-=a.offsetTop,a=a.offsetParent;while(a);this.args=this.args.slice(0,1).concat([b,c]),this._relative=!0}})},pipe:function(a){return this._step(function(){a.process(this.event)})},process:function(a){var b;this._index=0,this._relative=!1,this.event=a,this.args=[a.target,a.pageX,a.pageY];do b=this._next(a);while(b);return this.lastTarget=a.target,b}},["down","up","move"].forEach(function(a){g.prototype[a]=function(b){return this._on(a,b)}}),h.sources={},h.template=function(a){return a=a||{},a.source="none",h(a)},i.prototype={_createEvent:function(a,b){return{name:a,target:b.target||b.sourceElement,pageX:b.pageX,pageY:b.pageY,original:b}},add:function(a){this.chains.push(a)},handle:function(a){var b=this;return function(c){var d=b._createEvent(a,c);for(var e=b.chains.length;e--;)b.chains[e].process(d)}}};var j=h.sources.mouse=function(a){i.call(this,a),this._bindEvents(a)};j.prototype=new i,j.prototype.constructor=j,j.prototype._bindEvents=function(a){var b=this;e(a,"mousedown",b.handle("down")),e(a,"mousemove",b.handle("move")),e(a,"mouseup",b.handle("up"))},typeof module!="undefined"&&module.exports?module.exports=h:typeof define!="undefined"?define("fui",[],function(){return h}):a.fui=h})(this)
+(function(a){function g(a){this._index=0,this._relative=!1,this._steps=[],this.state={},this.lastTarget}function h(a){var d=b[b.length]=new g(a),e;a=a||{},a.source=a.source||("ontouchstart"in window?"touch":"mouse"),e=c[a.souce];if(!e){var f=h.sources[a.source];f&&(e=c[a.source]=new f(document))}return e&&e.add(d),d}function i(){this.chains=[]}var b=[],c={},d=typeof window.attachEvent!="undefined",e=d?function(a,b,c){a.attachEvent("on"+b,c)}:function(a,b,c){a.addEventListener(b,c,!0)},f=d?function(a,b,c){a.detachEvent("on"+b,c)}:function(a,b,c){a.removeEventListener(b,c,!0)};g.prototype={_next:function(a){var b=this._steps[this._index++];if(typeof b=="function"){var c=b.apply(this,this.args);return typeof c=="undefined"||c}return!1},_on:function(a,b){return this._step(function(){if(this.event.name===a)return b.apply(this,arguments)})},_step:function(a){return this._steps[this._steps.length]=a,this},each:function(a){return this._step(a)},filter:function(a){return this._step(function(b){return qwery.is(b,a)})},relative:function(){return this._step(function(a,b,c){if(!this._relative){if(a.offsetParent)do b-=a.offsetLeft,c-=a.offsetTop,a=a.offsetParent;while(a);this.args=this.args.slice(0,1).concat([b,c]),this._relative=!0}})},pipe:function(a){return this._step(function(){a.process(this.event)})},process:function(a){var b;this._index=0,this._relative=!1,this.event=a,this.args=[a.target,a.pageX,a.pageY];do b=this._next(a);while(b);return this.lastTarget=a.target,b}},["down","up","move"].forEach(function(a){g.prototype[a]=function(b){return this._on(a,b)}}),h.sources={},h.templates={},h.define=function(a,b){var c;return typeof a=="object"&&!a instanceof String&&(b=a,a=""),b=b||{},b.source="none",c=h(b),a&&(h.templates[a]=c),c},i.prototype={_createEvent:function(a,b){return{name:a,target:b.target||b.sourceElement,pageX:b.pageX,pageY:b.pageY,original:b}},add:function(a){this.chains.push(a)},handle:function(a){var b=this;return function(c){var d=b._createEvent(a,c);for(var e=b.chains.length;e--;)b.chains[e].process(d)}}};var j=h.sources.mouse=function(a){i.call(this,a),this._bindEvents(a)};j.prototype=new i,j.prototype.constructor=j,j.prototype._bindEvents=function(a){var b=this;e(a,"mousedown",b.handle("down")),e(a,"mousemove",b.handle("move")),e(a,"mouseup",b.handle("up"))},typeof module!="undefined"&&module.exports?module.exports=h:typeof define!="undefined"?define("fui",[],function(){return h}):a.fui=h})(this)
View
@@ -39,12 +39,33 @@
// initialise the different source handlers
fui.sources = {};
+ // initialise the templates hash
+ fui.templates = {};
+
// create a template function, which is useful for creating chains not automatically bound to an event source
- fui.template = function(opts) {
+ fui.define = function(name, opts) {
+ var chain;
+
+ // if we don't have a name specified, but have been passed options
+ // then remap arguments
+ if (typeof name == 'object' && (! name instanceof String)) {
+ opts = name;
+ name = '';
+ }
+
+ // initialise options
opts = opts || {};
opts.source = 'none';
- return fui(opts);
+ // create a chain
+ chain = fui(opts);
+
+ // if we have a name specified, then add to the template
+ if (name) {
+ fui.templates[name] = chain;
+ }
+
+ return chain;
};
//= source/eventsource
View
@@ -0,0 +1,24 @@
+fui.define('doodle')
+ .filter('canvas')
+ .relative()
+ .each(function(target) {
+ this.args.unshift(target.getContext('2d'));
+ })
+ .down(function(context, target, x, y) {
+ this.state.down = true;
+
+ context.beginPath();
+ context.moveTo(x, y);
+ })
+ .move(function(context, target, x, y) {
+ if (this.lastTarget && target !== this.lastTarget) {
+ context.moveTo(x, y);
+ }
+ else if (this.state.down) {
+ context.lineTo(x, y);
+ context.stroke();
+ }
+ })
+ .up(function(context, target, x, y) {
+ this.state.down = false;
+ });

0 comments on commit f3dc7a7

Please sign in to comment.