AnswerPairPicture.Controller.js 3.55 KB
Newer Older
Thitichaipun Wutthisak committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
'use strict'

angular.module('EXAM').controller('AnswerPairPicture', ['$scope', '$http', 'EXAMSERVICES', function($scope, $http, EXAMSERVICES){
  $scope.question = new Question()
  $scope.tmpobjuploadl = {} // tmp for keep left obj
  $scope.tmpobjuploadr = {} // tmp for keep right obj
  $scope.pathleft = ''
  $scope.pathright = ''
  $scope.isupload = {
    left: false,
    right: false
  }

  $scope.addPairPicture = function(obj){
    let max = 0;
    if (obj.right.length > 0){
      max = Math.max.apply(Math, obj.right.map(function(ansobj){ return ansobj.id}))
    }
    $scope.question.addPairPicture(obj, max)
  }

  $scope.deletePairPicture = 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.openUploadPairLeft = function(obj, name){
    if (!$scope.isupload.left){
      $scope.tmpobjuploadl = obj
      $scope.pathleft = pathfile.pairpicture.left
      $('input[name="'+name+'"]').click()
    }
  }


  $scope.openUploadPairRight = function(obj, name){
    if (!$scope.isupload.right){
      $scope.tmpobjuploadr = obj
      $scope.pathright = pathfile.pairpicture.right
      $('input[name="'+name+'"]').click()
    }
  }

  $scope.readRightFile = function(name){
    $scope.tmpobjuploadr.waitingload = true
    $scope.isupload.right = 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.pathright,
        filename: ranfile,
        filetype: filetype
      }

      EXAMSERVICES.UploadFileExam.save(objfile).$promise.then(function(result){
        $scope.tmpobjuploadr.picture = result.filename+'.'+result.filetype
        $scope.tmpobjuploadr.waitingload = false
        $scope.isupload.right = false
      })
    }
    fd.readAsDataURL($('input[name="'+name+'"]')[0].files[0])
  }

  $scope.readLeftFile = 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])
  }
}])