ng2-file-type
Version:
Angular input file type directive
15 lines (13 loc) • 9.64 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("@angular/core"),require("@angular/forms"),require("@angular/common")):"function"==typeof define&&define.amd?define(["@angular/core","@angular/forms","@angular/common"],t):"object"==typeof exports?exports["ng2-file-type.umd.min"]=t(require("@angular/core"),require("@angular/forms"),require("@angular/common")):e["ng2-file-type.umd.min"]=t(e["@angular/core"],e["@angular/forms"],e["@angular/common"])}(this,function(e,t,n){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};return t.m=e,t.c=n,t.i=function(e){return e},t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="/",t(t.s=5)}([function(e,t,n){"use strict";function r(e,t){function n(){this.constructor=e}m(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}function o(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols)for(var o=0,r=Object.getOwnPropertySymbols(e);o<r.length;o++)t.indexOf(r[o])<0&&(n[r[o]]=e[r[o]]);return n}function i(e,t,n,r){var o,i=arguments.length,a=i<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,n,r);else for(var u=e.length-1;u>=0;u--)(o=e[u])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}function a(e,t){return function(n,r){t(n,r,e)}}function u(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}function l(e,t,n,r){return new(n||(n=Promise))(function(o,i){function a(e){try{l(r.next(e))}catch(e){i(e)}}function u(e){try{l(r.throw(e))}catch(e){i(e)}}function l(e){e.done?o(e.value):new n(function(t){t(e.value)}).then(a,u)}l((r=r.apply(e,t||[])).next())})}function c(e,t){function n(e){return function(t){return r([e,t])}}function r(n){if(o)throw new TypeError("Generator is already executing.");for(;l;)try{if(o=1,i&&(a=i[2&n[0]?"return":n[0]?"throw":"next"])&&!(a=a.call(i,n[1])).done)return a;switch(i=0,a&&(n=[0,a.value]),n[0]){case 0:case 1:a=n;break;case 4:return l.label++,{value:n[1],done:!1};case 5:l.label++,i=n[1],n=[0];continue;case 7:n=l.ops.pop(),l.trys.pop();continue;default:if(a=l.trys,!(a=a.length>0&&a[a.length-1])&&(6===n[0]||2===n[0])){l=0;continue}if(3===n[0]&&(!a||n[1]>a[0]&&n[1]<a[3])){l.label=n[1];break}if(6===n[0]&&l.label<a[1]){l.label=a[1],a=n;break}if(a&&l.label<a[2]){l.label=a[2],l.ops.push(n);break}a[2]&&l.ops.pop(),l.trys.pop();continue}n=t.call(e,l)}catch(e){n=[6,e],i=0}finally{o=a=0}if(5&n[0])throw n[1];return{value:n[0]?n[1]:void 0,done:!0}}var o,i,a,u,l={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return u={next:n(0),throw:n(1),return:n(2)},"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u}function s(e,t){for(var n in e)t.hasOwnProperty(n)||(t[n]=e[n])}function p(e){var t="function"==typeof Symbol&&e[Symbol.iterator],n=0;return t?t.call(e):{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}}}function f(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,o,i=n.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(r=i.next()).done;)a.push(r.value)}catch(e){o={error:e}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return a}function y(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(f(arguments[t]));return e}function _(e){return this instanceof _?(this.v=e,this):new _(e)}function d(e,t,n){function r(e){s[e]&&(c[e]=function(t){return new Promise(function(n,r){p.push([e,t,n,r])>1||o(e,t)})})}function o(e,t){try{i(s[e](t))}catch(e){l(p[0][3],e)}}function i(e){e.value instanceof _?Promise.resolve(e.value.v).then(a,u):l(p[0][2],e)}function a(e){o("next",e)}function u(e){o("throw",e)}function l(e,t){e(t),p.shift(),p.length&&o(p[0][0],p[0][1])}if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var c,s=n.apply(e,t||[]),p=[];return c={},r("next"),r("throw"),r("return"),c[Symbol.asyncIterator]=function(){return this},c}function h(e){function t(t,o){e[t]&&(n[t]=function(n){return(r=!r)?{value:_(e[t](n)),done:"return"===t}:o?o(n):n})}var n,r;return n={},t("next"),t("throw",function(e){throw e}),t("return"),n[Symbol.iterator]=function(){return this},n}function g(e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var t=e[Symbol.asyncIterator];return t?t.call(e):"function"==typeof p?p(e):e[Symbol.iterator]()}Object.defineProperty(t,"__esModule",{value:!0}),t.__extends=r,n.d(t,"__assign",function(){return v}),t.__rest=o,t.__decorate=i,t.__param=a,t.__metadata=u,t.__awaiter=l,t.__generator=c,t.__exportStar=s,t.__values=p,t.__read=f,t.__spread=y,t.__await=_,t.__asyncGenerator=d,t.__asyncDelegator=h,t.__asyncValues=g;/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
var m=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])},v=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++){t=arguments[n];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])}return e}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(0),o=n(2),i=n(3),a=function(){function e(e){this.fileTypeErrorMsg="File type is invalid",this._multiple=!1,this._element=e}return t=e,Object.defineProperty(e.prototype,"multiple",{get:function(){return this._multiple},set:function(e){this._multiple=""===e||!!e},enumerable:!0,configurable:!0}),e.prototype.ngOnChanges=function(e){e.fileTypeErrorMsg&&!e.fileTypeErrorMsg.firstChange&&this._setValidity(this._getInputValue(this._element.nativeElement))},e.prototype.ngDoCheck=function(){if(this._control){var e=!1;this.ng2FileType!==this._oldValue&&(e=!0,this._oldValue=this.ng2FileType),e&&this._setValidity(this._getInputValue(this._element.nativeElement))}},e.prototype.validate=function(e){if(this._control||(this._control=e),this._hasError(this._control.value))return{type:this.fileTypeErrorMsg}},e.prototype.onChange=function(e){var t=this._getInputValue(e);this._setValidity(t)},e.prototype._setValidity=function(e){var t=Object.assign({},this._control.errors);this._hasError(e)?t.type=this.fileTypeErrorMsg:this._control.hasError("type")&&delete t.type,this._control.setErrors(Object.keys(t).length?t:null)},e.prototype._hasError=function(e){return this.ng2FileType&&!this._hasValidType(e)},e.prototype._hasValidType=function(e){var t=!0;if(e)if(this.multiple&&e.length){e=e;for(var n=0;n<e.length;n++){var r=e.item(n);if(!this._validateType(r)){t=!1;break}}}else t=this._validateType(e);return t},e.prototype._validateType=function(e){var t=!0;return e&&(this.ng2FileType instanceof RegExp?t=this.ng2FileType.test(e.type):"string"==typeof this.ng2FileType?t=this.ng2FileType===e.type:Array.isArray(this.ng2FileType)&&(t=this.ng2FileType.includes(e.type))),t},e.prototype._getInputValue=function(e){return this.multiple?e.files:e.files.item(0)},r.__decorate([o.Input(),r.__metadata("design:type",Object)],e.prototype,"ng2FileType",void 0),r.__decorate([o.Input(),r.__metadata("design:type",String)],e.prototype,"fileTypeErrorMsg",void 0),r.__decorate([o.Input(),r.__metadata("design:type",Object),r.__metadata("design:paramtypes",[Object])],e.prototype,"multiple",null),r.__decorate([o.HostListener("change",["$event.target"]),r.__metadata("design:type",Function),r.__metadata("design:paramtypes",[EventTarget]),r.__metadata("design:returntype",void 0)],e.prototype,"onChange",null),e=t=r.__decorate([o.Directive({selector:'input[type="file"][ng2FileType][formControlName],input[type="file"][ng2FileType][formControl],input[type="file"][ng2FileType][ngModel]',exportAs:"ng2FileTypeDirective",providers:[{provide:i.NG_VALIDATORS,useExisting:t,multi:!0}]}),r.__metadata("design:paramtypes",[o.ElementRef])],e);var t}();t.Ng2FileTypeDirective=a},function(t,n){t.exports=e},function(e,n){e.exports=t},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(0),o=n(6),i=n(3),a=n(2),u=n(1),l=function(){function e(){}return e=r.__decorate([a.NgModule({imports:[o.CommonModule,i.FormsModule],declarations:[u.Ng2FileTypeDirective],exports:[u.Ng2FileTypeDirective]})],e)}();t.Ng2FileTypeModule=l},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),n(0).__exportStar(n(1),t);var r=n(4);t.Ng2FileTypeModule=r.Ng2FileTypeModule},function(e,t){e.exports=n}])});