GitHub PullRequestReviewCommentEvent at 2014-04-01 03:51:36
Repository: angular.dart
Actor: mhevery
Payload: {"comment":{"url":"https://api.github.com/repos/angular/angular.dart/pulls/comments/11146751","id":11146751,"diff_hunk":"@@ -425,6 +425,179 @@ class InputNumberLikeDirective {\n   }\n }\n \n+/**\n+ * This directive affects which IDL attribute will be used to read the value of\n+ * date/time related input directives. Recognized values for this directive are:\n+ * - [DATE]: input directive will use [dom.InputElement.valueAsDate].\n+ * - [NUMBER]: input directive will use [dom.InputElement.valueAsNumber].\n+ * - [STRING]: input directive will use [dom.InputElement.value].\n+ * The default is [DATE].\n+ */\n+@NgDirective(selector: '[ng-bind-type]')\n+class NgBindTypeForDateLikeDirective {\n+  static const \n+    DIRECTIVE_NAME = 'ng-bind-type',\n+    DATE = 'date', \n+    NUMBER = 'number', \n+    STRING = 'string',\n+    DEFAULT = DATE;\n+  static const VALID_VALUES = const <String>[DATE, NUMBER, STRING];\n+  \n+  final dom.InputElement inputElement;\n+  String _idlAttrKind = DEFAULT;\n+\n+  NgBindTypeForDateLikeDirective(dom.Element this.inputElement);\n+  \n+  @NgAttr(DIRECTIVE_NAME)\n+  void set idlAttrKind(final String _) {\n+    String kind = _ == null ? DEFAULT : _.toLowerCase();\n+    if (!VALID_VALUES.contains(kind))\n+      throw \"Unsupported $DIRECTIVE_NAME attribute value '$_'; \"\n+            \"it should be one of $VALID_VALUES\";\n+    _idlAttrKind = kind;\n+  }\n+\n+  String get idlAttrKind => _idlAttrKind;\n+  \n+  dynamic get inputTypedValue {\n+    switch (idlAttrKind) {\n+      case DATE:   return inputValueAsDate;\n+      case NUMBER: return inputElement.valueAsNumber;\n+      default:     return inputElement.value;\n+    }\n+  }\n+  \n+  void set inputTypedValue(dynamic inputValue) {\n+    if (inputValue is DateTime) {\n+      inputValueAsDate = inputValue;\n+    } else if (inputValue is num) {\n+      inputElement.valueAsNumber = inputValue;\n+    } else {\n+      inputElement.value = inputValue;\n+    }\n+  }\n+  \n+  /// Input's `valueAsDate` normalized to UTC (per HTML5 std).\n+  DateTime get inputValueAsDate {\n+    DateTime dt;\n+    // Wrap in try-catch due to \n+    // https://code.google.com/p/dart/issues/detail?id=17625\n+    try {\n+      dt = inputElement.valueAsDate;\n+    } catch (e) {\n+      dt = null;\n+    }\n+    return (dt != null && !dt.isUtc) ? dt.toUtc() : dt;\n+  }\n+  \n+  /// Set input's `valueAsDate`. Argument is normalized to UTC if necessary\n+  /// (per HTML standard).\n+  void set inputValueAsDate(DateTime dt) {\n+    inputElement.valueAsDate = (dt != null && !dt.isUtc) ? dt.toUtc() : dt;\n+  }\n+}\n+\n+/**\n+ * **Background: Standards and Browsers**\n+ * \n+ * According to the\n+ * [HTML5 Standard](http://www.w3.org/TR/html5/forms.html#the-input-element),\n+ * the [dom.InputElement.valueAsDate] and [dom.InputElement.valueAsNumber] IDL\n+ * attributes should be available for all date/time related input types,\n+ * except for `datetime-local` which is limited to\n+ * [dom.InputElement.valueNumber]. Of course, all input types support\n+ * [dom.InputElement.value] which yields a [String];\n+ * [dom.InputElement.valueAsDate] yields a [DateTime] and\n+ * [dom.InputElement.valueNumber] yields a [num].\n+ * \n+ * But not all browsers currently support date/time related inputs and of\n+ * those that do, some deviate from the standard. Hence, this directive\n+ * allows developers to control the IDL attribute that will be used\n+ * to read the value of a date/time input. This is achieved via the subordinate \n+ * 'ng-bind-type' directive; see [NgBindTypeForDateLikeDirective] for details.\n+ *\n+ * **Usage**:\n+ *\n+ *     <input type=\"date|datetime|datetime-local|month|time|week\" \n+ *            [ng-bind-type=\"date\"]\n+ *            ng-model=\"myModel\">\n+ *\n+ * **Model**:\n+ *\n+ *     dynamic myModel; // one of DateTime | num | String\n+ *\n+ * This directive creates a two-way binding between the input and a model\n+ * property. The subordinate 'ng-bind-type' directive determines which input\n+ * IDL attribute is read (see [NgBindTypeForDateLikeDirective] for details) and\n+ * hence the type of the read values. The type of the model property value\n+ * determines which IDL attribute is written to: [DateTime] and [num] values\n+ * are assigned to [dom.InputElement.valueAsDate] and\n+ * [dom.InputElement.valueNumber], respectively; [String] and `null` values\n+ * are assigned to [dom.InputElement.value]. Setting the model to `null` will \n+ * clear the input if it is currently valid, otherwise, invalid input is left\n+ * untouched (so that the user has an opportunity to correct it). To clear the \n+ * input unconditionally, set the model property to the empty string ('').\n+ * \n+ * **Notes**:\n+ * - As prescribed by the HTML5 standard, [DateTime] values returned by the\n+ *   `valueAsDate` IDL attribute are meant to be in UTC.\n+ * - As of the HTML5 Editor's Draft 29 March 2014, datetime-local is no longer\n+ *   part of the standard. Other date related input are also at risk of being\n+ *   dropped.\n+ */\n+\n+@NgDirective(selector: 'input[type=date][ng-model]')\n+@NgDirective(selector: 'input[type=time][ng-model]')\n+@NgDirective(selector: 'input[type=datetime][ng-model]')\n+@NgDirective(selector: 'input[type=datetime-local][ng-model]')\n+@NgDirective(selector: 'input[type=month][ng-model]')\n+@NgDirective(selector: 'input[type=week][ng-model]')\n+class InputDateLikeDirective {\n+  final dom.InputElement inputElement;\n+  final NgModel ngModel;\n+  final Scope scope;\n+  NgBindTypeForDateLikeDirective ngBindType;\n+  \n+  InputDateLikeDirective(dom.Element this.inputElement, this.ngModel, this.scope,\n+      this.ngBindType) {\n+    if (ngBindType == null) {\n+      ngBindType = new NgBindTypeForDateLikeDirective(inputElement);\n+    }\n+    if (inputElement.type == 'datetime-local') {\n+      ngBindType.idlAttrKind = NgBindTypeForDateLikeDirective.NUMBER;\n+    }\n+    ngModel.render = (value) {\n+      scope.rootScope.domWrite(() {\n+        // print(\"model render: value=$value, input=$typedValue\");\n+        if (!eqOrNaN(value, typedValue)) {\n+          typedValue = value;\n+        }\n+      });\n+    };\n+    inputElement\n+        ..onChange.listen(relaxFnArgs(processValue))\n+        ..onInput.listen(relaxFnArgs(processValue))\n+        ..onBlur.listen((e) {\n+          ngModel.markAsTouched();\n+        });\n+  }\n+  \n+  dynamic get typedValue => ngBindType.inputTypedValue;","path":"lib/directive/ng_model.dart","position":173,"original_position":173,"commit_id":"a40b53f066598f7d72ebaeb251f8946135c84186","original_commit_id":"a40b53f066598f7d72ebaeb251f8946135c84186","user":{"login":"mhevery","id":111951,"avatar_url":"https://avatars.githubusercontent.com/u/111951?","gravatar_id":"8d67eedc1442e8392a7add197abacd66","url":"https://api.github.com/users/mhevery","html_url":"https://github.com/mhevery","followers_url":"https://api.github.com/users/mhevery/followers","following_url":"https://api.github.com/users/mhevery/following{/other_user}","gists_url":"https://api.github.com/users/mhevery/gists{/gist_id}","starred_url":"https://api.github.com/users/mhevery/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhevery/subscriptions","organizations_url":"https://api.github.com/users/mhevery/orgs","repos_url":"https://api.github.com/users/mhevery/repos","events_url":"https://api.github.com/users/mhevery/events{/privacy}","received_events_url":"https://api.github.com/users/mhevery/received_events","type":"User","site_admin":false},"body":"to read see:\r\nhttps://github.com/angular/angular.dart/blob/master/lib/directive/input_select.dart#L101\r\nhttps://github.com/angular/angular.dart/blob/master/lib/directive/input_select.dart#L120","created_at":"2014-04-01T03:51:36Z","updated_at":"2014-04-01T03:51:36Z","html_url":"https://github.com/angular/angular.dart/pull/747#discussion_r11146751","pull_request_url":"https://api.github.com/repos/angular/angular.dart/pulls/747","_links":{"self":{"href":"https://api.github.com/repos/angular/angular.dart/pulls/comments/11146751"},"html":{"href":"https://github.com/angular/angular.dart/pull/747#discussion_r11146751"},"pull_request":{"href":"https://api.github.com/repos/angular/angular.dart/pulls/747"}}}}