From 7fad4e9ea1fe085143eeb1b403e095399e554e2a Mon Sep 17 00:00:00 2001 From: Pete Richards Date: Thu, 5 May 2016 18:25:37 -0700 Subject: [PATCH] [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 --- platform/commonUI/general/bundle.js | 4 +- .../src/controllers/ClickAwayController.js | 17 ++- .../controllers/ClickAwayControllerSpec.js | 22 ++-- platform/search/bundle.js | 10 -- .../src/controllers/ClickAwayController.js | 105 ------------------ .../controllers/ClickAwayControllerSpec.js | 94 ---------------- 6 files changed, 22 insertions(+), 230 deletions(-) delete mode 100644 platform/search/src/controllers/ClickAwayController.js delete mode 100644 platform/search/test/controllers/ClickAwayControllerSpec.js diff --git a/platform/commonUI/general/bundle.js b/platform/commonUI/general/bundle.js index 3e7f558e8b..8bbdbc77af 100644 --- a/platform/commonUI/general/bundle.js +++ b/platform/commonUI/general/bundle.js @@ -268,8 +268,8 @@ define([ "key": "ClickAwayController", "implementation": ClickAwayController, "depends": [ - "$scope", - "$document" + "$document", + "$timeout" ] }, { diff --git a/platform/commonUI/general/src/controllers/ClickAwayController.js b/platform/commonUI/general/src/controllers/ClickAwayController.js index 9c7c6f8091..a25ff51d49 100644 --- a/platform/commonUI/general/src/controllers/ClickAwayController.js +++ b/platform/commonUI/general/src/controllers/ClickAwayController.js @@ -19,7 +19,7 @@ * this source code distribution or the Licensing information page available * at runtime from the About dialog for additional information. *****************************************************************************/ -/*global define,Promise*/ +/*global define*/ define( [], @@ -36,20 +36,19 @@ define( * @param $scope the scope in which this controller is active * @param $document the document element, injected by Angular */ - function ClickAwayController($scope, $document) { + function ClickAwayController($document, $timeout) { var self = this; this.state = false; - this.$scope = $scope; this.$document = $document; - // Callback used by the document listener. Deactivates; - // note also $scope.$apply is invoked to indicate that - // the state of this controller has changed. + // Callback used by the document listener. Timeout ensures that + // `clickaway` action occurs after `toggle` if `toggle` is + // triggered by a click/mouseup. this.clickaway = function () { - self.deactivate(); - $scope.$apply(); - return false; + $timeout(function () { + self.deactivate(); + }); }; } diff --git a/platform/commonUI/general/test/controllers/ClickAwayControllerSpec.js b/platform/commonUI/general/test/controllers/ClickAwayControllerSpec.js index 96e7b6c13f..9b73aefb51 100644 --- a/platform/commonUI/general/test/controllers/ClickAwayControllerSpec.js +++ b/platform/commonUI/general/test/controllers/ClickAwayControllerSpec.js @@ -19,7 +19,7 @@ * 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*/ +/*global define,describe,it,expect,beforeEach,jasmine*/ define( ["../../src/controllers/ClickAwayController"], @@ -27,20 +27,20 @@ define( "use strict"; describe("The click-away controller", function () { - var mockScope, - mockDocument, + var mockDocument, + mockTimeout, controller; beforeEach(function () { - mockScope = jasmine.createSpyObj( - "$scope", - [ "$apply" ] - ); mockDocument = jasmine.createSpyObj( "$document", [ "on", "off" ] ); - controller = new ClickAwayController(mockScope, mockDocument); + mockTimeout = jasmine.createSpy('timeout'); + controller = new ClickAwayController( + mockDocument, + mockTimeout + ); }); it("is initially inactive", function () { @@ -79,10 +79,12 @@ define( }); it("deactivates and detaches listener on document click", function () { - var callback; + var callback, timeout; controller.setState(true); callback = mockDocument.on.mostRecentCall.args[1]; callback(); + timeout = mockTimeout.mostRecentCall.args[0]; + timeout(); expect(controller.isActive()).toEqual(false); expect(mockDocument.off).toHaveBeenCalledWith("mouseup", callback); }); @@ -91,4 +93,4 @@ define( }); } -); \ No newline at end of file +); diff --git a/platform/search/bundle.js b/platform/search/bundle.js index 7a0a9f4b9e..9a0451aa9b 100644 --- a/platform/search/bundle.js +++ b/platform/search/bundle.js @@ -24,7 +24,6 @@ define([ "./src/controllers/SearchController", "./src/controllers/SearchMenuController", - "./src/controllers/ClickAwayController", "./src/services/GenericSearchProvider", "./src/services/SearchAggregator", "text!./res/templates/search-item.html", @@ -34,7 +33,6 @@ define([ ], function ( SearchController, SearchMenuController, - ClickAwayController, GenericSearchProvider, SearchAggregator, searchItemTemplate, @@ -73,14 +71,6 @@ define([ "$scope", "types[]" ] - }, - { - "key": "ClickAwayController", - "implementation": ClickAwayController, - "depends": [ - "$scope", - "$document" - ] } ], "representations": [ diff --git a/platform/search/src/controllers/ClickAwayController.js b/platform/search/src/controllers/ClickAwayController.js deleted file mode 100644 index 9b92e89cc0..0000000000 --- a/platform/search/src/controllers/ClickAwayController.js +++ /dev/null @@ -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; - } -); \ No newline at end of file diff --git a/platform/search/test/controllers/ClickAwayControllerSpec.js b/platform/search/test/controllers/ClickAwayControllerSpec.js deleted file mode 100644 index 96e7b6c13f..0000000000 --- a/platform/search/test/controllers/ClickAwayControllerSpec.js +++ /dev/null @@ -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); - }); - - - - }); - } -); \ No newline at end of file