├── .gitignore ├── index.js ├── focusIf.min.js ├── gulpfile.js ├── karma.conf.js ├── bower.json ├── focusIf.js ├── LICENSE ├── package.json ├── README.md └── focusIf.spec.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('./focusIf'); 2 | module.exports = 'focus-if'; 3 | -------------------------------------------------------------------------------- /focusIf.min.js: -------------------------------------------------------------------------------- 1 | !function(){"use strict";function c(c){function u(u,f,t){function n(f){f&&c(function(){i.focus()},u.$eval(t.focusDelay)||0)}var i=f[0];t.focusIf?u.$watch(t.focusIf,n):n(!0)}return{restrict:"A",link:u}}angular.module("focus-if",[]).directive("focusIf",c),c.$inject=["$timeout"]}(); -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var gulp = require('gulp'), 3 | ngAnnotate = require('gulp-ng-annotate'), 4 | uglify = require('gulp-uglify'), 5 | rename = require('gulp-rename'); 6 | 7 | gulp.task('default', function() { 8 | return gulp.src('focusIf.js') 9 | .pipe(ngAnnotate()) 10 | .pipe(uglify()) 11 | .pipe(rename({suffix: '.min'})) 12 | .pipe(gulp.dest('.')); 13 | }); 14 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | module.exports = function(config) { 2 | config.set({ 3 | files: [ 4 | 'node_modules/angular/angular.min.js', 5 | 'node_modules/angular-mocks/angular-mocks.js', 6 | 'focusIf.js', 7 | 'focusIf.spec.js' 8 | ], 9 | frameworks: [ 10 | 'jasmine' 11 | ], 12 | browsers: [ 13 | 'PhantomJS' 14 | ], 15 | reporters: [ 16 | 'spec' 17 | ], 18 | preprocessors: { 19 | 'app/**/!(*spec).js': ['coverage'] 20 | }, 21 | coverageReporter: { 22 | type: 'text' 23 | } 24 | }); 25 | }; 26 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ng-focus-if", 3 | "homepage": "https://github.com/hiebj/ng-focus-if", 4 | "authors": [ 5 | "Jonathan Hieb" 6 | ], 7 | "description": "AngularJS directive that will trigger focus on an element under specified conditions", 8 | "repository": { 9 | "type": "git", 10 | "url": "git://github.com/hiebj/ng-focus-if.git" 11 | }, 12 | "main": "focusIf.js", 13 | "keywords": [ 14 | "angular", 15 | "angularjs", 16 | "directive", 17 | "focus-if", 18 | "autofocus", 19 | "focus", 20 | "delay" 21 | ], 22 | "license": "MIT", 23 | "ignore": [ 24 | "**/.*", 25 | "node_modules", 26 | "bower_components", 27 | "test", 28 | "tests" 29 | ], 30 | "dependencies": { 31 | "angular": "1.x" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /focusIf.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | 'use strict'; 3 | angular 4 | .module('focus-if', []) 5 | .directive('focusIf', focusIf); 6 | 7 | focusIf.$inject = ['$timeout']; 8 | 9 | function focusIf($timeout) { 10 | function link($scope, $element, $attrs) { 11 | var dom = $element[0]; 12 | if ($attrs.focusIf) { 13 | $scope.$watch($attrs.focusIf, focus); 14 | } else { 15 | focus(true); 16 | } 17 | function focus(condition) { 18 | if (condition) { 19 | $timeout(function() { 20 | dom.focus(); 21 | }, $scope.$eval($attrs.focusDelay) || 0); 22 | } 23 | } 24 | } 25 | return { 26 | restrict: 'A', 27 | link: link 28 | }; 29 | } 30 | })(); 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jonathan Hieb 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ng-focus-if", 3 | "version": "1.0.7", 4 | "description": "AngularJS directive that will trigger focus on an element under specified conditions", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/hiebj/ng-focus-if.git" 8 | }, 9 | "main": "index.js", 10 | "author": "Jonathan Hieb", 11 | "keywords": [ 12 | "angular", 13 | "angularjs", 14 | "directive", 15 | "focus-if", 16 | "autofocus", 17 | "focus", 18 | "delay" 19 | ], 20 | "license": "MIT", 21 | "devDependencies": { 22 | "angular": "^1.4.2", 23 | "angular-mocks": "^1.4.2", 24 | "gulp": "^3.9.0", 25 | "gulp-cli": "^1.1.0", 26 | "gulp-ng-annotate": "^2.0.0", 27 | "gulp-rename": "^1.2.2", 28 | "gulp-uglify": "^1.2.0", 29 | "jasmine-core": "^2.3.4", 30 | "karma": "^1.0.0", 31 | "karma-cli": "^1.0.0", 32 | "karma-coverage": "^1.0.0", 33 | "karma-jasmine": "^1.0.2", 34 | "karma-phantomjs-launcher": "^1.0.0", 35 | "karma-spec-reporter": "^0.0.26", 36 | "phantomjs-prebuilt": "^2.1.4" 37 | }, 38 | "scripts": { 39 | "gulp": "gulp", 40 | "test": "karma start --single-run", 41 | "coverage": "karma start --reporters coverage --single-run" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ng-focus-if 2 | [![Bower version][bower-image]][github-url] 3 | [![NPM version][npm-image]][npm-url] 4 | [![Downloads][downloads-image]][npm-url] 5 | [![devDependency Status][david-image]][david-url] 6 | [![GitHub stars][stars-image]][github-url] 7 | 8 | An attribute directive that will trigger focus on an element under specified conditions. It can also be used as a cross-browser replacement for the `autofocus` attribute. 9 | 10 | View a live demo on [Plnkr][plnkr-url]. 11 | 12 | It is available through NPM: 13 | 14 | ```text 15 | npm install ng-focus-if 16 | ``` 17 | 18 | Or, via bower: 19 | 20 | ```text 21 | bower install ng-focus-if --save 22 | ``` 23 | 24 | ## Usage 25 | 26 | Include `focusIf.min.js` in your build or directly with a `