[SDC] code sync
[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
23 export interface IFileOpenerScope extends ng.IScope {
24     importFile:any;
25     testsId:any;
26     extensions:string;
27
28     onFileSelect():void;
29     onFileUpload(file:any):void;
30     getExtensionsWithDot():string;
31 }
32
33 export class FileOpenerDirective implements ng.IDirective {
34
35     constructor(private $compile:ng.ICompileService) {
36     }
37
38     scope = {
39         onFileUpload: '&',
40         testsId: '@',
41         extensions: '@'
42     };
43
44     restrict = 'AE';
45     replace = true;
46     template = ():string => {
47         return require('./file-opener.html');
48     };
49
50     link = (scope:IFileOpenerScope, element:any) => {
51
52         scope.onFileSelect = () => {
53             scope.onFileUpload({file: scope.importFile});
54             // fix bug 311261
55             // element.html('app/directives/file-opener/file-opener.html');
56             // this.$compile(element.contents())(scope);
57         };
58
59         scope.getExtensionsWithDot = ():string => {
60             let ret = [];
61             _.each(scope.extensions.split(','), function (item) {
62                 ret.push("." + item.toString());
63             });
64             return ret.join(",");
65         };
66
67     };
68
69     public static factory = ($compile:ng.ICompileService)=> {
70         return new FileOpenerDirective($compile);
71     };
72
73 }
74
75 FileOpenerDirective.factory.$inject = ['$compile'];