'use strict' angular.module('EXAM').controller('AnswerPairPictureText', ['$scope', '$http', 'EXAMSERVICES', function($scope, $http, EXAMSERVICES){ $scope.tmpobjuploadl = {} // tmp for keep left obj $scope.tmpobjuploadr = {} // tmp for keep right obj $scope.pathleft = '' $scope.isupload = { left: false, right: false } $scope.question = new Question() $scope.addPairPictureText = function(obj, index){ let max = 0 if (obj.right.length > 0){ max = Math.max.apply(Math, obj.right.map(function(ansobj){ return ansobj.id })) } $scope.question.addPairPictureText(obj, max) } $scope.deletePairPictureText = function(obj, index){ $scope.question.deleteArray(obj.left, index) $scope.question.deleteArray(obj.right, index) } $scope.deleteArray = function(obj, index){ $scope.question.deleteArray(obj, index) } $scope.readPicture = function(name){ $scope.tmpobjuploadl.waitingload = true $scope.isupload.left = true let fd = new FileReader() let objfile = $('input[name="'+name+'"]')[0].files[0] let filename = objfile.name.replace(/ /g, '') let filetype = objfile.name.substring(objfile.name.indexOf('.')+1, objfile.name.length) let ranfile = '' filename = filename.substring(0, filename.indexOf('.')) for (let i=0; i<filename.length; i++){ ranfile += filename.charAt(Math.floor(Math.random() * filename.length)) } fd.onload = function(){ let dataurl = fd.result dataurl = dataurl.substring(dataurl.indexOf(',')+1, dataurl.length) let objfile = { filedata: dataurl, despath: $scope.pathleft, filename: ranfile, filetype: filetype } EXAMSERVICES.UploadFileExam.save(objfile).$promise.then(function(result){ $scope.tmpobjuploadl.picture = result.filename+'.'+result.filetype $scope.tmpobjuploadl.waitingload = false $scope.isupload.left = false }) } fd.readAsDataURL($('input[name="'+name+'"]')[0].files[0]) } $scope.openUploadPairPicText = function(obj, hashkey){ if (!$scope.isupload.left){ $scope.tmpobjuploadl = obj $scope.pathleft = pathfile.pairpicture.lefttext $('input[name="'+hashkey+'"]').click() } } }])