diff options
Diffstat (limited to 'js/vendor/angular-ui/modules/directives/reset')
3 files changed, 99 insertions, 0 deletions
diff --git a/js/vendor/angular-ui/modules/directives/reset/reset.js b/js/vendor/angular-ui/modules/directives/reset/reset.js new file mode 100755 index 000000000..4f72fefba --- /dev/null +++ b/js/vendor/angular-ui/modules/directives/reset/reset.js @@ -0,0 +1,26 @@ +/** + * Add a clear button to form inputs to reset their value + */ +angular.module('ui.directives').directive('uiReset', ['ui.config', function (uiConfig) { + var resetValue = null; + if (uiConfig.reset !== undefined) + resetValue = uiConfig.reset; + return { + require: 'ngModel', + link: function (scope, elm, attrs, ctrl) { + var aElement; + aElement = angular.element('<a class="ui-reset" />'); + elm.wrap('<span class="ui-resetwrap" />').after(aElement); + aElement.bind('click', function (e) { + e.preventDefault(); + scope.$apply(function () { + if (attrs.uiReset) + ctrl.$setViewValue(scope.$eval(attrs.uiReset)); + else + ctrl.$setViewValue(resetValue); + ctrl.$render(); + }); + }); + } + }; +}]); diff --git a/js/vendor/angular-ui/modules/directives/reset/stylesheets/reset.less b/js/vendor/angular-ui/modules/directives/reset/stylesheets/reset.less new file mode 100644 index 000000000..3f2c9ea82 --- /dev/null +++ b/js/vendor/angular-ui/modules/directives/reset/stylesheets/reset.less @@ -0,0 +1,18 @@ + +/* ui-reset */ +.ui-resetwrap { + display: inline-block; + position: relative; +} +.ui-reset { + display: none; + position: absolute; + cursor: pointer; + top: 0; + right: 0; + z-index: 2; + height: 100%; +} +.ui-resetwrap:hover .ui-reset { + display: block; +} diff --git a/js/vendor/angular-ui/modules/directives/reset/test/resetSpec.js b/js/vendor/angular-ui/modules/directives/reset/test/resetSpec.js new file mode 100755 index 000000000..c96a2d8b5 --- /dev/null +++ b/js/vendor/angular-ui/modules/directives/reset/test/resetSpec.js @@ -0,0 +1,55 @@ +/*global describe, beforeEach, module, inject, it, spyOn, expect, $ */ +describe('uiReset', function () { + 'use strict'; + + var scope, $compile; + beforeEach(module('ui.directives')); + beforeEach(inject(function (_$rootScope_, _$compile_, _$window_) { + scope = _$rootScope_.$new(); + $compile = _$compile_; + })); + + describe('compiling this directive', function () { + it('should throw an error if we have no model defined', function () { + function compile() { + $compile('<input type="text" ui-reset/>')(scope); + } + + expect(compile).toThrow(); + }); + it('should proper DOM structure', function () { + scope.foo = 'bar'; + scope.$digest(); + var element = $compile('<input type="text" ui-reset ng-model="foo"/>')(scope); + expect(element.parent().is('span')).toBe(true); + expect(element.next().is('a')).toBe(true); + }); + }); + describe('clicking on the created anchor tag', function () { + it('should prevent the default action', function () { + var element = $compile('<input type="text" ui-reset ng-model="foo"/>')(scope); + spyOn($.Event.prototype, 'preventDefault'); + element.siblings().get(0).click(); + expect($.Event.prototype.preventDefault).toHaveBeenCalled(); + }); + it('should set the model value to null and clear control when no options given', function () { + scope.foo = 'bar'; + var element = $compile('<input type="text" ui-reset ng-model="foo"/>')(scope); + scope.$digest(); + expect(element.val()).toBe('bar'); + element.next().click(); + expect(scope.foo).toBe(null); + expect(element.val()).toBe(''); + }); + it('should set the model value to the options scope variable when a string is passed in options', function () { + scope.foo = 'bar'; + scope.resetTo = 'i was reset'; + var element = $compile('<input type="text" ui-reset="resetTo" ng-model="foo"/>')(scope); + scope.$digest(); + expect(element.val()).toBe('bar'); + element.next().click(); + expect(scope.foo).toBe('i was reset'); + expect(element.val()).toBe('i was reset'); + }); + }); +});
\ No newline at end of file |