3 * Copyright (C) 2015 "IoT.bzh"
4 * Author "Fulup Ar Foll"
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.
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..
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
24 function changeInput() {
25 console.log ('input imgClicked');
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" > ' +
36 '<img id="{{name}}-img" src="{{imagepath}}" ng-click="imgClicked()">' ;
38 function basename(path) {
39 return path.split('/').reverse()[0];
42 angular.module('UploadFile',['ConfigApp'])
44 .directive('uploadFile', function(ConfigApp, $http, JQemu) {
45 function mymethods(scope, elem, attrs) {
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");
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!!!
58 // upload file to server
59 scope.UpLoadFile= function(files) {
62 for (var i = 0; i < files.length; 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)) {
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
75 scope.basename=basename(file.name);
76 scope.imgElem[0].file = file;
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!!!
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';
93 if (attrs.istoobig) scope.istoobig= ConfigApp.paths[attrs.category] + attrs.istoobig;
94 else scope.istoobig=ConfigApp.paths.avatars + 'istoobig.jpg';
96 scope.name= attrs.name || 'avatar';
97 scope.mime= attrs.mime || 'image';
98 scope.sizemax= attrs.sizemax || 100; // default max size 100KB
112 console.log ("UploadFile Loaded");