cleaning up button group internal api. use options object to configure and avoid private methods and using the OpenSeaDragon delegate pattern where possible.

This commit is contained in:
thatcher 2011-12-06 14:48:20 -05:00
parent 531f73356b
commit e830ec6567
3 changed files with 118 additions and 120 deletions

View File

@ -1172,7 +1172,7 @@ $.NavControl.prototype = {
buttons: [zoomIn, zoomOut, goHome, fullPage] buttons: [zoomIn, zoomOut, goHome, fullPage]
}); });
this.elmt = this._group.get_element(); this.elmt = this._group.element;
this.elmt[$.SIGNAL] = true; // hack to get our controls to fade this.elmt[$.SIGNAL] = true; // hack to get our controls to fade
this._viewer.add_open($.delegate(this, this._lightUp)); this._viewer.add_open($.delegate(this, this._lightUp));
}, },
@ -3007,76 +3007,75 @@ $.Button.prototype = {
}( OpenSeadragon )); }( OpenSeadragon ));
(function( $ ){ (function( $ ){
/**
* OpenSeadragon ButtonGroup
*
* Manages events on groups of buttons.
*
* options {}
* buttons - an array of buttons
*
**/
$.ButtonGroup = function( options ) {
$.ButtonGroup = function(properties) { this.buttons = options.buttons;
this.element = options.group || $.Utils.makeNeutralElement("span");
this.config = options.config;
this.tracker = new $.MouseTracker(
this.element,
this.config.clickTimeThreshold,
this.config.clickDistThreshold
);
this._buttons = properties.buttons; // copy the botton elements
this._group = properties.group; var buttons = this.buttons.concat([]),
this.config = properties.config; _this = this,
i;
this.initialize(); this.element.style.display = "inline-block";
for ( i = 0; i < buttons.length; i++ ) {
this.element.appendChild( buttons[ i ].get_element() );
}
this.tracker.enter = options.enter || function() {
var i;
for ( i = 0; i < _this.buttons.length; i++) {
_this.buttons[ i ].notifyGroupEnter();
}
};
this.tracker.exit = options.exit || function() {
var i,
buttonDownElmt = arguments.length > 2 ? arguments[2] : null;
if ( !buttonDownElmt ) {
for ( i = 0; i < _this.buttons.length; i++ ) {
_this.buttons[ i ].notifyGroupExit();
}
}
};
this.tracker.release = options.release || function() {
var i,
insideElmtRelease = arguments.length > 3 ? arguments[3] : null;
if ( !insideElmtRelease ) {
for ( i = 0; i < _this.buttons.length; i++ ) {
_this.buttons[ i ].notifyGroupExit();
}
}
};
this.tracker.setTracking( true );
}; };
$.ButtonGroup.prototype = { $.ButtonGroup.prototype = {
initialize: function() {
this._group = $.Utils.makeNeutralElement("span");
var buttons = this._buttons.concat([]); // copy
var tracker = new $.MouseTracker(this._group, this.config.clickTimeThreshold, this.config.clickDistThreshold);
this._group.style.display = "inline-block";
for (var i = 0; i < buttons.length; i++) {
this._group.appendChild(buttons[i].get_element());
}
tracker.enterHandler = $.delegate(this, this._enterHandler);
tracker.exitHandler = $.delegate(this, this._exitHandler);
tracker.releaseHandler = $.delegate(this, this._releaseHandler);
tracker.setTracking(true);
},
get_buttons: function() {
return this._buttons;
},
set_buttons: function(value) {
this._buttons = value;
},
get_element: function() {
return this._group;
},
get_config: function() {
return this.config;
},
set_config: function(value) {
this.config = value;
},
_enterHandler: function(tracker, position, buttonDownElmt, buttonDownAny) {
for (var i = 0; i < this._buttons.length; i++) {
this._buttons[i].notifyGroupEnter();
}
},
_exitHandler: function(tracker, position, buttonDownElmt, buttonDownAny) {
if (!buttonDownElmt) {
for (var i = 0; i < this._buttons.length; i++) {
this._buttons[i].notifyGroupExit();
}
}
},
_releaseHandler: function(tracker, position, insideElmtPress, insideElmtRelease) {
if (!insideElmtRelease) {
for (var i = 0; i < this._buttons.length; i++) {
this._buttons[i].notifyGroupExit();
}
}
},
emulateEnter: function() { emulateEnter: function() {
this._enterHandler(); this.tracker.enter();
}, },
emulateExit: function() { emulateExit: function() {
this._exitHandler(); this.tracker.exit();
} }
}; };

View File

@ -1,75 +1,74 @@
(function( $ ){ (function( $ ){
/**
* OpenSeadragon ButtonGroup
*
* Manages events on groups of buttons.
*
* options {}
* buttons - an array of buttons
*
**/
$.ButtonGroup = function( options ) {
$.ButtonGroup = function(properties) { this.buttons = options.buttons;
this.element = options.group || $.Utils.makeNeutralElement("span");
this.config = options.config;
this.tracker = new $.MouseTracker(
this.element,
this.config.clickTimeThreshold,
this.config.clickDistThreshold
);
this._buttons = properties.buttons; // copy the botton elements
this._group = properties.group; var buttons = this.buttons.concat([]),
this.config = properties.config; _this = this,
i;
this.initialize(); this.element.style.display = "inline-block";
for ( i = 0; i < buttons.length; i++ ) {
this.element.appendChild( buttons[ i ].get_element() );
}
this.tracker.enter = options.enter || function() {
var i;
for ( i = 0; i < _this.buttons.length; i++) {
_this.buttons[ i ].notifyGroupEnter();
}
};
this.tracker.exit = options.exit || function() {
var i,
buttonDownElmt = arguments.length > 2 ? arguments[2] : null;
if ( !buttonDownElmt ) {
for ( i = 0; i < _this.buttons.length; i++ ) {
_this.buttons[ i ].notifyGroupExit();
}
}
};
this.tracker.release = options.release || function() {
var i,
insideElmtRelease = arguments.length > 3 ? arguments[3] : null;
if ( !insideElmtRelease ) {
for ( i = 0; i < _this.buttons.length; i++ ) {
_this.buttons[ i ].notifyGroupExit();
}
}
};
this.tracker.setTracking( true );
}; };
$.ButtonGroup.prototype = { $.ButtonGroup.prototype = {
initialize: function() {
this._group = $.Utils.makeNeutralElement("span");
var buttons = this._buttons.concat([]); // copy
var tracker = new $.MouseTracker(this._group, this.config.clickTimeThreshold, this.config.clickDistThreshold);
this._group.style.display = "inline-block";
for (var i = 0; i < buttons.length; i++) {
this._group.appendChild(buttons[i].get_element());
}
tracker.enterHandler = $.delegate(this, this._enterHandler);
tracker.exitHandler = $.delegate(this, this._exitHandler);
tracker.releaseHandler = $.delegate(this, this._releaseHandler);
tracker.setTracking(true);
},
get_buttons: function() {
return this._buttons;
},
set_buttons: function(value) {
this._buttons = value;
},
get_element: function() {
return this._group;
},
get_config: function() {
return this.config;
},
set_config: function(value) {
this.config = value;
},
_enterHandler: function(tracker, position, buttonDownElmt, buttonDownAny) {
for (var i = 0; i < this._buttons.length; i++) {
this._buttons[i].notifyGroupEnter();
}
},
_exitHandler: function(tracker, position, buttonDownElmt, buttonDownAny) {
if (!buttonDownElmt) {
for (var i = 0; i < this._buttons.length; i++) {
this._buttons[i].notifyGroupExit();
}
}
},
_releaseHandler: function(tracker, position, insideElmtPress, insideElmtRelease) {
if (!insideElmtRelease) {
for (var i = 0; i < this._buttons.length; i++) {
this._buttons[i].notifyGroupExit();
}
}
},
emulateEnter: function() { emulateEnter: function() {
this._enterHandler(); this.tracker.enter();
}, },
emulateExit: function() { emulateExit: function() {
this._exitHandler(); this.tracker.exit();
} }
}; };

View File

@ -78,7 +78,7 @@ $.NavControl.prototype = {
buttons: [zoomIn, zoomOut, goHome, fullPage] buttons: [zoomIn, zoomOut, goHome, fullPage]
}); });
this.elmt = this._group.get_element(); this.elmt = this._group.element;
this.elmt[$.SIGNAL] = true; // hack to get our controls to fade this.elmt[$.SIGNAL] = true; // hack to get our controls to fade
this._viewer.add_open($.delegate(this, this._lightUp)); this._viewer.add_open($.delegate(this, this._lightUp));
}, },