From e0f6412dc8b178531252e6bcdb40788fb931e564 Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Wed, 13 Dec 2017 14:29:31 +1100 Subject: [PATCH] add keyboard controller to date chooser --- .../src/main/resources/basic-manifest.xml | 4 + .../Basic/src/main/royale/BasicClasses.as | 2 + .../beads/controllers/CalendarNavigation.as | 171 ++++++++++++++++++ .../DateChooserKeyboardController.as | 104 +++++++++++ .../controllers/DateChooserMouseController.as | 20 +- .../DateChooserMouseKeyboardController.as | 70 +++++++ 6 files changed, 354 insertions(+), 17 deletions(-) create mode 100644 frameworks/projects/Basic/src/main/royale/org/apache/royale/html/beads/controllers/CalendarNavigation.as create mode 100644 frameworks/projects/Basic/src/main/royale/org/apache/royale/html/beads/controllers/DateChooserKeyboardController.as create mode 100644 frameworks/projects/Basic/src/main/royale/org/apache/royale/html/beads/controllers/DateChooserMouseKeyboardController.as diff --git a/frameworks/projects/Basic/src/main/resources/basic-manifest.xml b/frameworks/projects/Basic/src/main/resources/basic-manifest.xml index 81ad5b3ac9..933ca16833 100644 --- a/frameworks/projects/Basic/src/main/resources/basic-manifest.xml +++ b/frameworks/projects/Basic/src/main/resources/basic-manifest.xml @@ -159,6 +159,10 @@ + + + + diff --git a/frameworks/projects/Basic/src/main/royale/BasicClasses.as b/frameworks/projects/Basic/src/main/royale/BasicClasses.as index 2112bc3a1c..729c457e99 100644 --- a/frameworks/projects/Basic/src/main/royale/BasicClasses.as +++ b/frameworks/projects/Basic/src/main/royale/BasicClasses.as @@ -189,6 +189,8 @@ internal class BasicClasses import org.apache.royale.html.beads.models.DateChooserModel; DateChooserModel; import org.apache.royale.html.beads.models.DataGridPresentationModel; DataGridPresentationModel; import org.apache.royale.html.beads.controllers.DateChooserMouseController; DateChooserMouseController; + import org.apache.royale.html.beads.controllers.DateChooserKeyboardController; DateChooserKeyboardController; + import org.apache.royale.html.beads.controllers.DateChooserMouseKeyboardController; DateChooserMouseKeyboardController; import org.apache.royale.html.beads.controllers.DateFieldMouseController; DateFieldMouseController; import org.apache.royale.html.beads.controllers.RangeStepperMouseController; RangeStepperMouseController; import org.apache.royale.html.supportClasses.DataGridColumn; DataGridColumn; diff --git a/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/beads/controllers/CalendarNavigation.as b/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/beads/controllers/CalendarNavigation.as new file mode 100644 index 0000000000..58ad57dd01 --- /dev/null +++ b/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/beads/controllers/CalendarNavigation.as @@ -0,0 +1,171 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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. +// +//////////////////////////////////////////////////////////////////////////////// +package org.apache.royale.html.beads.controllers +{ + import org.apache.royale.html.beads.models.DateChooserModel; + + /** + * The CalendarNavigation class adjusts a calendar by a month, week and/or day. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.9 + */ + public class CalendarNavigation + { + /** + * constructor. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.9 + */ + public function CalendarNavigation() + { + } + + /** + * Move the display model back one month. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.9 + */ + public function previousMonth(model:DateChooserModel):void + { + var month:Number = model.displayedMonth - 1; + var year:Number = model.displayedYear; + if (month < 0) { + month = 11; + year--; + } + model.displayedMonth = month; + model.displayedYear = year; + } + + /** + * Move the display model forward one month. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.9 + */ + public function nextMonth(model:DateChooserModel):void + { + var month:Number = model.displayedMonth + 1; + var year:Number = model.displayedYear; + if (month >= 12) { + month = 0; + year++; + } + model.displayedMonth = month; + model.displayedYear = year; + } + + /** + * Move the date one week into the past, the month and year + * displayed may change. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.9 + */ + public function previousWeek(model:DateChooserModel):Date + { + return pastDate(model, 7); + } + + /** + * Move the date one week into the future, the month and year + * displayed may change. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.9 + */ + public function nextWeek(model:DateChooserModel):Date + { + return futureDate(model, 7); + } + + /** + * Move the date one day into the past, teh month and year + * displayed may change. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.9 + */ + public function previousDay(model:DateChooserModel):Date + { + return pastDate(model, 1); + } + + /** + * Move the date one day into the future, the month and year + * displayed may change. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.9 + */ + public function nextDay(model:DateChooserModel):Date + { + return futureDate(model, 1); + } + + /** + * @private + */ + private function futureDate(model:DateChooserModel, noDays:int):Date + { + var selected:Date = new Date(model.selectedDate.getTime()); + var month:Number = selected.getMonth(); + + selected.setDate(selected.getDate() + noDays); + if (month > selected.getMonth()) { + nextMonth(model); + } + return selected; + } + + /** + * @private + */ + private function pastDate(model:DateChooserModel, noDays:int):Date + { + var selected:Date = new Date(model.selectedDate.getTime()); + var month:Number = selected.getMonth(); + + selected.setDate(selected.getDate() - noDays); + if (month < selected.getMonth()) { + previousMonth(model); + } + return selected; + } + } +} \ No newline at end of file diff --git a/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/beads/controllers/DateChooserKeyboardController.as b/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/beads/controllers/DateChooserKeyboardController.as new file mode 100644 index 0000000000..4130dd335e --- /dev/null +++ b/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/beads/controllers/DateChooserKeyboardController.as @@ -0,0 +1,104 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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. +// +//////////////////////////////////////////////////////////////////////////////// +package org.apache.royale.html.beads.controllers +{ + import org.apache.royale.html.beads.DateChooserView; + import org.apache.royale.html.beads.models.DateChooserModel; + + import org.apache.royale.core.IBeadController; + import org.apache.royale.core.IBeadModel; + import org.apache.royale.core.IBeadView; + import org.apache.royale.core.IStrand; + import org.apache.royale.events.Event; + import org.apache.royale.events.KeyboardEvent; + import org.apache.royale.events.IEventDispatcher; + + /** + * The DateChooserMouseController class is responsible for listening to + * mouse event related to the DateChooser. Events such as selecting a date + * or changing the calendar. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion Royale 0.0 + */ + public class DateChooserKeyboardController extends CalendarNavigation implements IBeadController + { + /** + * constructor. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion Royale 0.0 + */ + public function DateChooserKeyboardController() + { + } + + private var _strand:IStrand; + + /** + * @copy org.apache.royale.core.IBead#strand + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion Royale 0.9 + */ + public function set strand(value:IStrand):void + { + _strand = value; + var view:DateChooserView = value.getBeadByType(IBeadView) as DateChooserView; + IEventDispatcher(_strand).addEventListener(KeyboardEvent.KEY_DOWN, keyboardHandler); + } + + private function keyboardHandler(event:KeyboardEvent):void + { + var model:DateChooserModel = _strand.getBeadByType(IBeadModel) as DateChooserModel; + var changed:Boolean = false; + var newDate:Date; + + switch (event.key) { + case "ArrowUp": + newDate = previousWeek(model); + changed = true; + break; + case "ArrowDown": + newDate = nextWeek(model); + changed = true; + break; + case "ArrowLeft": + newDate = previousDay(model); + changed = true; + break; + case "ArrowRight": + newDate = nextDay(model); + changed = true; + break; + } + + if (changed) { + model.selectedDate = newDate; + IEventDispatcher(_strand).dispatchEvent( new Event("change") ); + } + } + } +} diff --git a/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/beads/controllers/DateChooserMouseController.as b/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/beads/controllers/DateChooserMouseController.as index ccf3cbcf34..c482d6432a 100644 --- a/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/beads/controllers/DateChooserMouseController.as +++ b/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/beads/controllers/DateChooserMouseController.as @@ -41,7 +41,7 @@ package org.apache.royale.html.beads.controllers * @playerversion AIR 2.6 * @productversion Royale 0.0 */ - public class DateChooserMouseController implements IBeadController + public class DateChooserMouseController extends CalendarNavigation implements IBeadController { /** * constructor. @@ -91,14 +91,7 @@ package org.apache.royale.html.beads.controllers event.preventDefault(); var model:DateChooserModel = _strand.getBeadByType(IBeadModel) as DateChooserModel; - var month:Number = model.displayedMonth - 1; - var year:Number = model.displayedYear; - if (month < 0) { - month = 11; - year--; - } - model.displayedMonth = month; - model.displayedYear = year; + nextMonth(model); } /** @@ -109,14 +102,7 @@ package org.apache.royale.html.beads.controllers event.preventDefault(); var model:DateChooserModel = _strand.getBeadByType(IBeadModel) as DateChooserModel; - var month:Number = model.displayedMonth + 1; - var year:Number = model.displayedYear; - if (month >= 12) { - month = 0; - year++; - } - model.displayedMonth = month; - model.displayedYear = year; + previousMonth(model); } } diff --git a/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/beads/controllers/DateChooserMouseKeyboardController.as b/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/beads/controllers/DateChooserMouseKeyboardController.as new file mode 100644 index 0000000000..b9b29969f2 --- /dev/null +++ b/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/beads/controllers/DateChooserMouseKeyboardController.as @@ -0,0 +1,70 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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. +// +//////////////////////////////////////////////////////////////////////////////// +package org.apache.royale.html.beads.controllers +{ + import org.apache.royale.core.IBeadController; + import org.apache.royale.core.IStrand; + + /** + * The DateChooserMouseKeyboardController combines both the mouse + * and keyboard controllers for the DateChooser. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.9 + */ + public class DateChooserMouseKeyboardController implements IBeadController + { + /** + * constructor. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.9 + */ + public function DateChooserMouseKeyboardController() + { + } + + private var _strand:IStrand; + + private var _mouseController:DateChooserMouseController; + private var _keyboardController:DateChooserKeyboardController; + + /** + * @copy org.apache.flex.core.IBead#strand + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.9 + */ + public function set strand(value:IStrand):void + { + _strand = value; + + _mouseController = new DateChooserMouseController(); + _keyboardController = new DateChooserKeyboardController(); + _mouseController.strand = value; + _keyboardController.strand = value; + } + } +} \ No newline at end of file