9a2f0311ae6f56f7835d4a9d1f971fc859d24435
[src/app-framework-demo.git] / afb-client / app / Frontend / widgets / FormInput / UploadFile.js
1
2 /* 
3  * Copyright (C) 2015 "IoT.bzh"
4  * Author "Fulup Ar Foll"
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details..
15  * 
16  * Reference: 
17  *   https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications#Using_hidden_file_input_elements_using_the_click%28%29_method
18  *   https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs
19  *   https://www.terlici.com/2015/05/16/uploading-files-locally.html
20  *   https://github.com/nervgh/angular-file-upload/blob/master/src/services/FileUploader.js
21  */
22
23    
24 function changeInput() {
25      console.log ('input imgClicked'); 
26 }   
27
28 (function() {
29 'use strict';
30
31 // WARNING: Angular ng-change does not work on input/file. Let's hook our callback through standard JS function
32 var tmpl = '<form target="null" action="/api/afbs/file-upload" method="post" enctype="multipart/form-data" >'+
33            '<input type="file" name="{{name}}" onchange="angular.element(this).scope().UpLoadFile(this.files)" accept="{{mime}}/*" style="display" >'+
34            '<input type="submit" class="submit" style="display" > ' +
35            '</form>' + 
36            '<img id="{{name}}-img" src="{{imagepath}}" ng-click="imgClicked()">' ;
37
38 function basename(path) {
39    return path.split('/').reverse()[0];
40 }
41
42 angular.module('UploadFile',['ConfigApp'])
43
44 .directive('uploadFile', function(ConfigApp, $http, JQemu) {
45     function mymethods(scope, elem, attrs) {
46  
47         // get widget image handle from template
48         scope.imgElem    = elem.find('img');
49         scope.inputElem  = elem.find('input');
50         scope.submitElem = JQemu.findByType (elem.children(), "submit");
51
52         
53         // Image was ckick let's simulate an input (file) click
54         scope.imgClicked = function () {
55             scope.inputElem[0].click(); // Warning Angular TriggerEvent does not work!!!
56         };
57         
58         // upload file to server 
59         scope.UpLoadFile= function(files) {
60             
61
62             for (var i = 0; i < files.length; i++) {
63                 var file = files[i];
64                 console.log ("Selected file=" + file.name + " size="+ file.size/1024);
65                 var mimeType = /image.*/;  // build regular expression from Mime
66                 if (!file.type.match(mimeType)) {
67                     continue;
68                 }
69                          
70                 if (file.size > scope.sizemax*1024) {
71                     scope.imagepath = scope.istoobig; // warning is path is wrong nothing happen
72                     scope.$apply('imagepath'); // we short-circuit Angular resync Image
73                 } else {
74
75                     scope.basename=basename(file.name);
76                     scope.imgElem[0].file = file;
77
78                     var reader = new FileReader();
79                     reader.readAsDataURL(file);
80                     reader.onload = function (upload) {
81                         scope.imagepath = upload.target.result;
82                         scope.$apply('imagepath'); // we short-circuit Angular resync image
83                         scope.submitElem[0].click(); // Warning Angular TriggerEvent does not work!!!
84                     };
85                 }
86             }
87         };
88
89         // Initiallize default values from attributes values
90         if (attrs.icon) scope.imagepath= ConfigApp.paths[attrs.category] +  attrs.icon;
91         else  scope.imagepath=ConfigApp.paths.avatars + 'tux-bzh.png';
92         
93         if (attrs.istoobig) scope.istoobig= ConfigApp.paths[attrs.category] +  attrs.istoobig;
94         else  scope.istoobig=ConfigApp.paths.avatars + 'istoobig.jpg';
95         
96         scope.name= attrs.name || 'avatar';
97         scope.mime= attrs.mime || 'image';
98         scope.sizemax= attrs.sizemax || 100; // default max size 100KB
99     
100     }
101     
102     return {
103         restrict: 'E',
104         template: tmpl,
105         link: mymethods,
106         scope: {
107             callback : '='
108         }
109     };
110 });
111
112 console.log ("UploadFile Loaded");
113 })();