'use strict' angular.module('EXAM').controller('Question', ['$scope', '$http', '$sce', '$interval', 'GETLANG', function($scope, $http, $sce, $interval, GETLANG){ $scope.lang = getLang() $scope.swlang = GETLANG /*$scope.mouseDownEvent = function(){ let promise = new Promise((resolve, reject) => { }) }*/ }]) .directive('labelImg', function(){ return { restrict: 'E', scope: { obj: '=obj' }, controller: 'PreviewExam', templateUrl: 'label.img.html' } }) .directive('labelAudio', function(){ return { restrict: 'E', scope: { obj: '=obj' }, controller: 'PreviewExam', templateUrl: 'label.audio.html' } }) .directive('labelVideo', function(){ return { restrict: 'E', scope: { obj: '=obj' }, controller: 'PreviewExam', templateUrl: 'label.video.html' } }) .directive('textbox', function(){ return { restrict: 'E', scope: { obj: '=obj' }, controller: 'PreviewExam', templateUrl: 'question.text.html' } }) .directive('singleChoice', function(){ return { restrict: 'E', scope: { obj: '=obj' }, controller: 'PreviewExam', templateUrl: 'question.single.choice.html' } }) .directive('multiChoice', function(){ return { restrict: 'E', scope: { obj: '=obj' }, controller: 'PreviewExam', templateUrl: 'question.multi.choice.html' } }) .directive('sortSentence', function(){ return { restrict: 'E', scope: { obj: '=obj' }, controller: 'PreviewExam', templateUrl: 'question.sortsentence.html', link: function(scope, elem, attr){ let initSel = {tha:'ไม่ระบุ', eng:'none'} scope.selobj = [{id: '', isselected: false, label: initSel[scope.lang]}] console.log(scope.obj.sentence) for (let i=0; i<scope.obj.sentence.length; i++){ scope.selobj.push({id: scope.obj.sentence[i].value, isselected: false, label: scope.obj.sentence[i].value}) } console.log(scope.selobj) function shufferArray(obj){ let i, tmpobj, count = obj.length while (count > 0){ i = Math.floor(Math.random() * count--) tmpobj = obj[i] obj[i] = obj[count] obj[count] = tmpobj } return obj } scope.updateSelected = function(be_val, af_val){ let beobj = scope.selobj.find((obj) => obj.id == be_val) let afobj = scope.selobj.find((obj) => obj.id == af_val) beobj.isselected = false if (afobj.id != ''){ afobj.isselected = true } } scope.obj.sentence = shufferArray(scope.obj.sentence) } } }) .directive('pairText', function(){ return { restrict: 'E', scope: { obj: '=obj' }, controller: 'PreviewExam', templateUrl: 'question.pair.text.html', link: function(scope, elem, attr){ scope.selobj = [{id: '', isselected: false, label: {eng: 'none', tha: 'ไม่ระบุ'}}] function shufferArray(obj){ let i, tmpobj, count = obj.length while (count > 0){ i = Math.floor(Math.random() * count--) tmpobj = obj[i] obj[i] = obj[count] obj[count] = tmpobj } return obj } scope.updateSelected = function(be_val, af_val){ let beobj = scope.selobj.find((obj) => obj.id == be_val) let afobj = scope.selobj.find((obj) => obj.id == af_val) beobj.isselected = false if (afobj.id != ''){ afobj.isselected = true } } scope.obj.left = shufferArray(scope.obj.left) scope.obj.right = shufferArray(scope.obj.right) for (let i=0; i<scope.obj.right.length; i++){ scope.selobj.push({id: scope.obj.right[i].id, isselected: false, label: scope.obj.right[i].label}) } } } }) .directive('pairPicture', function($interval){ return { restrict: 'E', scope: { obj: '=obj' }, controller: 'Question', templateUrl: 'question.pair.picture.html', link: function(scope, elem, attr){ let promise = null let time = 0 let lobj = null, robj = null scope.selectList = function(direction, obj){ let max = Math.max.apply(Math, scope.obj.left.map((objlist) => { return objlist.selected.numpair})) return new Promise((resolve, reject) => { if (direction === 'left'){ if (obj.answer != ''){ let objright = scope.obj.right.find((right) => right.id === obj.answer) obj.selected.isselected = false obj.selected.numpair = 0 objright.selected.isselected = false objright.selected.numpair = 0 obj.answer = '' } else if (lobj != null && lobj.answer == ''){ lobj.selected.isselected = false if (lobj.id != obj.id){ lobj = obj lobj.selected.isselected = true } else { lobj = null } } else if (lobj == null){ lobj = obj lobj.selected.isselected = true } } else if (direction === 'right'){ let objleft = scope.obj.left.find((left) => left.answer === obj.id) if (objleft != undefined){ obj.selected.isselected = false obj.selected.numpair = 0 objleft.selected.isselected = false objleft.selected.numpair = 0 objleft.answer = '' } else if (robj != null){ robj.selected.isselected = false if (robj.id != obj.id){ robj = obj robj.selected.isselected = true } else { robj = null } } else if (robj == null){ robj = obj robj.selected.isselected = true } } if (lobj != null && robj != null){ lobj.answer = robj.id lobj.selected.numpair = ++max robj.selected.numpair = max lobj = null robj = null } resolve(true) }) } scope.checkLeftPair = function(obj){ let classname = '' if (obj.answer != ''){ classname = 'blue-4' } return classname } scope.checkRightPair = function(obj){ let classname = '' let objleft = scope.obj.left.find((left) => left.answer == obj.id) if (objleft != undefined){ classname = 'blue-4' } return classname } scope.mouseDownEvent = function(direction, obj){ time = 0 if (promise){ $interval.cancel(promise) } promise = $interval(function(){ time++ if (time > 0){ scope.selectList(direction, obj).then(() => { console.log('select success') $interval.cancel(promise) return; }) } }, 700) } scope.mouseUpEvent = function(){ $interval.cancel(promise) promise = null } scope.checkObj = function(){ console.log(lobj, robj) } } } }) .directive('pairPicturetext', function(){ return { restrict: 'E', scope: { obj: '=obj' }, controller: 'PreviewExam', templateUrl: 'question.pair.picturetext.html', link: function(scope, elem, attr){ scope.selobj = [{id: '', isselected: false, label: {eng:'none', tha: 'ไม่ระบุ'}}] function shuffterArray(obj){ let i, tmpobj, count = obj.length while (count > 0){ i = Math.floor(Math.random() * count--) tmpobj = obj[i] obj[i] = obj[count] obj[count] = tmpobj } return obj } scope.updateSelected = function(be_val, af_val){ let beobj = scope.selobj.find((obj) => obj.id == be_val) let afobj = scope.selobj.find((obj) => obj.id == af_val) beobj.isselected = false if (afobj.id != ''){ afobj.isselected = true } } scope.obj.left = shuffterArray(scope.obj.left) scope.obj.right = shuffterArray(scope.obj.right) for (let i=0; i<scope.obj.right.length; i++){ scope.selobj.push({id: scope.obj.right[i].id, isselected: false, label: scope.obj.right[i].label}) } } } }) .directive('subquestion', function(){ return { restrict: 'E', scope: { obj: '=obj', level: '=level', pindex: '=pindex', gpindex: '=gpindex' }, controller: 'Question', templateUrl: 'preview.question.html' } })