Repository: toddmotto/angular-component Branch: master Commit: 86a65539136e Files: 11 Total size: 19.9 KB Directory structure: gitextract_rmb3cuj_/ ├── .editorconfig ├── .gitignore ├── .jshintrc ├── .travis.yml ├── LICENSE ├── README.md ├── bower.json ├── dist/ │ └── angular-component.js ├── gulpfile.js ├── package.json └── src/ └── angular-component.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .editorconfig ================================================ # EditorConfig helps developers define and maintain consistent # coding styles between different editors and IDEs # editorconfig.org root = true [*] # Change these settings to your own preference indent_style = space indent_size = 2 # We recommend you to keep these unchanged end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.md] trim_trailing_whitespace = false ================================================ FILE: .gitignore ================================================ # Node node_modules ## OS X .DS_Store .AppleDouble .LSOverride Icon ._* .Spotlight-V100 .Trashes ## Windows Thumbs.db ehthumbs.db Desktop.ini $RECYCLE.BIN/ ================================================ FILE: .jshintrc ================================================ { "node": true, "browser": true, "esnext": true, "bitwise": true, "camelcase": true, "curly": false, "eqeqeq": true, "indent": 2, "loopfunc": true, "immed": true, "latedef": true, "newcap": true, "noarg": true, "quotmark": "single", "regexp": true, "undef": true, "unused": false, "trailing": false, "sub": true, "globals": { "angular": true } } ================================================ FILE: .travis.yml ================================================ language: node_js node_js: - "0.10" - "0.11" before_script: - npm install -g gulp script: gulp ================================================ FILE: LICENSE ================================================ LICENSE The MIT License Copyright (c) 2015 Todd Motto Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # angular-component.js Start using `.component()` now! > [View live demo in v1.3.0](https://jsfiddle.net/vz53audf) angular-component.js is a `~1kb` script that adds `component()` support in Angular 1.5 to all Angular versions above `1.3`, so you can begin using the new method now. _Note_: you must include this script straight after `angular.js` and before your application code to ensure the `component()` method has been added to the `angular.module` global. > Read about the Angular 1.5 `component()` method [here](http://toddmotto.com/exploring-the-angular-1-5-component-method) ### Polyfill support This polyfill supports the following feature set of the `.component()` method: | Feature | Supports | |----------------------------------------------------------------|-----------| | `angular.module.component()` method | Yes | | `bindings` property | Yes | | `'E'` restrict default | Yes | | `$ctrl` controllerAs default | Yes | | `transclude` property | Yes | | `template` support | Yes | | `templateUrl` injectable for `$element` and `$attrs` | Yes | | One-way data-binding emulated | Yes | | Lifecycles: `$onInit`, `$onChanges`m `$postLink`, `$onDestroy` | Yes | | `require` Object for parent Component inheritance | Yes | | `$` prefixed properties such as `$canActivate` | Yes | ### Component method usage ```html
``` ## Installing with NPM ``` npm install angular-component --save ``` ## Installing with Bower ``` bower install angular-component.js --save ``` ## Manual installation Ensure you're using the files from the `dist` directory (contains compiled production-ready code). Ensure you place the script before the closing `` tag. ```html ``` ## Contributing In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Gulp. ## Release history - 0.1.2 - Version fixes - 0.1.1 - Version fixes - 0.1.0 - Add one-way data-binding and $onChanges lifecycle, stable release - 0.0.8 - Add new lifecycle hooks ($onInit, $onDestroy, $postLink) - 0.0.7 - Add `require` property from 1.5 stable, `restrict: 'E'` as default - 0.0.6 - Add automated `$` prefixed properties to factory Object - 0.0.5 - Add automatic function annotations - 0.0.4 - Fix Array dependency annotations [#11](https://github.com/toddmotto/angular-component/issues/11) - 0.0.3 - Fix bug caused by bugfix (aligns with new 1.5 changes) - 0.0.2 - Bugfix isolate scope when `bindings` is omitted, short-circuit if .component() is already supported - 0.0.1 - Initial release ================================================ FILE: bower.json ================================================ { "name": "angular-component.js", "version": "0.0.7", "main": "./dist/angular-component.min.js", "dependencies": { "angular": ">=1.3.0 <=1.5" }, "homepage": "https://github.com/toddmotto/angular-component", "authors": [ "Todd Motto " ], "description": "Angular component() polyfill for v1.3+", "moduleType": [], "keywords": [ "Angular", "Component", "Directives" ], "license": "MIT", "ignore": [ "**/.*", "node_modules", "bower_components", "test", "tests" ] } ================================================ FILE: dist/angular-component.js ================================================ /*! angular-component v0.1.3 | (c) 2017 @toddmotto | https://github.com/toddmotto/angular-component */ (function () { var ng = angular.module; function identifierForController(controller, ident) { if (ident && typeof ident === 'string') return ident; if (typeof controller === 'string') { var match = /^(\S+)(\s+as\s+(\w+))?$/.exec(controller); if (match) return match[3]; } } function module() { var hijacked = ng.apply(this, arguments); if (hijacked.component) { return hijacked; } function component(name, options) { function factory($injector) { function makeInjectable(fn) { var closure; var isArray = angular.isArray(fn); if (angular.isFunction(fn) || isArray) { return function (tElement, tAttrs) { return $injector.invoke((isArray ? fn : [ '$element', '$attrs', fn ]), this, { $element: tElement, $attrs: tAttrs }); }; } else { return fn; } } var oneWayQueue = []; function parseBindings(bindings) { var newBindings = {}; for (var prop in bindings) { var binding = bindings[prop]; if (binding.charAt(0) === '<') { var attr = binding.substring(1); oneWayQueue.unshift({ local: prop, attr: attr === '' ? prop : attr }); } else { newBindings[prop] = binding; } } return newBindings; } var modifiedBindings = parseBindings(options.bindings); var requires = [name]; var ctrlNames = []; if (angular.isObject(options.require)) { for (var prop in options.require) { requires.push(options.require[prop]); ctrlNames.push(prop); } } return { controller: options.controller || angular.noop, controllerAs: identifierForController(options.controller) || options.controllerAs || '$ctrl', template: makeInjectable( !options.template && !options.templateUrl ? '' : options.template ), templateUrl: makeInjectable(options.templateUrl), transclude: options.transclude, scope: modifiedBindings || {}, bindToController: !!modifiedBindings, restrict: 'E', require: requires, link: { pre: function ($scope, $element, $attrs, $ctrls) { var self = $ctrls[0]; for (var i = 0; i < ctrlNames.length; i++) { self[ctrlNames[i]] = $ctrls[i + 1]; } if (typeof self.$onDestroy === 'function') { $scope.$on('$destroy', function () { self.$onDestroy.call(self); }); } var changes; function triggerOnChanges() { self.$onChanges(changes); changes = undefined; } function updateChangeListener(key, newValue, oldValue, flush) { if (typeof self.$onChanges === 'function') { if (!changes) { changes = {}; } if (changes[key]) { oldValue = changes[key].currentValue; } changes[key] = { currentValue: newValue, previousValue: oldValue }; if (flush) { triggerOnChanges(); } } } if (oneWayQueue.length) { var destroyQueue = []; for (var q = oneWayQueue.length; q--;) { (function(current){ self[current.local] = $scope.$parent.$eval($attrs[current.attr]); var unbindParent = $scope.$parent.$watch($attrs[current.attr], function (newValue, oldValue) { self[current.local] = newValue; updateChangeListener(current.local, newValue, oldValue, true); }); destroyQueue.unshift(unbindParent); var unbindLocal = $scope.$watch(function () { return self[current.local]; }, function (newValue, oldValue) { updateChangeListener(current.local, newValue, oldValue, false); }); destroyQueue.unshift(unbindLocal); })(oneWayQueue[q]); } $scope.$on('$destroy', function () { for (var i = destroyQueue.length; i--;) { destroyQueue[i](); } }); } if (typeof self.$onInit === 'function') { self.$onInit(); } }, post: function ($scope, $element, $attrs, $ctrls) { var self = $ctrls[0]; if (typeof self.$postLink === 'function') { self.$postLink(); } } } }; } for (var key in options) { if (key.charAt(0) === '$') { factory[key] = options[key]; } } factory.$inject = ['$injector']; return hijacked.directive(name, factory); } hijacked.component = component; return hijacked; } angular.module = module; })(); ================================================ FILE: gulpfile.js ================================================ var gulp = require('gulp'), jshint = require('gulp-jshint'), stylish = require('jshint-stylish'), header = require('gulp-header'), uglify = require('gulp-uglify'), plumber = require('gulp-plumber'), clean = require('gulp-clean'), rename = require('gulp-rename'), package = require('./package.json'); var paths = { output : 'dist/', scripts : [ 'src/angular-component.js' ], test: [ 'test/spec/**/*.js' ] }; var banner = [ '/*! ', '<%= package.name %> ', 'v<%= package.version %> | ', '(c) ' + new Date().getFullYear() + ' <%= package.author %> |', ' <%= package.homepage %>', ' */', '\n' ].join(''); gulp.task('scripts', ['clean'], function() { return gulp.src(paths.scripts) .pipe(plumber()) .pipe(header(banner, { package : package })) .pipe(gulp.dest('dist/')) .pipe(rename({ suffix: '.min' })) .pipe(uglify()) .pipe(header(banner, { package : package })) .pipe(gulp.dest('dist/')); }); gulp.task('lint', function () { return gulp.src(paths.scripts) .pipe(plumber()) .pipe(jshint()) .pipe(jshint.reporter('jshint-stylish')); }); gulp.task('clean', function () { return gulp.src(paths.output, { read: false }) .pipe(plumber()) .pipe(clean()); }); gulp.task('default', [ 'lint', 'clean', 'scripts' ]); ================================================ FILE: package.json ================================================ { "name": "angular-component", "version": "0.1.3", "main": "./dist/angular-component.min.js", "description": "Angular component() polyfill", "author": "@toddmotto", "license": "MIT", "homepage": "https://github.com/toddmotto/angular-component", "repository": { "type": "git", "url": "https://github.com/toddmotto/angular-component.git" }, "devDependencies": { "gulp": "~3.9.0", "gulp-uglify": "~0.3.0", "gulp-rename": "~1.1.0", "gulp-clean": "^0.2.4", "gulp-plumber": "~0.6.2", "gulp-header": "^1.0.2", "gulp-jshint": "^1.6.1", "jshint-stylish": "^0.2.0" } } ================================================ FILE: src/angular-component.js ================================================ (function () { var ng = angular.module; function identifierForController(controller, ident) { if (ident && typeof ident === 'string') return ident; if (typeof controller === 'string') { var match = /^(\S+)(\s+as\s+(\w+))?$/.exec(controller); if (match) return match[3]; } } function module() { var hijacked = ng.apply(this, arguments); if (hijacked.component) { return hijacked; } function component(name, options) { function factory($injector) { function makeInjectable(fn) { var closure; var isArray = angular.isArray(fn); if (angular.isFunction(fn) || isArray) { return function (tElement, tAttrs) { return $injector.invoke((isArray ? fn : [ '$element', '$attrs', fn ]), this, { $element: tElement, $attrs: tAttrs }); }; } else { return fn; } } var oneWayQueue = []; function parseBindings(bindings) { var newBindings = {}; for (var prop in bindings) { var binding = bindings[prop]; if (binding.charAt(0) === '<') { var attr = binding.substring(1); oneWayQueue.unshift({ local: prop, attr: attr === '' ? prop : attr }); } else { newBindings[prop] = binding; } } return newBindings; } var modifiedBindings = parseBindings(options.bindings); var requires = [name]; var ctrlNames = []; if (angular.isObject(options.require)) { for (var prop in options.require) { requires.push(options.require[prop]); ctrlNames.push(prop); } } return { controller: options.controller || angular.noop, controllerAs: identifierForController(options.controller) || options.controllerAs || '$ctrl', template: makeInjectable( !options.template && !options.templateUrl ? '' : options.template ), templateUrl: makeInjectable(options.templateUrl), transclude: options.transclude, scope: modifiedBindings || {}, bindToController: !!modifiedBindings, restrict: 'E', require: requires, link: { pre: function ($scope, $element, $attrs, $ctrls) { var self = $ctrls[0]; for (var i = 0; i < ctrlNames.length; i++) { self[ctrlNames[i]] = $ctrls[i + 1]; } if (typeof self.$onDestroy === 'function') { $scope.$on('$destroy', function () { self.$onDestroy.call(self); }); } var changes; function triggerOnChanges() { self.$onChanges(changes); changes = undefined; } function updateChangeListener(key, newValue, oldValue, flush) { if (typeof self.$onChanges === 'function') { if (!changes) { changes = {}; } if (changes[key]) { oldValue = changes[key].currentValue; } changes[key] = { currentValue: newValue, previousValue: oldValue }; if (flush) { triggerOnChanges(); } } } if (oneWayQueue.length) { var destroyQueue = []; for (var q = oneWayQueue.length; q--;) { (function(current){ self[current.local] = $scope.$parent.$eval($attrs[current.attr]); var unbindParent = $scope.$parent.$watch($attrs[current.attr], function (newValue, oldValue) { self[current.local] = newValue; updateChangeListener(current.local, newValue, oldValue, true); }); destroyQueue.unshift(unbindParent); var unbindLocal = $scope.$watch(function () { return self[current.local]; }, function (newValue, oldValue) { updateChangeListener(current.local, newValue, oldValue, false); }); destroyQueue.unshift(unbindLocal); })(oneWayQueue[q]); } $scope.$on('$destroy', function () { for (var i = destroyQueue.length; i--;) { destroyQueue[i](); } }); } if (typeof self.$onInit === 'function') { self.$onInit(); } }, post: function ($scope, $element, $attrs, $ctrls) { var self = $ctrls[0]; if (typeof self.$postLink === 'function') { self.$postLink(); } } } }; } for (var key in options) { if (key.charAt(0) === '$') { factory[key] = options[key]; } } factory.$inject = ['$injector']; return hijacked.directive(name, factory); } hijacked.component = component; return hijacked; } angular.module = module; })();