Browse Source

Changed request-reflow and request-repaint into events

css_transitions
josdejong 10 years ago
parent
commit
3e5e1f162b
8 changed files with 52 additions and 148 deletions
  1. +0
    -2
      Jakefile.js
  2. +0
    -89
      src/EventBus.js
  3. +0
    -1
      src/module/exports.js
  4. +43
    -45
      src/timeline/Controller.js
  5. +2
    -4
      src/timeline/Stack.js
  6. +2
    -2
      src/timeline/Timeline.js
  7. +2
    -3
      src/timeline/component/Component.js
  8. +3
    -2
      src/timeline/component/ItemSet.js

+ 0
- 2
Jakefile.js View File

@ -55,7 +55,6 @@ task('build', {async: true}, function () {
'./src/shim.js',
'./src/util.js',
'./src/events.js',
'./src/EventBus.js',
'./src/DataSet.js',
'./src/DataView.js',
@ -82,7 +81,6 @@ task('build', {async: true}, function () {
'./src/graph/Popup.js',
'./src/graph/Groups.js',
'./src/graph/Images.js',
'./src/graph/manipulationMixin.js',
'./src/graph/SectorsMixin.js',
'./src/graph/ClusterMixin.js',
'./src/graph/SelectionMixin.js',

+ 0
- 89
src/EventBus.js View File

@ -1,89 +0,0 @@
/**
* An event bus can be used to emit events, and to subscribe to events
* @constructor EventBus
*/
function EventBus() {
this.subscriptions = [];
}
/**
* Subscribe to an event
* @param {String | RegExp} event The event can be a regular expression, or
* a string with wildcards, like 'server.*'.
* @param {function} callback. Callback are called with three parameters:
* {String} event, {*} [data], {*} [source]
* @param {*} [target]
* @returns {String} id A subscription id
*/
EventBus.prototype.on = function (event, callback, target) {
var regexp = (event instanceof RegExp) ?
event :
new RegExp(event.replace('*', '\\w+'));
var subscription = {
id: util.randomUUID(),
event: event,
regexp: regexp,
callback: (typeof callback === 'function') ? callback : null,
target: target
};
this.subscriptions.push(subscription);
return subscription.id;
};
/**
* Unsubscribe from an event
* @param {String | Object} filter Filter for subscriptions to be removed
* Filter can be a string containing a
* subscription id, or an object containing
* one or more of the fields id, event,
* callback, and target.
*/
EventBus.prototype.off = function (filter) {
var i = 0;
while (i < this.subscriptions.length) {
var subscription = this.subscriptions[i];
var match = true;
if (filter instanceof Object) {
// filter is an object. All fields must match
for (var prop in filter) {
if (filter.hasOwnProperty(prop)) {
if (filter[prop] !== subscription[prop]) {
match = false;
}
}
}
}
else {
// filter is a string, filter on id
match = (subscription.id == filter);
}
if (match) {
this.subscriptions.splice(i, 1);
}
else {
i++;
}
}
};
/**
* Emit an event
* @param {String} event
* @param {*} [data]
* @param {*} [source]
*/
EventBus.prototype.emit = function (event, data, source) {
for (var i =0; i < this.subscriptions.length; i++) {
var subscription = this.subscriptions[i];
if (subscription.regexp.test(event)) {
if (subscription.callback) {
subscription.callback(event, data, source);
}
}
}
};

+ 0
- 1
src/module/exports.js View File

@ -11,7 +11,6 @@ var vis = {
Range: Range,
Stack: Stack,
TimeStep: TimeStep,
EventBus: EventBus,
components: {
items: {

+ 43
- 45
src/timeline/Controller.js View File

@ -1,14 +1,54 @@
/**
* @constructor Controller
*
* A Controller controls the reflows and repaints of all visual components
* A Controller controls the reflows and repaints of all components,
* and is used as an event bus for all components.
*/
function Controller () {
var me = this;
this.id = util.randomUUID();
this.components = {};
this.repaintTimer = undefined;
this.reflowTimer = undefined;
/**
* Listen for a 'request-reflow' event. The controller will schedule a reflow
* @param {Boolean} [force] If true, an immediate reflow is forced. Default
* is false.
*/
var reflowTimer = null;
this.on('request-reflow', function requestReflow(force) {
if (force) {
me.reflow();
}
else {
if (!reflowTimer) {
reflowTimer = setTimeout(function () {
reflowTimer = null;
me.reflow();
}, 0);
}
}
});
/**
* Request a repaint. The controller will schedule a repaint
* @param {Boolean} [force] If true, an immediate repaint is forced. Default
* is false.
*/
var repaintTimer = null;
this.on('request-repaint', function requestRepaint(force) {
if (force) {
me.repaint();
}
else {
if (!repaintTimer) {
repaintTimer = setTimeout(function () {
repaintTimer = null;
me.repaint();
}, 0);
}
}
});
}
// Extend controller with Emitter mixin
@ -56,48 +96,6 @@ Controller.prototype.remove = function remove(component) {
}
};
/**
* Request a reflow. The controller will schedule a reflow
* @param {Boolean} [force] If true, an immediate reflow is forced. Default
* is false.
*/
// TODO: change requestReflow into an event
Controller.prototype.requestReflow = function requestReflow(force) {
if (force) {
this.reflow();
}
else {
if (!this.reflowTimer) {
var me = this;
this.reflowTimer = setTimeout(function () {
me.reflowTimer = undefined;
me.reflow();
}, 0);
}
}
};
/**
* Request a repaint. The controller will schedule a repaint
* @param {Boolean} [force] If true, an immediate repaint is forced. Default
* is false.
*/
// TODO: change requestReflow into an event
Controller.prototype.requestRepaint = function requestRepaint(force) {
if (force) {
this.repaint();
}
else {
if (!this.repaintTimer) {
var me = this;
this.repaintTimer = setTimeout(function () {
me.repaintTimer = undefined;
me.repaint();
}, 0);
}
}
};
/**
* Repaint all components
*/

+ 2
- 4
src/timeline/Stack.js View File

@ -63,10 +63,8 @@ Stack.prototype.update = function update() {
};
/**
* Order the items. The items are ordered by width first, and by left position
* second.
* If a custom order function has been provided via the options, then this will
* be used.
* Order the items. If a custom order function has been provided via the options,
* then this will be used.
* @private
*/
Stack.prototype._order = function _order () {

+ 2
- 2
src/timeline/Timeline.js View File

@ -96,12 +96,12 @@ function Timeline (container, items, options) {
this.range.subscribe(this.controller, this.rootPanel, 'zoom', 'horizontal');
this.range.on('rangechange', function (properties) {
var force = true;
me.controller.requestReflow(force);
me.controller.emit('request-reflow', force);
me.emit('rangechange', properties);
});
this.range.on('rangechanged', function (properties) {
var force = true;
me.controller.requestReflow(force);
me.controller.emit('request-reflow', force);
me.emit('rangechanged', properties);
});

+ 2
- 3
src/timeline/component/Component.js View File

@ -20,7 +20,6 @@ function Component () {
* set.
* @param {Object} options Available parameters:
* {String | function} [className]
* {EventBus} [eventBus]
* {String | Number | function} [left]
* {String | Number | function} [top]
* {String | Number | function} [width]
@ -143,7 +142,7 @@ Component.prototype.show = function show() {
*/
Component.prototype.requestRepaint = function requestRepaint() {
if (this.controller) {
this.controller.requestRepaint();
this.controller.emit('request-repaint');
}
else {
throw new Error('Cannot request a repaint: no controller configured');
@ -156,7 +155,7 @@ Component.prototype.requestRepaint = function requestRepaint() {
*/
Component.prototype.requestReflow = function requestReflow() {
if (this.controller) {
this.controller.requestReflow();
this.controller.emit('request-reflow');
}
else {
throw new Error('Cannot request a reflow: no controller configured');

+ 3
- 2
src/timeline/component/ItemSet.js View File

@ -677,8 +677,7 @@ ItemSet.prototype.toScreen = function toScreen(time) {
* @private
*/
ItemSet.prototype._onDragStart = function (event) {
var itemSet = ItemSet.itemSetFromTarget(event),
item = ItemSet.itemFromTarget(event),
var item = ItemSet.itemFromTarget(event),
me = this;
if (item && item.selected) {
@ -704,6 +703,8 @@ ItemSet.prototype._onDrag = function (event) {
item.setOffset(deltaX);
});
// TODO: stacking on dragend changes the order. (stacking orders by start date, which isn't yet changed)
// TODO: implement snapping to nice dates
// TODO: implement dragging from one group to another

Loading…
Cancel
Save