2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 * Created by obarda on 1/27/2016.
25 import * as _ from "lodash";
26 import {IAppConfigurtaion} from "app/models";
28 export class FileUploadModel {
35 export interface IFileUploadScope extends ng.IScope {
36 fileModel:FileUploadModel;
37 formElement:ng.IFormController;
39 elementDisabled:string;
41 elementRequired:string;
42 myFileModel:any; // From the ng bind to <input type=file
44 onFileChangedInDirective:Function;
46 getExtensionsWithDot():string;
48 onFileClick(element:any):void;
49 onAfterValidate():void;
50 setEmptyError(element):void;
51 validateField(field:any):boolean;
56 export class FileUploadDirective implements ng.IDirective {
58 constructor(private sdcConfig:IAppConfigurtaion) {
68 onFileChangedInDirective: '=?',
74 template = ():string => {
75 return require('./file-upload.html');
78 link = (scope:IFileUploadScope, element:any, $attr:any) => {
80 // In case the browse has filename, set it valid.
81 // When editing artifact the file is not sent again, so if we have filename I do not want to show error.
82 if (scope.fileModel && scope.fileModel.filename && scope.fileModel.filename !== '') {
83 scope.formElement[scope.elementName].$setValidity('required', true);
86 scope.getExtensionsWithDot = ():string => {
88 if (scope.extensions) {
89 _.each(scope.extensions.split(','), function (item) {
90 ret.push("." + item.toString());
96 scope.onFileChange = ():void => {
97 if (scope.myFileModel || scope.fileModel) {
98 scope.fileModel = scope.myFileModel;
99 scope.formElement[scope.elementName].value = scope.myFileModel;
101 if (scope.onFileChangedInDirective) {
102 scope.onFileChangedInDirective();
106 scope.setEmptyError = (element):void => {
107 if (element.files[0].size) {
108 scope.formElement[scope.elementName].$setValidity('emptyFile', true);
110 scope.formElement[scope.elementName].$setValidity('emptyFile', false);
111 scope.fileModel = undefined;
116 // Prevent case-sensitivity in the upload-file accept parameter
117 // Workaround for github issue: https://github.com/adonespitogo/angular-base64-upload/issues/81
118 scope.onAfterValidate = () => {
119 if (!scope.formElement[scope.elementName].$valid && scope.extensions) {
120 let uploadfileExtension:string = scope.fileModel.filename.split('.').pop().toLowerCase();
121 if (scope.extensions.split(',').indexOf(uploadfileExtension) > -1) {
122 scope.formElement[scope.elementName].$setValidity('accept', true);
125 // Adding fix for cases when we're changing file type for upload from file that requires certain
126 // extensions to a file that don't requires any extensions
127 if (!scope.formElement[scope.elementName].$valid && scope.formElement[scope.elementName].$error.accept && scope.extensions === "") {
128 scope.formElement[scope.elementName].$setValidity('accept', true);
132 // Workaround, in case user select a file then cancel (X) then select the file again, the event onChange is not fired.
133 // This is a workaround to fix this issue.
134 scope.onFileClick = (element:any):void => {
135 element.value = null;
138 scope.cancel = ():void => {
139 scope.myFileModel = new FileUploadModel();
140 scope.formElement[scope.elementName].$pristine;
141 scope.formElement[scope.elementName].$setValidity('required', false);
142 scope.onFileChange();
146 public static factory = (sdcConfig:IAppConfigurtaion)=> {
147 return new FileUploadDirective(sdcConfig);
152 FileUploadDirective.factory.$inject = [ 'sdcConfig'];