[General] remove dupe, debounce inputs

Remove duplicate ClickAwayController implementation that was obscuring actual
implementation.  Debounce clickaway action in case toggle is invoked in a
click handler.

Resolves https://github.com/nasa/openmct/issues/888
This commit is contained in:
Pete Richards 2016-05-05 18:25:37 -07:00
parent 01f290dc45
commit 7fad4e9ea1
6 changed files with 22 additions and 230 deletions

View File

@ -268,8 +268,8 @@ define([
"key": "ClickAwayController", "key": "ClickAwayController",
"implementation": ClickAwayController, "implementation": ClickAwayController,
"depends": [ "depends": [
"$scope", "$document",
"$document" "$timeout"
] ]
}, },
{ {

View File

@ -19,7 +19,7 @@
* this source code distribution or the Licensing information page available * this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information. * at runtime from the About dialog for additional information.
*****************************************************************************/ *****************************************************************************/
/*global define,Promise*/ /*global define*/
define( define(
[], [],
@ -36,20 +36,19 @@ define(
* @param $scope the scope in which this controller is active * @param $scope the scope in which this controller is active
* @param $document the document element, injected by Angular * @param $document the document element, injected by Angular
*/ */
function ClickAwayController($scope, $document) { function ClickAwayController($document, $timeout) {
var self = this; var self = this;
this.state = false; this.state = false;
this.$scope = $scope;
this.$document = $document; this.$document = $document;
// Callback used by the document listener. Deactivates; // Callback used by the document listener. Timeout ensures that
// note also $scope.$apply is invoked to indicate that // `clickaway` action occurs after `toggle` if `toggle` is
// the state of this controller has changed. // triggered by a click/mouseup.
this.clickaway = function () { this.clickaway = function () {
self.deactivate(); $timeout(function () {
$scope.$apply(); self.deactivate();
return false; });
}; };
} }

View File

@ -19,7 +19,7 @@
* this source code distribution or the Licensing information page available * this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information. * at runtime from the About dialog for additional information.
*****************************************************************************/ *****************************************************************************/
/*global define,Promise,describe,it,expect,beforeEach,waitsFor,jasmine*/ /*global define,describe,it,expect,beforeEach,jasmine*/
define( define(
["../../src/controllers/ClickAwayController"], ["../../src/controllers/ClickAwayController"],
@ -27,20 +27,20 @@ define(
"use strict"; "use strict";
describe("The click-away controller", function () { describe("The click-away controller", function () {
var mockScope, var mockDocument,
mockDocument, mockTimeout,
controller; controller;
beforeEach(function () { beforeEach(function () {
mockScope = jasmine.createSpyObj(
"$scope",
[ "$apply" ]
);
mockDocument = jasmine.createSpyObj( mockDocument = jasmine.createSpyObj(
"$document", "$document",
[ "on", "off" ] [ "on", "off" ]
); );
controller = new ClickAwayController(mockScope, mockDocument); mockTimeout = jasmine.createSpy('timeout');
controller = new ClickAwayController(
mockDocument,
mockTimeout
);
}); });
it("is initially inactive", function () { it("is initially inactive", function () {
@ -79,10 +79,12 @@ define(
}); });
it("deactivates and detaches listener on document click", function () { it("deactivates and detaches listener on document click", function () {
var callback; var callback, timeout;
controller.setState(true); controller.setState(true);
callback = mockDocument.on.mostRecentCall.args[1]; callback = mockDocument.on.mostRecentCall.args[1];
callback(); callback();
timeout = mockTimeout.mostRecentCall.args[0];
timeout();
expect(controller.isActive()).toEqual(false); expect(controller.isActive()).toEqual(false);
expect(mockDocument.off).toHaveBeenCalledWith("mouseup", callback); expect(mockDocument.off).toHaveBeenCalledWith("mouseup", callback);
}); });
@ -91,4 +93,4 @@ define(
}); });
} }
); );

View File

@ -24,7 +24,6 @@
define([ define([
"./src/controllers/SearchController", "./src/controllers/SearchController",
"./src/controllers/SearchMenuController", "./src/controllers/SearchMenuController",
"./src/controllers/ClickAwayController",
"./src/services/GenericSearchProvider", "./src/services/GenericSearchProvider",
"./src/services/SearchAggregator", "./src/services/SearchAggregator",
"text!./res/templates/search-item.html", "text!./res/templates/search-item.html",
@ -34,7 +33,6 @@ define([
], function ( ], function (
SearchController, SearchController,
SearchMenuController, SearchMenuController,
ClickAwayController,
GenericSearchProvider, GenericSearchProvider,
SearchAggregator, SearchAggregator,
searchItemTemplate, searchItemTemplate,
@ -73,14 +71,6 @@ define([
"$scope", "$scope",
"types[]" "types[]"
] ]
},
{
"key": "ClickAwayController",
"implementation": ClickAwayController,
"depends": [
"$scope",
"$document"
]
} }
], ],
"representations": [ "representations": [

View File

@ -1,105 +0,0 @@
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
/*global define,Promise*/
/*
* Copied from the ClickAwayController in platform/commonUI/general
*/
define(
[],
function () {
"use strict";
/**
* A ClickAwayController is used to toggle things (such as context
* menus) where clicking elsewhere in the document while the toggle
* is in an active state is intended to dismiss the toggle.
*
* @constructor
* @param $scope the scope in which this controller is active
* @param $document the document element, injected by Angular
*/
function ClickAwayController($scope, $document) {
var state = false,
clickaway;
// Track state, but also attach and detach a listener for
// mouseup events on the document.
function deactivate() {
state = false;
$document.off("mouseup", clickaway);
}
function activate() {
state = true;
$document.on("mouseup", clickaway);
}
function changeState() {
if (state) {
deactivate();
} else {
activate();
}
}
// Callback used by the document listener. Deactivates;
// note also $scope.$apply is invoked to indicate that
// the state of this controller has changed.
clickaway = function () {
deactivate();
$scope.$apply();
return false;
};
return {
/**
* Get the current state of the toggle.
* @return {boolean} true if active
*/
isActive: function () {
return state;
},
/**
* Set a new state for the toggle.
* @return {boolean} true to activate
*/
setState: function (newState) {
if (state !== newState) {
changeState();
}
},
/**
* Toggle the current state; activate if it is inactive,
* deactivate if it is active.
*/
toggle: function () {
changeState();
}
};
}
return ClickAwayController;
}
);

View File

@ -1,94 +0,0 @@
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
/*global define,Promise,describe,it,expect,beforeEach,waitsFor,jasmine*/
define(
["../../src/controllers/ClickAwayController"],
function (ClickAwayController) {
"use strict";
describe("The click-away controller", function () {
var mockScope,
mockDocument,
controller;
beforeEach(function () {
mockScope = jasmine.createSpyObj(
"$scope",
[ "$apply" ]
);
mockDocument = jasmine.createSpyObj(
"$document",
[ "on", "off" ]
);
controller = new ClickAwayController(mockScope, mockDocument);
});
it("is initially inactive", function () {
expect(controller.isActive()).toBe(false);
});
it("does not listen to the document before being toggled", function () {
expect(mockDocument.on).not.toHaveBeenCalled();
});
it("tracks enabled/disabled state when toggled", function () {
controller.toggle();
expect(controller.isActive()).toBe(true);
controller.toggle();
expect(controller.isActive()).toBe(false);
controller.toggle();
expect(controller.isActive()).toBe(true);
controller.toggle();
expect(controller.isActive()).toBe(false);
});
it("allows active state to be explictly specified", function () {
controller.setState(true);
expect(controller.isActive()).toBe(true);
controller.setState(true);
expect(controller.isActive()).toBe(true);
controller.setState(false);
expect(controller.isActive()).toBe(false);
controller.setState(false);
expect(controller.isActive()).toBe(false);
});
it("registers a mouse listener when activated", function () {
controller.setState(true);
expect(mockDocument.on).toHaveBeenCalled();
});
it("deactivates and detaches listener on document click", function () {
var callback;
controller.setState(true);
callback = mockDocument.on.mostRecentCall.args[1];
callback();
expect(controller.isActive()).toEqual(false);
expect(mockDocument.off).toHaveBeenCalledWith("mouseup", callback);
});
});
}
);