From 0011bcdeb8c056fe22b4f189e3417c79dbfc2cfb Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Thu, 22 Nov 2012 16:14:36 +0800 Subject: [PATCH] Update --- bin/build.js | 1 + bin/config.js | 18 +- deps/eventproxy-0.2.0.js | 440 +++++++++++++++++++++++++++++++++++++++ deps/eventproxy.js | 345 ------------------------------ 4 files changed, 457 insertions(+), 347 deletions(-) create mode 100644 deps/eventproxy-0.2.0.js delete mode 100644 deps/eventproxy.js diff --git a/bin/build.js b/bin/build.js index 6f6030e..cd01072 100644 --- a/bin/build.js +++ b/bin/build.js @@ -28,4 +28,5 @@ exports.build = function (name, withVersion, minify) { exports.build('datav'); exports.build('deps'); exports.build('data_mofang'); +exports.build('stream_component'); exports.build('all'); \ No newline at end of file diff --git a/bin/config.js b/bin/config.js index df8a48d..11d1758 100644 --- a/bin/config.js +++ b/bin/config.js @@ -3,7 +3,7 @@ exports.deps = [ 'deps/json2.js', 'deps/d3.js', 'deps/raphael.js', - 'deps/eventproxy.js', + 'deps/eventproxy-0.2.0.js', 'deps/jquery-1.7.1.js', 'deps/underscore-1.4.2.js' ]; @@ -29,7 +29,7 @@ exports.data_html5 = exports.without([ exports.data_shu = exports.without([ 'deps/raphael.js', - 'deps/eventproxy.js', + 'deps/eventproxy-0.2.0.js', 'deps/jquery-1.7.1.js', 'deps/underscore-1.4.2.js' ]); @@ -59,3 +59,17 @@ exports.all = exports.datav.concat([ 'lib/charts/treemap.js', 'lib/charts/chinamap.js' ]); + +exports.stream_component = [ + 'lib/charts/axis.js', + 'lib/charts/stream_axis.js', + 'lib/charts/legend.js', + 'lib/charts/navi.js', + 'lib/charts/tip.js', + 'lib/charts/percentage.js', + 'lib/charts/hover_line.js', + 'lib/charts/path_label.js', + 'lib/charts/cover.js', + 'lib/charts/stream.js', + 'lib/components/stream.js' +]; diff --git a/deps/eventproxy-0.2.0.js b/deps/eventproxy-0.2.0.js new file mode 100644 index 0000000..3971b0a --- /dev/null +++ b/deps/eventproxy-0.2.0.js @@ -0,0 +1,440 @@ +/*global exports */ +/*! + * This file is used for define the EventProxy library. + * @author Jackson Tian + * @version 0.1.0 + */ +;(function (name, definition) { + // this is considered "safe": + var hasDefine = typeof define === 'function', + // hasDefine = typeof define === 'function', + hasExports = typeof module !== 'undefined' && module.exports; + + if (hasDefine) { // AMD Module or CMD Module + define(definition); + } else if (hasExports) { // Node.js Module + module.exports = definition(); + } else { // Assign to common namespaces or simply the global object (window) + this[name] = definition(); + } +})('EventProxy', function () { + + /** + * EventProxy. An implementation of task/event based asynchronous pattern. + * A module that can be mixed in to *any object* in order to provide it with custom events. + * You may `bind` or `unbind` a callback function to an event; + * `trigger`-ing an event fires all callbacks in succession. + * Examples: + * ``` + * var render = function (template, resources) {}; + * var proxy = new EventProxy(); + * proxy.assign("template", "l10n", render); + * proxy.trigger("template", template); + * proxy.trigger("l10n", resources); + * ``` + */ + var EventProxy = function () { + if (!(this instanceof EventProxy)) { + return new EventProxy(); + } + this._callbacks = {}; + this._fired = {}; + }; + + /** + * Bind an event, specified by a string name, `ev`, to a `callback` function. + * Passing `all` will bind the callback to all events fired. + * @param {String} eventName Event name. + * @param {Function} callback Callback. + */ + EventProxy.prototype.addListener = function (ev, callback) { + this._callbacks = this._callbacks || {}; + this._callbacks[ev] = this._callbacks[ev] || []; + this._callbacks[ev].push(callback); + return this; + }; + /** + * `addListener` alias + */ + EventProxy.prototype.bind = EventProxy.prototype.addListener; + /** + * `addListener` alias + */ + EventProxy.prototype.on = EventProxy.prototype.addListener; + /** + * `addListener` alias + */ + EventProxy.prototype.await = EventProxy.prototype.addListener; + + /** + * Remove one or many callbacks. If `callback` is null, removes all callbacks for the event. + * If `ev` is null, removes all bound callbacks + * for all events. + * @param {String} eventName Event name. + * @param {Function} callback Callback. + */ + EventProxy.prototype.removeListener = function (ev, callback) { + var calls = this._callbacks, i, l; + if (!ev) { + this._callbacks = {}; + } else if (calls) { + if (!callback) { + calls[ev] = []; + } else { + var list = calls[ev]; + if (!list) { + return this; + } + l = list.length; + for (i = 0; i < l; i++) { + if (callback === list[i]) { + list[i] = null; + break; + } + } + } + } + return this; + }; + /** + * `removeListener` alias + */ + EventProxy.prototype.unbind = EventProxy.prototype.removeListener; + + /** + * Remove all listeners. It equals unbind() + * Just add this API for as same as Event.Emitter. + * @param {String} event Event name. + */ + EventProxy.prototype.removeAllListeners = function (event) { + return this.unbind(event); + }; + + /** + * Trigger an event, firing all bound callbacks. Callbacks are passed the + * same arguments as `trigger` is, apart from the event name. + * Listening for `"all"` passes the true event name as the first argument. + * @param {String} eventName Event name + * @param {Mix} data Pass in data + */ + EventProxy.prototype.trigger = function (eventName, data) { + var list, calls, ev, callback, args, i, l; + var both = 2; + if (!(calls = this._callbacks)) { + return this; + } + while (both--) { + ev = both ? eventName : 'all'; + list = calls[ev]; + if (list) { + for (i = 0, l = list.length; i < l; i++) { + if (!(callback = list[i])) { + list.splice(i, 1); i--; l--; + } else { + args = both ? Array.prototype.slice.call(arguments, 1) : arguments; + callback.apply(this, args); + } + } + } + } + return this; + }; + /** + * `trigger` alias + */ + EventProxy.prototype.emit = EventProxy.prototype.trigger; + /** + * `trigger` alias + */ + EventProxy.prototype.fire = EventProxy.prototype.trigger; + + /** + * Bind an event like the bind method, but will remove the listener after it was fired. + * @param {String} ev Event name + * @param {Function} callback Callback + */ + EventProxy.prototype.once = function (ev, callback) { + var self = this; + var wrapper = function () { + callback.apply(self, arguments); + self.unbind(ev, wrapper); + }; + this.bind(ev, wrapper); + return this; + }; + + /** + * Bind an event, and trigger it immediately. + * @param {String} ev Event name. + * @param {Function} callback Callback. + * @param {Mix} data The data that will be passed to calback as arguments. + */ + EventProxy.prototype.immediate = function (ev, callback, data) { + this.bind(ev, callback); + this.trigger(ev, data); + return this; + }; + /** + * `immediate` alias + */ + EventProxy.prototype.asap = EventProxy.prototype.immediate; + + var _assign = function (eventname1, eventname2, cb, once) { + var proxy = this, length, index = 0, argsLength = arguments.length, + bind, _all, + callback, events, isOnce, times = 0, flag = {}; + + // Check the arguments length. + if (argsLength < 3) { + return this; + } + + events = Array.prototype.slice.apply(arguments, [0, argsLength - 2]); + callback = arguments[argsLength - 2]; + isOnce = arguments[argsLength - 1]; + + // Check the callback type. + if (typeof callback !== "function") { + return this; + } + + length = events.length; + bind = function (key) { + var method = isOnce ? "once" : "bind"; + proxy[method](key, function (data) { + proxy._fired[key] = proxy._fired[key] || {}; + proxy._fired[key].data = data; + if (!flag[key]) { + flag[key] = true; + times++; + } + }); + }; + + for (index = 0; index < length; index++) { + bind(events[index]); + } + + _all = function (event) { + if (times < length) { + return; + } + if (!flag[event]) { + return; + } + var data = []; + for (index = 0; index < length; index++) { + data.push(proxy._fired[events[index]].data); + } + if (isOnce) { + proxy.unbind("all", _all); + } + callback.apply(null, data); + }; + proxy.bind("all", _all); + }; + + /** + * Assign some events, after all events were fired, the callback will be executed once. + * Examples: + * ``` + * proxy.all(ev1, ev2, callback); + * proxy.all([ev1, ev2], callback); + * proxy.all(ev1, [ev2, ev3], callback); + * ``` + * @param {String} eventName1 First event name. + * @param {String} eventName2 Second event name. + * @param {Function} callback Callback, that will be called after predefined events were fired. + */ + EventProxy.prototype.all = function (eventname1, eventname2, callback) { + var args = Array.prototype.concat.apply([], arguments); + args.push(true); + _assign.apply(this, args); + return this; + }; + /** + * `all` alias + */ + EventProxy.prototype.assign = EventProxy.prototype.all; + + /** + * Assign the only one 'error' event handler. + * @param {Function(err)} callback + */ + EventProxy.prototype.fail = function (callback) { + var that = this; + that.once('error', function (err) { + that.unbind(); + callback(err); + }); + return this; + }; + + /** + * Assign some events, after all events were fired, the callback will be executed first time. + * Then any event that predefined be fired again, the callback will executed with the newest data. + * Examples: + * ``` + * proxy.tail(ev1, ev2, callback); + * proxy.tail([ev1, ev2], callback); + * proxy.tail(ev1, [ev2, ev3], callback); + * ``` + * @param {String} eventName1 First event name. + * @param {String} eventName2 Second event name. + * @param {Function} callback Callback, that will be called after predefined events were fired. + */ + EventProxy.prototype.tail = function () { + var args = Array.prototype.concat.apply([], arguments); + args.push(false); + _assign.apply(this, args); + return this; + }; + /** + * `tail` alias + */ + EventProxy.prototype.assignAll = EventProxy.prototype.tail; + /** + * `tail` alias + */ + EventProxy.prototype.assignAlways = EventProxy.prototype.tail; + + /** + * The callback will be executed after the event be fired N times. + * @param {String} eventName Event name. + * @param {Mumber} times N times. + * @param {Function} callback Callback, that will be called after event was fired N times. + */ + EventProxy.prototype.after = function (eventName, times, callback) { + if (times === 0) { + callback.call(null, []); + return this; + } + var proxy = this, + firedData = [], + all; + all = function (name, data) { + if (name === eventName) { + times--; + firedData.push(data); + if (times < 1) { + proxy.unbind("all", all); + callback.apply(null, [firedData]); + } + } + }; + proxy.bind("all", all); + return this; + }; + + /** + * The callback will be executed after any registered event was fired. It only executed once. + * @param {string} eventName1 Event name. + * @param {string} eventName2 Event name. + * @param {function} callback The callback will get a map that has data and eventName attributes. + */ + EventProxy.prototype.any = function () { + var proxy = this, + index, + _bind, + len = arguments.length, + callback = arguments[len - 1], + events = Array.prototype.slice.apply(arguments, [0, len - 1]), + count = events.length, + _eventName = events.join("_"); + + proxy.once(_eventName, callback); + + _bind = function (key) { + proxy.bind(key, function (data) { + proxy.trigger(_eventName, {"data": data, eventName: key}); + }); + }; + + for (index = 0; index < count; index++) { + _bind(events[index]); + } + }; + + /** + * The callback will be executed when the evnet name not equals with assigned evnet. + * @param {string} eventName Event name. + * @param {function} callback Callback. + */ + EventProxy.prototype.not = function (eventName, callback) { + var proxy = this; + proxy.bind("all", function (name, data) { + if (name !== eventName) { + callback(data); + } + }); + }; + + /** + * Success callback wraper, will handler err for you. + * + * ```js + * fs.readFile('foo.txt', ep.done('content')); + * + * // equal to => + * + * fs.readFile('foo.txt', function (err, content) { + * if (err) { + * return ep.emit('error', err); + * } + * ep.emit('content', content); + * }); + * ``` + * + * @param {Function|String} handler, success callback or event name will be emit after callback. + * @return {Function} + */ + EventProxy.prototype.done = function (handler) { + var that = this; + return function (err, data) { + if (err) { + return that.emit('error', err); + } + if (typeof handler === 'function') { + return handler(data); + } + var eventname = handler; + that.emit(eventname, data); + }; + }; + + /** + * Create a new EventProxy + * Examples: + * ``` + * var ep = EventProxy.create(); + * ep.assign('user', 'articles', function(user, articles) { + * // do something... + * }); + * // or one line ways: Create EventProxy and Assign + * var ep = EventProxy.create('user', 'articles', function(user, articles) { + * // do something... + * }); + * + * @returns {EventProxy} EventProxy instance + */ + + EventProxy.create = function () { + var ep = new EventProxy(); + var args = Array.prototype.concat.apply([], arguments); + if (args.length) { + var errorHandler = args[args.length - 1]; + var callback = args[args.length - 2]; + if (typeof errorHandler === 'function' && typeof callback === 'function') { + args.pop(); + ep.fail(errorHandler); + } + ep.assign.apply(ep, Array.prototype.slice.call(args)); + } + return ep; + }; + + // Backwards compatibility + EventProxy.EventProxy = EventProxy; + + return EventProxy; +}); diff --git a/deps/eventproxy.js b/deps/eventproxy.js deleted file mode 100644 index 5ebab1d..0000000 --- a/deps/eventproxy.js +++ /dev/null @@ -1,345 +0,0 @@ -/*global exports */ -/** - * @fileoverview This file is used for define the EventProxy library. - * @author Jackson Tian - * @version 0.1.0 - */ -(function () { - /** - * @description EventProxy. A module that can be mixed in to *any object* in order to provide it with - * custom events. You may `bind` or `unbind` a callback function to an event; - * `trigger`-ing an event fires all callbacks in succession. - * @constructor - * @name EventProxy - * @class EventProxy. An implementation of task/event based asynchronous pattern. - * @example - * var render = function (template, resources) {}; - * var proxy = new EventProxy(); - * proxy.assign("template", "l10n", render); - * proxy.trigger("template", template); - * proxy.trigger("l10n", resources); - */ - var EventProxy = function () { - if (!(this instanceof EventProxy)) { - return new EventProxy(); - } - this._callbacks = {}; - this._fired = {}; - }; - - /** - * @description Bind an event, specified by a string name, `ev`, to a `callback` function. - * Passing `"all"` will bind the callback to all events fired. - * @memberOf EventProxy# - * @param {string} eventName Event name. - * @param {function} callback Callback. - */ - EventProxy.prototype.addListener = function (ev, callback) { - this._callbacks = this._callbacks || {}; - this._callbacks[ev] = this._callbacks[ev] || []; - this._callbacks[ev].push(callback); - return this; - }; - EventProxy.prototype.bind = EventProxy.prototype.addListener; - EventProxy.prototype.on = EventProxy.prototype.addListener; - EventProxy.prototype.await = EventProxy.prototype.addListener; - - /** - * @description Remove one or many callbacks. If `callback` is null, removes all - * callbacks for the event. If `ev` is null, removes all bound callbacks - * for all events. - * @memberOf EventProxy# - * @param {string} eventName Event name. - * @param {function} callback Callback. - */ - EventProxy.prototype.removeListener = function (ev, callback) { - var calls = this._callbacks, i, l; - if (!ev) { - this._callbacks = {}; - } else if (calls) { - if (!callback) { - calls[ev] = []; - } else { - var list = calls[ev]; - if (!list) { - return this; - } - l = list.length; - for (i = 0; i < l; i++) { - if (callback === list[i]) { - list[i] = null; - break; - } - } - } - } - return this; - }; - EventProxy.prototype.unbind = EventProxy.prototype.removeListener; - - /** - * @description Remove all listeners. - * It equals unbind(); Just add this API for as same as Event.Emitter. - * @memberOf EventProxy# - * @param {string} event Event name. - */ - EventProxy.prototype.removeAllListeners = function (event) { - return this.unbind(event); - }; - - /** - * @description Trigger an event, firing all bound callbacks. Callbacks are passed the - * same arguments as `trigger` is, apart from the event name. - * Listening for `"all"` passes the true event name as the first argument. - * @param {string} eventName Event name. - * @param {mix} data Pass in data. - */ - EventProxy.prototype.trigger = function (eventName, data) { - var list, calls, ev, callback, args, i, l; - var both = 2; - if (!(calls = this._callbacks)) { - return this; - } - while (both--) { - ev = both ? eventName : 'all'; - list = calls[ev]; - if (list) { - for (i = 0, l = list.length; i < l; i++) { - if (!(callback = list[i])) { - list.splice(i, 1); i--; l--; - } else { - args = both ? Array.prototype.slice.call(arguments, 1) : arguments; - callback.apply(this, args); - } - } - } - } - return this; - }; - EventProxy.prototype.emit = EventProxy.prototype.trigger; - EventProxy.prototype.fire = EventProxy.prototype.trigger; - - /** - * @description Bind an event like the bind method, but will remove the listener after it was fired. - * @param {string} ev Event name. - * @param {function} callback Callback. - */ - EventProxy.prototype.once = function (ev, callback) { - var self = this, - wrapper = function () { - callback.apply(self, arguments); - self.unbind(ev, wrapper); - }; - this.bind(ev, wrapper); - return this; - }; - - /** - * @description Bind an event, and trigger it immediately. - * @param {string} ev Event name. - * @param {function} callback Callback. - * @param {mix} data The data that will be passed to calback as arguments. - */ - EventProxy.prototype.immediate = function (ev, callback, data) { - this.bind(ev, callback); - this.trigger(ev, data); - return this; - }; - - var _assign = function (eventname1, eventname2, cb, once) { - var proxy = this, length, index = 0, argsLength = arguments.length, - bind, _all, - callback, events, isOnce, times = 0, flag = {}; - - // Check the arguments length. - if (argsLength < 3) { - return this; - } - - events = Array.prototype.slice.apply(arguments, [0, argsLength - 2]); - callback = arguments[argsLength - 2]; - isOnce = arguments[argsLength - 1]; - - // Check the callback type. - if (typeof callback !== "function") { - return this; - } - - length = events.length; - bind = function (key) { - var method = isOnce ? "once" : "bind"; - proxy[method](key, function (data) { - proxy._fired[key] = proxy._fired[key] || {}; - proxy._fired[key].data = data; - if (!flag[key]) { - flag[key] = true; - times++; - } - }); - }; - - for (index = 0; index < length; index++) { - bind(events[index]); - } - - _all = function () { - if (times < length) { - return; - } - var data = []; - for (index = 0; index < length; index++) { - data.push(proxy._fired[events[index]].data); - } - if (isOnce) { - proxy.unbind("all", _all); - } - callback.apply(null, data); - }; - proxy.bind("all", _all); - }; - - /** - * @description Assign some events, after all events were fired, the callback will be executed once. - * @example - * proxy.all(ev1, ev2, callback); - * proxy.all([ev1, ev2], callback); - * proxy.all(ev1, [ev2, ev3], callback); - * @param {string} eventName1 First event name. - * @param {string} eventName2 Second event name. - * @param {function} callback Callback, that will be called after predefined events were fired. - */ - EventProxy.prototype.all = function (eventname1, eventname2, cb) { - var args = Array.prototype.concat.apply([], arguments); - args.push(true); - _assign.apply(this, args); - return this; - }; - EventProxy.prototype.assign = EventProxy.prototype.all; - - /** - * @description Assign some events, after all events were fired, the callback will be executed first time. - * then any event that predefined be fired again, the callback will executed with the newest data. - * @example - * proxy.tail(ev1, ev2, callback); - * proxy.tail([ev1, ev2], callback); - * proxy.tail(ev1, [ev2, ev3], callback); - * @memberOf EventProxy# - * @param {string} eventName1 First event name. - * @param {string} eventName2 Second event name. - * @param {function} callback Callback, that will be called after predefined events were fired. - */ - EventProxy.prototype.tail = function () { - var args = Array.prototype.concat.apply([], arguments); - args.push(false); - _assign.apply(this, args); - return this; - }; - EventProxy.prototype.assignAll = EventProxy.prototype.tail; - EventProxy.prototype.assignAlways = EventProxy.prototype.tail; - - /** - * @description The callback will be executed after the event be fired N times. - * @memberOf EventProxy# - * @param {string} eventName Event name. - * @param {number} times N times. - * @param {function} callback Callback, that will be called after event was fired N times. - */ - EventProxy.prototype.after = function (eventName, times, callback) { - if (times === 0) { - callback.call(null, []); - return this; - } - var proxy = this, - firedData = [], - all; - all = function (name, data) { - if (name === eventName) { - times--; - firedData.push(data); - if (times < 1) { - proxy.unbind("all", all); - callback.apply(null, [firedData]); - } - } - }; - proxy.bind("all", all); - return this; - }; - - /** - * @description The callback will be executed after any registered event was fired. It only executed once. - * @memberOf EventProxy# - * @param {string} eventName1 Event name. - * @param {string} eventName2 Event name. - * @param {function} callback The callback will get a map that has data and eventName attributes. - */ - EventProxy.prototype.any = function () { - var proxy = this, - index, - _bind, - len = arguments.length, - callback = arguments[len - 1], - events = Array.prototype.slice.apply(arguments, [0, len - 1]), - count = events.length, - _eventName = events.join("_"); - - proxy.once(_eventName, callback); - - _bind = function (key) { - proxy.bind(key, function (data) { - proxy.trigger(_eventName, {"data": data, eventName: key}); - }); - }; - - for (index = 0; index < count; index++) { - _bind(events[index]); - } - }; - - /** - * @description The callback will be executed when the evnet name not equals with assigned evnet. - * @memberOf EventProxy# - * @param {string} eventName Event name. - * @param {function} callback Callback. - */ - EventProxy.prototype.not = function (eventName, callback) { - var proxy = this; - proxy.bind("all", function (name, data) { - if (name !== eventName) { - callback(data); - } - }); - }; - - /** - * Create a new EventProxy - * @example - * var ep = EventProxy.create(); - * ep.assign('user', 'articles', function(user, articles) { - * // do something... - * }); - * - * // or one line ways: Create EventProxy and Assign - * - * var ep = EventProxy.create('user', 'articles', function(user, articles) { - * // do something... - * }); - * - * @returns {EventProxy} - */ - EventProxy.create = function () { - var ep = new EventProxy(); - if (arguments.length) { - ep.assign.apply(ep, Array.prototype.slice.call(arguments)); - } - return ep; - }; - - // Event proxy can be used in browser and Nodejs both. - if (typeof exports !== "undefined") { - exports.EventProxy = EventProxy; - } else { - this.EventProxy = EventProxy; - } - -}());