Sync Integ to Master
[sdc.git] / catalog-ui / src / app / directives / file-opener / file-opener.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 'use strict';
22 import * as _ from "lodash";
23
24 export interface IFileOpenerScope extends ng.IScope {
25     importFile:any;
26     testsId:any;
27     extensions:string;
28
29     onFileSelect():void;
30     onFileUpload(file:any):void;
31     getExtensionsWithDot():string;
32 }
33
34 export class FileOpenerDirective implements ng.IDirective {
35
36     constructor(private $compile:ng.ICompileService) {
37     }
38
39     scope = {
40         onFileUpload: '&',
41         testsId: '@',
42         extensions: '@'
43     };
44
45     restrict = 'AE';
46     replace = true;
47     template = ():string => {
48         return require('./file-opener.html');
49     };
50
51     link = (scope:IFileOpenerScope, element:any) => {
52
53         scope.onFileSelect = () => {
54             scope.onFileUpload({file: scope.importFile});
55             // fix bug 311261
56             // element.html('app/directives/file-opener/file-opener.html');
57             // this.$compile(element.contents())(scope);
58         };
59
60         scope.getExtensionsWithDot = ():string => {
61             let ret = [];
62             _.each(scope.extensions.split(','), function (item) {
63                 ret.push("." + item.toString());
64             });
65             return ret.join(",");
66         };
67
68     };
69
70     public static factory = ($compile:ng.ICompileService)=> {
71         return new FileOpenerDirective($compile);
72     };
73
74 }
75
76 FileOpenerDirective.factory.$inject = ['$compile'];