Bead to dead with simple date formats. Doesnât support time zones, some foreign date formats, month names and a lot of other things.
Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/edd27839 Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/edd27839 Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/edd27839 Branch: refs/heads/develop Commit: edd27839eefc8d2352dad8ee997142e3979418f8 Parents: 4842fa7 Author: Justin Mclean <jmcl...@apache.org> Authored: Mon Apr 17 14:24:16 2017 +1000 Committer: Justin Mclean <jmcl...@apache.org> Committed: Mon Apr 17 14:24:16 2017 +1000 ---------------------------------------------------------------------- .../html/accessories/SimpleDateFormatBead.as | 222 +++++++++++++++++++ 1 file changed, 222 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/edd27839/frameworks/projects/Formatters/src/main/flex/org/apache/flex/html/accessories/SimpleDateFormatBead.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Formatters/src/main/flex/org/apache/flex/html/accessories/SimpleDateFormatBead.as b/frameworks/projects/Formatters/src/main/flex/org/apache/flex/html/accessories/SimpleDateFormatBead.as new file mode 100644 index 0000000..f2548a7 --- /dev/null +++ b/frameworks/projects/Formatters/src/main/flex/org/apache/flex/html/accessories/SimpleDateFormatBead.as @@ -0,0 +1,222 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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.flex.html.accessories +{ + import org.apache.flex.core.IBead; + import org.apache.flex.core.IDateChooserModel; + import org.apache.flex.core.IFormatBead; + import org.apache.flex.core.IStrand; + import org.apache.flex.core.IStrandWithModel; + import org.apache.flex.events.Event; + import org.apache.flex.events.EventDispatcher; + + /** + * The DateFormatBead class formats the display of a DateField using a format. + * + * @flexjsignoreimport org.apache.flex.core.IStrandWithModel + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.8 + */ + public class SimpleDateFormatBead extends EventDispatcher implements IBead, IFormatBead + { + /** + * constructor. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.8 + */ + public function SimpleDateFormatBead() + { + } + + private var _format:String; + private var _seperator:String; + private var _propertyName:String; + private var _eventName:String; + private var _formattedResult:String; + + /** + * The name of the property on the model holding the value to be formatted. + * The default is selectedDate. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.8 + */ + public function get propertyName():String + { + if (_propertyName == null) { + return "selectedDate"; + } + return _propertyName; + } + public function set propertyName(value:String):void + { + _propertyName = value; + } + + /** + * The name of the event dispatched when the property changes. The + * default is selectedDateChanged. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.8 + */ + public function get eventName():String + { + if (_eventName == null) { + return propertyName+"Changed"; + } + return _eventName; + } + + public function set eventName(value:String):void + { + _eventName = value; + } + + /** + * The format of the date string. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.8 + */ + public function get format():String + { + if (_format == null) { + _format = "YYYY/MM/DD"; + _seperator = "/"; + } + return _format; + } + + public function set format(value:String):void + { + if (_format != value) { + _format = value; + + var length:int = _format.length; + + for (var i:int = 0; i < length; i++) { + var letter:String = _format.charAt(i); + // assumes a single separator + if (letter != 'M' && letter != 'Y' && letter != 'D') { + _seperator = letter; + break; + } + } + _format = value; + } + } + + /** + * The formatted result. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.8 + */ + public function get formattedString():String + { + return _formattedResult; + } + + private var _strand:IStrand; + + /** + * @copy org.apache.flex.core.IBead#strand + * + * @flexjsignorecoercion org.apache.flex.core.IStrandWithModel + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.8 + */ + public function set strand(value:IStrand):void + { + _strand = value; + + var model:IDateChooserModel = IStrandWithModel(_strand).model as IDateChooserModel; + model.addEventListener(propertyName+"Changed",handleTextChange); + } + + /** + * @private + * + * @flexjsignorecoercion org.apache.flex.core.IStrandWithModel + */ + private function handleTextChange(event:Event):void + { + var model:IDateChooserModel = IStrandWithModel(_strand).model as IDateChooserModel; + + var selectedDate:Date = model.selectedDate; + if (selectedDate != null) { + var month:String = String(selectedDate.getMonth()+1); + var day:String = String(selectedDate.getDate()); + var year:String = String(selectedDate.getFullYear()); + var tokens:Array = _format.split(_seperator); + var length:int = tokens.length; + + _formattedResult = ""; + + for (var i:int = 0; i < length; i++) { + switch (tokens[i]) { + case "YYYY": + _formattedResult += year; + break; + case "YY": + _formattedResult += year.slice(2,3); + break; + case "MM": + if (Number(month) < 10) + month = "0" + month; + case "M": + _formattedResult += month; + break; + case "DD": + if (Number(day) < 10) + day = "0" + day; + case "D": + _formattedResult += day; + break; + } + + if (i <= length - 2) { + _formattedResult += _seperator; + } + } + + dispatchEvent(new Event("formatChanged") ); + } + } + + } +}