diff --git a/src/app/application.js b/src/app/application.js index ae792fa6c9..48f09a8d10 100644 --- a/src/app/application.js +++ b/src/app/application.js @@ -224,6 +224,8 @@ dwv.App = function () } } toolList.Filter = new dwv.tool.Filter(filterList, this); + toolList.Filter.addEventListener("filter-run", fireEvent); + toolList.Filter.addEventListener("filter-undo", fireEvent); } } else { diff --git a/src/tools/filter.js b/src/tools/filter.js index 625734f4f8..75b0757d01 100644 --- a/src/tools/filter.js +++ b/src/tools/filter.js @@ -37,6 +37,11 @@ dwv.tool.Filter = function ( filterList, app ) * @type Boolean */ this.displayed = false; + /** + * Listener handler. + * @type Object + */ + var listenerHandler = new dwv.utils.ListenerHandler(); /** * Setup the filter GUI. Called at app startup. @@ -48,6 +53,8 @@ dwv.tool.Filter = function ( filterList, app ) gui.setup(this.filterList); for( var key in this.filterList ){ this.filterList[key].setup(); + this.filterList[key].addEventListener("filter-run", fireEvent); + this.filterList[key].addEventListener("filter-undo", fireEvent); } } }; @@ -97,6 +104,32 @@ dwv.tool.Filter = function ( filterList, app ) app.onKeydown(event); }; + /** + * Add an event listener to this class. + * @param {String} type The event type. + * @param {Object} callback The method associated with the provided event type, + * will be called with the fired event. + */ + this.addEventListener = function (type, callback) { + listenerHandler.add(type, callback); + }; + /** + * Remove an event listener from this class. + * @param {String} type The event type. + * @param {Object} callback The method associated with the provided event type. + */ + this.removeEventListener = function (type, callback) { + listenerHandler.remove(type, callback); + }; + /** + * Fire an event: call all associated listeners with the input event object. + * @param {Object} event The event to fire. + * @private + */ + function fireEvent (event) { + listenerHandler.fireEvent(event); + } + }; // class dwv.tool.Filter /** @@ -186,6 +219,11 @@ dwv.tool.filter.Threshold = function ( app ) * @type Boolean */ var resetImage = true; + /** + * Listener handler. + * @type Object + */ + var listenerHandler = new dwv.utils.ListenerHandler(); /** * Setup the filter GUI. Called at app startup. @@ -230,11 +268,39 @@ dwv.tool.filter.Threshold = function ( app ) resetImage = false; } var command = new dwv.tool.RunFilterCommand(filter, app); + command.onExecute = fireEvent; + command.onUndo = fireEvent; command.execute(); // save command in undo stack app.addToUndoStack(command); }; + /** + * Add an event listener to this class. + * @param {String} type The event type. + * @param {Object} callback The method associated with the provided event type, + * will be called with the fired event. + */ + this.addEventListener = function (type, callback) { + listenerHandler.add(type, callback); + }; + /** + * Remove an event listener from this class. + * @param {String} type The event type. + * @param {Object} callback The method associated with the provided event type. + */ + this.removeEventListener = function (type, callback) { + listenerHandler.remove(type, callback); + }; + /** + * Fire an event: call all associated listeners with the input event object. + * @param {Object} event The event to fire. + * @private + */ + function fireEvent (event) { + listenerHandler.fireEvent(event); + } + }; // class dwv.tool.filter.Threshold @@ -250,6 +316,11 @@ dwv.tool.filter.Sharpen = function ( app ) * @type Object */ var gui = new dwv.gui.Sharpen(app); + /** + * Listener handler. + * @type Object + */ + var listenerHandler = new dwv.utils.ListenerHandler(); /** * Setup the filter GUI. Called at app startup. @@ -285,11 +356,39 @@ dwv.tool.filter.Sharpen = function ( app ) var filter = new dwv.image.filter.Sharpen(); filter.setOriginalImage(app.getImage()); var command = new dwv.tool.RunFilterCommand(filter, app); + command.onExecute = fireEvent; + command.onUndo = fireEvent; command.execute(); // save command in undo stack app.addToUndoStack(command); }; + /** + * Add an event listener to this class. + * @param {String} type The event type. + * @param {Object} callback The method associated with the provided event type, + * will be called with the fired event. + */ + this.addEventListener = function (type, callback) { + listenerHandler.add(type, callback); + }; + /** + * Remove an event listener from this class. + * @param {String} type The event type. + * @param {Object} callback The method associated with the provided event type. + */ + this.removeEventListener = function (type, callback) { + listenerHandler.remove(type, callback); + }; + /** + * Fire an event: call all associated listeners with the input event object. + * @param {Object} event The event to fire. + * @private + */ + function fireEvent (event) { + listenerHandler.fireEvent(event); + } + }; // dwv.tool.filter.Sharpen /** @@ -304,6 +403,11 @@ dwv.tool.filter.Sobel = function ( app ) * @type Object */ var gui = new dwv.gui.Sobel(app); + /** + * Listener handler. + * @type Object + */ + var listenerHandler = new dwv.utils.ListenerHandler(); /** * Setup the filter GUI. Called at app startup. @@ -339,11 +443,39 @@ dwv.tool.filter.Sobel = function ( app ) var filter = new dwv.image.filter.Sobel(); filter.setOriginalImage(app.getImage()); var command = new dwv.tool.RunFilterCommand(filter, app); + command.onExecute = fireEvent; + command.onUndo = fireEvent; command.execute(); // save command in undo stack app.addToUndoStack(command); }; + /** + * Add an event listener to this class. + * @param {String} type The event type. + * @param {Object} callback The method associated with the provided event type, + * will be called with the fired event. + */ + this.addEventListener = function (type, callback) { + listenerHandler.add(type, callback); + }; + /** + * Remove an event listener from this class. + * @param {String} type The event type. + * @param {Object} callback The method associated with the provided event type. + */ + this.removeEventListener = function (type, callback) { + listenerHandler.remove(type, callback); + }; + /** + * Fire an event: call all associated listeners with the input event object. + * @param {Object} event The event to fire. + * @private + */ + function fireEvent (event) { + listenerHandler.fireEvent(event); + } + }; // class dwv.tool.filter.Sobel /** @@ -365,16 +497,41 @@ dwv.tool.RunFilterCommand = function (filter, app) { */ this.execute = function () { + // run filter and set app image app.setImage(filter.update()); + // update display app.render(); + // callback + this.onExecute({'type': 'filter-run', 'id': this.getName()}); }; - + /** * Undo the command. */ this.undo = function () { + // reset the image app.setImage(filter.getOriginalImage()); + // update display app.render(); + // callback + this.onUndo({'type': 'filter-undo', 'id': this.getName()}); }; }; // RunFilterCommand class + +/** + * Handle an execute event. + * @param {Object} event The execute event with type and id. + */ +dwv.tool.RunFilterCommand.prototype.onExecute = function (/*event*/) +{ + // default does nothing. +}; +/** + * Handle an undo event. + * @param {Object} event The undo event with type and id. + */ +dwv.tool.RunFilterCommand.prototype.onUndo = function (/*event*/) +{ + // default does nothing. +}; diff --git a/src/utils/listen.js b/src/utils/listen.js new file mode 100644 index 0000000000..0a221e6722 --- /dev/null +++ b/src/utils/listen.js @@ -0,0 +1,67 @@ +// namespaces +var dwv = dwv || {}; +dwv.utils = dwv.utils || {}; + +/** + * ListenerHandler class: handles add/removing and firing listeners. + * @constructor + */ +dwv.utils.ListenerHandler = function () +{ + /** + * listeners. + * @private + */ + var listeners = {}; + + /** + * Add an event listener. + * @param {String} type The event type. + * @param {Object} callback The method associated with the provided event type, + * will be called with the fired event. + */ + this.add = function (type, callback) + { + // create array if not present + if ( typeof listeners[type] === "undefined" ) { + listeners[type] = []; + } + // add callback to listeners array + listeners[type].push(callback); + }; + + /** + * Remove an event listener. + * @param {String} type The event type. + * @param {Object} callback The method associated with the provided event type. + */ + this.remove = function (type, callback) + { + // check if the type is present + if( typeof listeners[type] === "undefined" ) { + return; + } + // remove from listeners array + for ( var i = 0; i < listeners[type].length; ++i ) { + if ( listeners[type][i] === callback ) { + listeners[type].splice(i,1); + } + } + }; + + /** + * Fire an event: call all associated listeners with the input event object. + * @param {Object} event The event to fire. + */ + this.fireEvent = function (event) + { + // check if they are listeners for the event type + if ( typeof listeners[event.type] === "undefined" ) { + return; + } + // fire events + for ( var i=0; i < listeners[event.type].length; ++i ) { + listeners[event.type][i](event); + } + }; +}; diff --git a/viewers/mobile/applauncher.js b/viewers/mobile/applauncher.js index f02f1e055a..36aba52bd3 100644 --- a/viewers/mobile/applauncher.js +++ b/viewers/mobile/applauncher.js @@ -38,6 +38,8 @@ function startApp() { //myapp.addEventListener("frame-change", listener); //myapp.addEventListener("zoom-change", listener); //myapp.addEventListener("offset-change", listener); + //myapp.addEventListener("filter-run", listener); + //myapp.addEventListener("filter-undo", listener); // initialise the application myapp.init({ diff --git a/viewers/mobile/index.html b/viewers/mobile/index.html index 1187251982..95bdb70628 100644 --- a/viewers/mobile/index.html +++ b/viewers/mobile/index.html @@ -125,6 +125,7 @@ + diff --git a/viewers/simple/index.html b/viewers/simple/index.html index 3fff8ce2e5..eea63da862 100644 --- a/viewers/simple/index.html +++ b/viewers/simple/index.html @@ -89,6 +89,7 @@ + diff --git a/viewers/simplistic/index.html b/viewers/simplistic/index.html index 325f9a5945..d3eeed2b08 100644 --- a/viewers/simplistic/index.html +++ b/viewers/simplistic/index.html @@ -100,6 +100,7 @@ + diff --git a/viewers/static/index.html b/viewers/static/index.html index cb9afbff52..c3aca7d16f 100644 --- a/viewers/static/index.html +++ b/viewers/static/index.html @@ -116,6 +116,7 @@ +