From 2430f52bb97fa9d682e5f028c977c5bf94c5ec38 Mon Sep 17 00:00:00 2001
From: Misko Hevery
Date: Fri, 23 Mar 2012 14:03:24 -0700
Subject: chore(module): move files around in preparation for more modules
---
src/ng/directive/a.js | 29 +
src/ng/directive/booleanAttrDirs.js | 314 +++++++++
src/ng/directive/directives.js | 11 +
src/ng/directive/form.js | 267 ++++++++
src/ng/directive/input.js | 1194 +++++++++++++++++++++++++++++++++++
src/ng/directive/ngBind.js | 155 +++++
src/ng/directive/ngClass.js | 143 +++++
src/ng/directive/ngCloak.js | 61 ++
src/ng/directive/ngController.js | 103 +++
src/ng/directive/ngEventDirs.js | 222 +++++++
src/ng/directive/ngInclude.js | 131 ++++
src/ng/directive/ngInit.js | 37 ++
src/ng/directive/ngNonBindable.js | 33 +
src/ng/directive/ngPluralize.js | 204 ++++++
src/ng/directive/ngRepeat.js | 181 ++++++
src/ng/directive/ngShowHide.js | 80 +++
src/ng/directive/ngStyle.js | 42 ++
src/ng/directive/ngSwitch.js | 112 ++++
src/ng/directive/ngTransclude.js | 58 ++
src/ng/directive/ngView.js | 170 +++++
src/ng/directive/script.js | 43 ++
src/ng/directive/select.js | 449 +++++++++++++
src/ng/directive/style.js | 6 +
23 files changed, 4045 insertions(+)
create mode 100644 src/ng/directive/a.js
create mode 100644 src/ng/directive/booleanAttrDirs.js
create mode 100644 src/ng/directive/directives.js
create mode 100644 src/ng/directive/form.js
create mode 100644 src/ng/directive/input.js
create mode 100644 src/ng/directive/ngBind.js
create mode 100644 src/ng/directive/ngClass.js
create mode 100644 src/ng/directive/ngCloak.js
create mode 100644 src/ng/directive/ngController.js
create mode 100644 src/ng/directive/ngEventDirs.js
create mode 100644 src/ng/directive/ngInclude.js
create mode 100644 src/ng/directive/ngInit.js
create mode 100644 src/ng/directive/ngNonBindable.js
create mode 100644 src/ng/directive/ngPluralize.js
create mode 100644 src/ng/directive/ngRepeat.js
create mode 100644 src/ng/directive/ngShowHide.js
create mode 100644 src/ng/directive/ngStyle.js
create mode 100644 src/ng/directive/ngSwitch.js
create mode 100644 src/ng/directive/ngTransclude.js
create mode 100644 src/ng/directive/ngView.js
create mode 100644 src/ng/directive/script.js
create mode 100644 src/ng/directive/select.js
create mode 100644 src/ng/directive/style.js
(limited to 'src/ng/directive')
diff --git a/src/ng/directive/a.js b/src/ng/directive/a.js
new file mode 100644
index 00000000..d96af784
--- /dev/null
+++ b/src/ng/directive/a.js
@@ -0,0 +1,29 @@
+'use strict';
+
+/*
+ * Modifies the default behavior of html A tag, so that the default action is prevented when href
+ * attribute is empty.
+ *
+ * The reasoning for this change is to allow easy creation of action links with ng-click without
+ * changing the location or causing page reloads, e.g.:
+ * Save
+ */
+var htmlAnchorDirective = valueFn({
+ restrict: 'E',
+ compile: function(element, attr) {
+ // turn link into a link in IE
+ // but only if it doesn't have name attribute, in which case it's an anchor
+ if (!attr.href) {
+ attr.$set('href', '');
+ }
+
+ return function(scope, element) {
+ element.bind('click', function(event){
+ // if we have no href url, then don't navigate anywhere.
+ if (!element.attr('href')) {
+ event.preventDefault();
+ }
+ });
+ }
+ }
+});
diff --git a/src/ng/directive/booleanAttrDirs.js b/src/ng/directive/booleanAttrDirs.js
new file mode 100644
index 00000000..7da52db0
--- /dev/null
+++ b/src/ng/directive/booleanAttrDirs.js
@@ -0,0 +1,314 @@
+'use strict';
+
+/**
+ * @ngdoc directive
+ * @name angular.module.ng.$compileProvider.directive.ng-href
+ * @restrict A
+ *
+ * @description
+ * Using markup like {{hash}} in an href attribute makes
+ * the page open to a wrong URL, if the user clicks that link before
+ * angular has a chance to replace the {{hash}} with actual URL, the
+ * link will be broken and will most likely return a 404 error.
+ * The `ng-href` solves this problem by placing the `href` in the
+ * `ng-` namespace.
+ *
+ * The buggy way to write it:
+ *
+ *
+ *
+ *
+ * The correct way to write it:
+ *
+ *
+ *
+ *
+ * @element A
+ * @param {template} ng-href any string which can contain `{{}}` markup.
+ *
+ * @example
+ * This example uses `link` variable inside `href` attribute:
+
+
+
+ link 1 (link, don't reload)
+ link 2 (link, don't reload)
+ link 3 (link, reload!)
+ anchor (link, don't reload)
+ anchor (no link)
+ link (link, change hash)
+
+
+ it('should execute ng-click but not reload when href without value', function() {
+ element('#link-1').click();
+ expect(input('value').val()).toEqual('1');
+ expect(element('#link-1').attr('href')).toBe("");
+ });
+
+ it('should execute ng-click but not reload when href empty string', function() {
+ element('#link-2').click();
+ expect(input('value').val()).toEqual('2');
+ expect(element('#link-2').attr('href')).toBe("");
+ });
+
+ it('should execute ng-click and change url when ng-href specified', function() {
+ expect(element('#link-3').attr('href')).toBe("/123");
+
+ element('#link-3').click();
+ expect(browser().window().path()).toEqual('/123');
+ });
+
+ it('should execute ng-click but not reload when href empty string and name specified', function() {
+ element('#link-4').click();
+ expect(input('value').val()).toEqual('4');
+ expect(element('#link-4').attr('href')).toBe("");
+ });
+
+ it('should execute ng-click but not reload when no href but name specified', function() {
+ element('#link-5').click();
+ expect(input('value').val()).toEqual('5');
+ expect(element('#link-5').attr('href')).toBe("");
+ });
+
+ it('should only change url when only ng-href', function() {
+ input('value').enter('6');
+ expect(element('#link-6').attr('href')).toBe("/6");
+
+ element('#link-6').click();
+ expect(browser().window().path()).toEqual('/6');
+ });
+
+
+ */
+
+/**
+ * @ngdoc directive
+ * @name angular.module.ng.$compileProvider.directive.ng-src
+ * @restrict A
+ *
+ * @description
+ * Using markup like `{{hash}}` in a `src` attribute doesn't
+ * work right: The browser will fetch from the URL with the literal
+ * text `{{hash}}` until replaces the expression inside
+ * `{{hash}}`. The `ng-src` attribute solves this problem by placing
+ * the `src` attribute in the `ng-` namespace.
+ *
+ * The buggy way to write it:
+ *
+ *
+ *
+ *
+ * The correct way to write it:
+ *
+ *
+ *
+ *
+ * @element IMG
+ * @param {template} ng-src any string which can contain `{{}}` markup.
+ */
+
+/**
+ * @ngdoc directive
+ * @name angular.module.ng.$compileProvider.directive.ng-disabled
+ * @restrict A
+ *
+ * @description
+ *
+ * The following markup will make the button enabled on Chrome/Firefox but not on IE8 and older IEs:
+ *
+ *
+ *
+ *
+ *
+ *
+ * The HTML specs do not require browsers to preserve the special attributes such as disabled.
+ * (The presence of them means true and absence means false)
+ * This prevents the angular compiler from correctly retrieving the binding expression.
+ * To solve this problem, we introduce ng-disabled.
+ *
+ * @example
+
+
+ Click me to toggle:
+
+
+
+ it('should toggle button', function() {
+ expect(element('.doc-example-live :button').prop('disabled')).toBeFalsy();
+ input('checked').check();
+ expect(element('.doc-example-live :button').prop('disabled')).toBeTruthy();
+ });
+
+
+ *
+ * @element INPUT
+ * @param {string} expression Angular expression that will be evaluated.
+ */
+
+
+/**
+ * @ngdoc directive
+ * @name angular.module.ng.$compileProvider.directive.ng-checked
+ * @restrict A
+ *
+ * @description
+ * The HTML specs do not require browsers to preserve the special attributes such as checked.
+ * (The presence of them means true and absence means false)
+ * This prevents the angular compiler from correctly retrieving the binding expression.
+ * To solve this problem, we introduce ng-checked.
+ * @example
+
+
+ Check me to check both:
+
+
+
+ it('should check both checkBoxes', function() {
+ expect(element('.doc-example-live #checkSlave').prop('checked')).toBeFalsy();
+ input('master').check();
+ expect(element('.doc-example-live #checkSlave').prop('checked')).toBeTruthy();
+ });
+
+
+ *
+ * @element INPUT
+ * @param {string} expression Angular expression that will be evaluated.
+ */
+
+
+/**
+ * @ngdoc directive
+ * @name angular.module.ng.$compileProvider.directive.ng-multiple
+ * @restrict A
+ *
+ * @description
+ * The HTML specs do not require browsers to preserve the special attributes such as multiple.
+ * (The presence of them means true and absence means false)
+ * This prevents the angular compiler from correctly retrieving the binding expression.
+ * To solve this problem, we introduce ng-multiple.
+ *
+ * @example
+
+
+ Check me check multiple:
+
+
+
+ it('should toggle multiple', function() {
+ expect(element('.doc-example-live #select').prop('multiple')).toBeFalsy();
+ input('checked').check();
+ expect(element('.doc-example-live #select').prop('multiple')).toBeTruthy();
+ });
+
+
+ *
+ * @element SELECT
+ * @param {string} expression Angular expression that will be evaluated.
+ */
+
+
+/**
+ * @ngdoc directive
+ * @name angular.module.ng.$compileProvider.directive.ng-readonly
+ * @restrict A
+ *
+ * @description
+ * The HTML specs do not require browsers to preserve the special attributes such as readonly.
+ * (The presence of them means true and absence means false)
+ * This prevents the angular compiler from correctly retrieving the binding expression.
+ * To solve this problem, we introduce ng-readonly.
+ * @example
+
+
+ Check me to make text readonly:
+
+
+
+ it('should toggle readonly attr', function() {
+ expect(element('.doc-example-live :text').prop('readonly')).toBeFalsy();
+ input('checked').check();
+ expect(element('.doc-example-live :text').prop('readonly')).toBeTruthy();
+ });
+
+
+ *
+ * @element INPUT
+ * @param {string} expression Angular expression that will be evaluated.
+ */
+
+
+/**
+ * @ngdoc directive
+ * @name angular.module.ng.$compileProvider.directive.ng-selected
+ * @restrict A
+ *
+ * @description
+ * The HTML specs do not require browsers to preserve the special attributes such as selected.
+ * (The presence of them means true and absence means false)
+ * This prevents the angular compiler from correctly retrieving the binding expression.
+ * To solve this problem, we introduce ng-selected.
+ * @example
+
+
+ Check me to select:
+
+
+
+ it('should select Greetings!', function() {
+ expect(element('.doc-example-live #greet').prop('selected')).toBeFalsy();
+ input('selected').check();
+ expect(element('.doc-example-live #greet').prop('selected')).toBeTruthy();
+ });
+
+
+ *
+ * @element OPTION
+ * @param {string} expression Angular expression that will be evaluated.
+ */
+
+
+var ngAttributeAliasDirectives = {};
+
+
+// boolean attrs are evaluated
+forEach(BOOLEAN_ATTR, function(propName, attrName) {
+ var normalized = directiveNormalize('ng-' + attrName);
+ ngAttributeAliasDirectives[normalized] = function() {
+ return {
+ compile: function(tpl, attr) {
+ attr.$observers[attrName] = [];
+ return function(scope, element, attr) {
+ scope.$watch(attr[normalized], function(value) {
+ attr.$set(attrName, value);
+ });
+ };
+ }
+ };
+ };
+});
+
+
+// ng-src, ng-href are interpolated
+forEach(['src', 'href'], function(attrName) {
+ var normalized = directiveNormalize('ng-' + attrName);
+ ngAttributeAliasDirectives[normalized] = function() {
+ return {
+ compile: function(tpl, attr) {
+ attr.$observers[attrName] = [];
+ return function(scope, element, attr) {
+ attr.$observe(normalized, function(value) {
+ attr.$set(attrName, value);
+ });
+ };
+ }
+ };
+ };
+});
diff --git a/src/ng/directive/directives.js b/src/ng/directive/directives.js
new file mode 100644
index 00000000..123645f9
--- /dev/null
+++ b/src/ng/directive/directives.js
@@ -0,0 +1,11 @@
+'use strict';
+
+function ngDirective(directive) {
+ if (isFunction(directive)) {
+ directive = {
+ link: directive
+ }
+ }
+ directive.restrict = directive.restrict || 'AC';
+ return valueFn(directive);
+};
diff --git a/src/ng/directive/form.js b/src/ng/directive/form.js
new file mode 100644
index 00000000..b6d3f4be
--- /dev/null
+++ b/src/ng/directive/form.js
@@ -0,0 +1,267 @@
+'use strict';
+
+
+var nullFormCtrl = {
+ $addControl: noop,
+ $removeControl: noop,
+ $setValidity: noop,
+ $setDirty: noop
+}
+
+/**
+ * @ngdoc object
+ * @name angular.module.ng.$compileProvider.directive.form.FormController
+ *
+ * @property {boolean} $pristine True if user has not interacted with the form yet.
+ * @property {boolean} $dirty True if user has already interacted with the form.
+ * @property {boolean} $valid True if all of the containg forms and controls are valid.
+ * @property {boolean} $invalid True if at least one containing control or form is invalid.
+ *
+ * @property {Object} $error Is an object hash, containing references to all invalid controls or
+ * forms, where:
+ *
+ * - keys are validation tokens (error names) — such as `REQUIRED`, `URL` or `EMAIL`),
+ * - values are arrays of controls or forms that are invalid with given error.
+ *
+ * @description
+ * `FormController` keeps track of all its controls and nested forms as well as state of them,
+ * such as being valid/invalid or dirty/pristine.
+ *
+ * Each {@link angular.module.ng.$compileProvider.directive.form form} directive creates an instance
+ * of `FormController`.
+ *
+ */
+FormController.$inject = ['$element', '$attrs'];
+function FormController(element, attrs) {
+ var form = this,
+ parentForm = element.parent().controller('form') || nullFormCtrl,
+ invalidCount = 0, // used to easily determine if we are valid
+ errors = form.$error = {};
+
+ // init state
+ form.$name = attrs.name;
+ form.$dirty = false;
+ form.$pristine = true;
+ form.$valid = true;
+ form.$invalid = false;
+
+ parentForm.$addControl(form);
+
+ // Setup initial state of the control
+ element.addClass(PRISTINE_CLASS);
+ toggleValidCss(true);
+
+ // convenience method for easy toggling of classes
+ function toggleValidCss(isValid, validationErrorKey) {
+ validationErrorKey = validationErrorKey ? '-' + snake_case(validationErrorKey, '-') : '';
+ element.
+ removeClass((isValid ? INVALID_CLASS : VALID_CLASS) + validationErrorKey).
+ addClass((isValid ? VALID_CLASS : INVALID_CLASS) + validationErrorKey);
+ }
+
+ form.$addControl = function(control) {
+ if (control.$name && !form.hasOwnProperty(control.$name)) {
+ form[control.$name] = control;
+ }
+ };
+
+ form.$removeControl = function(control) {
+ if (control.$name && form[control.$name] === control) {
+ delete form[control.$name];
+ }
+ forEach(errors, cleanupControlErrors, control);
+ };
+
+ form.$setValidity = function(validationToken, isValid, control) {
+ if (isValid) {
+ cleanupControlErrors(errors[validationToken], validationToken, control);
+
+ if (!invalidCount) {
+ toggleValidCss(isValid);
+ form.$valid = true;
+ form.$invalid = false;
+ }
+ } else {
+ if (!invalidCount) {
+ toggleValidCss(isValid);
+ }
+ addControlError(validationToken, control);
+
+ form.$valid = false;
+ form.$invalid = true;
+ }
+ };
+
+ form.$setDirty = function() {
+ element.removeClass(PRISTINE_CLASS).addClass(DIRTY_CLASS);
+ form.$dirty = true;
+ form.$pristine = false;
+ };
+
+ function cleanupControlErrors(queue, validationToken, control) {
+ if (queue) {
+ control = control || this; // so that we can be used in forEach;
+ arrayRemove(queue, control);
+ if (!queue.length) {
+ invalidCount--;
+ errors[validationToken] = false;
+ toggleValidCss(true, validationToken);
+ parentForm.$setValidity(validationToken, true, form);
+ }
+ }
+ }
+
+ function addControlError(validationToken, control) {
+ var queue = errors[validationToken];
+ if (queue) {
+ if (includes(queue, control)) return;
+ } else {
+ errors[validationToken] = queue = [];
+ invalidCount++;
+ toggleValidCss(false, validationToken);
+ parentForm.$setValidity(validationToken, false, form);
+ }
+ queue.push(control);
+ }
+}
+
+
+/**
+ * @ngdoc directive
+ * @name angular.module.ng.$compileProvider.directive.ng-form
+ * @restrict EAC
+ *
+ * @description
+ * Nestable alias of {@link angular.module.ng.$compileProvider.directive.form `form`} directive. HTML
+ * does not allow nesting of form elements. It is useful to nest forms, for example if the validity of a
+ * sub-group of controls needs to be determined.
+ *
+ * @param {string=} ng-form|name Name of the form. If specified, the form controller will be published into
+ * related scope, under this name.
+ *
+ */
+
+ /**
+ * @ngdoc directive
+ * @name angular.module.ng.$compileProvider.directive.form
+ * @restrict E
+ *
+ * @description
+ * Directive that instantiates
+ * {@link angular.module.ng.$compileProvider.directive.form.FormController FormController}.
+ *
+ * If `name` attribute is specified, the form controller is published onto the current scope under
+ * this name.
+ *
+ * # Alias: {@link angular.module.ng.$compileProvider.directive.ng-form `ng-form`}
+ *
+ * In angular forms can be nested. This means that the outer form is valid when all of the child
+ * forms are valid as well. However browsers do not allow nesting of `