/* global _, angular, i18n, Ladda, Odometer */ 'use strict'; angular.module('PromptSkill.directives', []) /* layouts */ .directive('promptskillBar', [function() { return { restrict: 'C', templateUrl: '/templates/layouts/promptskill-bar.promptskill' }; }]) .directive('privacy', ['$cookies', function($cookies) { return { restrict: 'C', templateUrl: '/templates/layouts/cookie-policy.promptskill', link: function(scope, element, attrs) { // Cookie Policy scope.checkCookie = function() { var accept = $cookies.get('cookie_consent'); if (accept != 'accept') { scope.cookie_popup = true; } } scope.acceptCookie = function() { let expire = new Date(); const domain = '.promptskill.com'; // expire.setDate(expire.getDate() + 1); $cookies.put('cookie_consent', 'accept', { domain: domain, expires: expire, path: '/' }); scope.cookie_popup = false; } // } }; }]) .directive('copyright', [function() { return { restrict: 'C', templateUrl: '/templates/layouts/copyright.promptskill' }; }]) .directive('promptskill', [function() { return { restrict: 'A', link: function(scope, element, attrs) { attrs.$observe('promptskill', function(logo) { var $logo = $(''); if (logo) { $logo.attr('src', '//services.promptskill.com/app/public/img/logo/logo-topbar.png'); if (logo == 'id') { $logo.attr('src', '//services.promptskill.com/app/public/img/logo/logo-promptskill-id.png'); } else { $logo.addClass('top-bar click') $logo.on('click', function() { scope.promptSkill(); }) }; element.append($logo); } }); } } }]) .directive('headBar', ['$location', '$timeout', function($location, $timeout) { return { restrict: 'A', template: '
', link: function(scope, element, attrs) { scope.$watch('headbar', function(newValue, oldValue) { //console.log(newValue) if (newValue != oldValue) { if (newValue) { scope.navigation = 'templates/layouts/header.promptskill'; // Initialization Mobile Navigator $timeout(function() { $('.sidenav').sidenav(); }, 1000); } else { scope.navigation = ''; } } }); scope.service.program.info().then(function(program) { scope.service.frontpage.content(program.program_id, 'menu', null, program.institute.inst_id).then(function(content) { // console.log(content); if (program.institute.inst_id == "fcc13db5-7bb5-4dd6-88b9-ca12289a26bb") { var _content = []; content.forEach((item) => { // console.log(item.institute) temp = {}; temp.title = item.title; temp.link = '//promptskill.com' + item.link; temp.rec_status = item.rec_status; _content.push(temp); }); scope.home = '//promptskill.com'; content = _content; } else { scope.home = '/'; } // console.log(content); var menu = []; var temp = {}; content.forEach((item) => { // console.log(item.institute) temp = {}; temp.title = item.title; temp.path = item.link; temp.state = item.rec_status == 'active' ? 1 : 0; menu.push(temp); }); // console.log(menu) scope.menu = menu; }); }); // scope.menu = [ // { "title": "หน้าหลัก", "path": "/", "state": "1" }, // { "title": "หลักสูตร", "path": "/courses", "state": "1" }, // // { "title": "วิธีการเรียน", "path": "/how-to-learn", "state": "1" }, // { "title": "วิทยากร", "path": "/faculty-of-lecturers", "state": "0" }, // { "title": "คำถามที่พบบ่อย", "path": "/frequently-asked-questions", "state": "1" }, // { "title": "เกี่ยวกับเรา", "path": "/about-us", "state": "1" }, // { "title": "ติดต่อเรา", "path": "/contact-us", "state": "1" } // ]; // Header Change scope.navigationChange = function() { // const path = $location.path().split('/'); const pathArray = ['course', 'enrol', 'register']; const host = $location.host().split('.').length; // // console.log($location.host().split('.').length) if (pathArray.includes(path[1]) || host > 2) { $('header').addClass('program-header'); // $('header').css('position', 'absoluzte').find('.page-wrapper').css('padding', '0 2em'); } else { $('header').removeClass('program-header'); // $('header').css('position', 'relative').find('.page-wrapper').css('padding', '0 8.625rem'); } } // } }; }]) .directive('headbarLogo', [function() { return { restrict: 'A', link: function(scope, element, attrs) { var logo = scope.service.program.logo() ? scope.service.program.logo() : '//services.promptskill.com/app/public/img/logo/logo-topbar.png'; // console.log(logo) if (logo) { var $img = $('
').append($('').attr('height', '100%').attr('src', logo).attr('class', 'top-bar')); element.append($img.addClass('valign-wrapper')); } } } }]) .directive('headbarLogoRight', [function() { return { restrict: 'A', link: function(scope, element, attrs) { var logo = ''; //'https://services.promptskill.com/app/public/img/logo/logo-topbar.png'; if (logo) { var $img = $('
').append($('').attr('src', logo)); element.append($img); } } } }]) .directive('sitemap', [function() { return { restrict: 'A', templateUrl: '/templates/layouts/sitemap.promptskill', link: function(scope, element, attrs) { // scope.service.program.info().then(function(program) { // console.log(program); scope.sitemap = program; }); // attrs.$observe('sitemap', function(program) { // scope.service.program.data('sitemap', institution).then(function(data) { // console.log(data) // scope.sitemap = data; // }); // }); // } }; }]) /* elements */ .directive('image', [function() { return { restrict: 'A', link: function(scope, element, attrs) { attrs.$observe('image', function(image) { var img = new Image(); img.onload = function() { var ratio = this.height / this.width; if (attrs.height) { ratio = ratio * parseInt(attrs.height) / 100; } console.log(ratio) element.css('padding-top', ratio * 100 + '%'); element.css('position', 'relative'); } img.src = 'https:' + image; element.removeAttr('image'); element.css('background-image', "url('" + image + "')").addClass('cover'); // element.removeAttr('image'); }); } } }]) .directive('background', [function() { return { restrict: 'A', link: function(scope, element, attrs) { attrs.$observe('background', function(image) { var img = new Image(); img.onload = function() { if (attrs.height == 'fit') { //element.height(this.height); //element.css('height', this.height + ''); //element.addClass('width'); element.css('background-size', 'auto ' + this.height + 'px') } else if (attrs.height == 'auto') { element.css('background-size', 'auto ' + this.height + 'px'); element.removeClass('background-image'); } //else element.addClass(attrs.height); } img.src = 'https:' + image; element.removeAttr('background'); element.css("background-image", "url('" + image + "')"); element.addClass('background-image'); }); } } }]) .directive('icon', [function() { return { restrict: 'A', link: function(scope, element, attrs) { attrs.$observe('icon', function(image) { var $img = $(''); var img = new Image(); img.onload = function() { $img.attr('src', image); element.append($img); } img.src = image; element.removeAttr('icon'); }); } } }]) .directive('aos', ['$timeout', function($timeout) { return { restrict: 'A', link: function(scope, element, attrs) { // $timeout(function() { AOS.init({ // disable: 'mobile' }); }, 900); // } } }]) /* style */ .directive('fitBrief', ['$timeout', '$sanitize', function($timeout, $sanitize) { return { restrict: 'A', priority: 10, link: function(scope, element, attrs) { scope.$watch(attrs.ngBindHtml, function(value) { var fontSize = parseInt(element.css('font-size')); var margin = element.parent().css('margin'); //console.log(margin) $timeout(function() { if (value != undefined) { var blockBrief = element.prev(); var brief = blockBrief.find('.brief'); var marginTop = (blockBrief.height() - brief.height()) / fontSize; element.children().children().children(':first-child').css('margin-top', -marginTop + 'em').css('margin-left', '-4%'); // catchphrase if (scope.course.catchphrase) { let catchphrase_text = scope.course.catchphrase; const tag = (/xhtml/i).test(document.doctype) ? '
' : '
'; const $catchphrase = $('

').addClass('overline-text underline-text short dbadman bold'); // catchphrase_text = (catchphrase_text + '').replace(/(\r\n|\n\r|\r|\n| | | | )/g, tag + '$1'); $catchphrase.html($sanitize(catchphrase_text)); $catchphrase.css('margin-top', '0.8em'); element.children().children().children(':nth-child(2)').prepend($catchphrase); } } }) }) } } }]) /* page */ .directive('page', [function() { return { restrict: 'A', link: function(scope, element, attrs) { var paddingTop = 64; attrs.$observe('page', function(name) { element.removeAttr('page').attr('id', name); scope.service.program.page(name).then(function(data) { console.log(data) scope.page = data; if (data) { element.addClass(data.headbar) if ($('header').outerHeight() < paddingTop) { paddingTop += $('header').outerHeight(); } else { paddingTop = $('header').outerHeight(); } element.css('padding-top', paddingTop); } }); }); } }; }]) .directive('pageBanner', [function() { return { restrict: 'A', link: function(scope, element, attrs) { var designWidth = 1920; attrs.$observe('pageBanner', function(banner) { if (banner) { element.removeAttr('page-banner') //if(attrs.fit=='width') { //var img = new Image(); //img.onload = function() { // var height = this.height / designWidth; // element.css('height', height *100+'vw'); //} //img.src = 'https:' + banner; element.css('background-image', "url('" + banner + "')").addClass(attrs.fit); } }); } } }]) .directive('bookCover', [function() { return { restrict: 'A', link: function(scope, element, attrs) { attrs.$observe('bookCover', function(cover) { if (cover != '') { var image = scope.activity.resource.cover; var title = scope.activity.resource.title; var file = scope.activity.resource.file; var $document = $('
').css('padding', '1em'); var $img = $('').css('width', '10em').attr('src', image); var $title = $('
').css('margin-bottom', '1.5em').text(title); var $download = $('').append($('').attr('src', '//services.promptskill.com/app/program/public/img/icon/icon-book-open.png')); $download.attr('href', 'https:' + file); $download.attr('target', '_blank'); $document.append($img).append($title).append($download); element.append($document); } }); } } }]) .directive('alertPanel', ['$timeout', function($timeout) { return { restrict: 'A', link: function(scope, element, attrs) { scope.$watch('alert.msg', function(msg, old) { //console.log(msg) //console.log(old) var $panel = angular.element('#course-navigator .panel') //console.log($panel.innerWidth()) if (msg) { //element.empty(); // console.log(msg); angular.element('
').css('width', '100%').html(msg).appendTo(element); element.addClass('alert-panel valign-wrapper'); $panel.addClass('show'); //scope.timeout(3000, scope.$parent.alert = {}); $timeout(function() { $panel.removeClass('show'); scope.alert.msg = ''; element.empty(); }, 7000); } /*if(msg=="") { if($panel.hasClass('show')) { $panel.removeClass('show'); element.empty(); //console.log(msg) } }*/ }); } } }]) .directive('timeCountdown', ['$timeout', function($timeout) { return { restrict: 'A', link: function(scope, element, attrs) { // scope.$watch('alert.time', function(msg, old) { var $iframe = angular.element('#moobed'); var $card = $('
').addClass('card-timecountdown orange lighten-5 white z-depth-1 valign-wrapper'); var $icon = $('').addClass('icon-timecountdown').attr('src', '//services.promptskill.com/app/client/dbdacademy/public/img/icon/icon-alert-warning.png').attr('width', '30'); var $title = $('

').addClass('black-text normal regular').text('คุณมีเวลาในการทำข้อสอบ ' + '30' + ' นาที'); var $time = $('').addClass('timecountdown bold').text('00 : 30 : 00'); $title.append($time); $card.append($icon).append($title); // $iframe.append($card); element.append($card); // }); } } }]) .directive('toggle', [function() { return { restrict: 'C', link: function(scope, element, attr) { element.on('click', function() { //console.log(element) if (element.hasClass('current')) { element.removeClass('current') //element.addClass('done') } else { $(element).parent().find('.toggle.current').removeClass('current'); element.addClass('current') } }); } } }]) /* register */ .directive('registerInfo', ['$location', function($location) { return { restrict: 'A', link: function(scope, element, attrs) { //console.log($location.hash()); setTimeout(function() { scope.service.member.register($location.hash()).then(function(info) { console.log(info) // var data = JSON.parse(info.data); $("#prefix").val(data.prefix); $("#firstname").val(data.firstname); $("#lastname").val(data.lastname); $("#company").val(data.company); $("#province").val(data.province); $("#contact").val(data.contact); }) }, 1000); } } }]) .directive('register', ['$location', function($location) { return { restrict: 'A', link: function(scope, element, attrs) { // if(scope.service.user.token()=='') //scope.gotoUrl('/'); var program = ''; scope.$parent.promptskill = true; scope.$parent.headbar = false; scope.$parent.fullpage = true; scope.$parent.member = false; // attrs.$observe('register', function(action) { scope.service.program.info(action).then(function(data) { // console.log(data) scope.program = data; program = data.domain.toLowerCase(); scope.formUrl = '/register-form/' + program + '.' + action; // scope.name = data.name; scope.background = data.background; scope.banner = data.banner; scope.register['code'] = data.registration; }); }); // let formdata = new FormData(); let files = []; var upload = false; var valid_national_id = false; scope.formReady = function() { // upload = angular.element('input.input-file').length == 0; angular.element('input#email').on('change', function() { angular.element('input[name="username"]').val($(this).val()).prop('readonly', true) .removeClass('validate invalid').removeAttr('required'); }); // input file drag & drop image if (angular.element('input.input-file').length) { let button = document.querySelector('.title-top button'), form = document.getElementById("file_img"), section = document.getElementById(".card-input-file section"), container = document.querySelector('.container-image'), text = document.querySelector('.inner'), browse = document.querySelector('.select'), input = document.querySelector('.card-input-file section input'); browse.addEventListener('click', () => input.click()); //input change event input.addEventListener('change', () => { let file = input.files; for (let i = 0; i < file.length; i++) { if (files.every(e => e.name != file[i].name)) { if (file[i].size > 16000000) { scope.alert('รูปภาพมีขนาดใหญ่เกินไป'); } else { files.push(file[i]); } } } //document.getElementById("file_img").value = ""; showImage(); if (files.length > 0) { upload = true; } //console.log(files); }) function showImage() { let images = ''; files.forEach((e, i) => { images += `

image ×
` }) container.innerHTML = images; } //delete image // function delImage(index) { // files.splice(index, 1); // showImage(); // console.log(files); // } window.delImage = (index) => { files.splice(index, 1); showImage(); //console.log(files); if (files.length == 0) { upload = false; } }; //drag and drop image document.getElementById("section-file").addEventListener('dragover', e => { e.preventDefault(); $('#section-file').addClass('dragover'); $('.inner').css('display', 'none'); $('.drop-here').css('display', 'block'); }) document.getElementById("section-file").addEventListener('dragleave', e => { e.preventDefault(); $('#section-file').removeClass('dragover'); $('.inner').css('display', 'block'); $('.drop-here').css('display', 'none'); }) document.getElementById("section-file").addEventListener('drop', e => { e.preventDefault(); $('#section-file').removeClass('dragover'); $('.inner').css('display', 'block'); $('.drop-here').css('display', 'none'); let file = e.dataTransfer.files; for (let i = 0; i < file.length; i++) { // if (files.every(e => e.name != file[i].name)) files.push(file[i]) if (files.every(e => e.name != file[i].name)) { if (file[i].size > 16000000) { scope.alert('รูปภาพมีขนาดใหญ่เกินไป'); } else { files.push(file[i]); } } } showImage(); if (files.length > 0) { upload = true; } console.log(files); }) } // // angular.element('input.input-file').on('change', function() { // var file = $(this)[0].files[0]; // //formdata.append('files[]', file); // files.forEach((e, i) => formdata.append('files[]', e)); // upload = true; // }) //valid national ID if (angular.element('input[name="id_card_number"]').length) { $("#id_card_number").keyup(function(e) { var input = $("#id_card_number"); var id = input.val(); if (id.length >= 13) { for (var i = 0, sum = 0; i < 12; i++) { sum += parseInt(id.charAt(i)) * (13 - i); } let mod = sum % 11; let check = (11 - mod) % 10; if (check == parseInt(id.charAt(12))) { valid_national_id = false; input.removeClass("invalid"); input.addClass("valid"); $("#helper").attr('data-error', 'ต้องการ'); } else { valid_national_id = true; input.removeClass("valid"); input.addClass("invalid"); $("#helper").attr('data-error', 'เลขประจำตัวประชาชนไม่ถูกต้อง'); } } }); } } // scope.register = function(state) { var accepted = true; var national_id = true; var confirm_password = true; var formCheck = angular.element('.form-checkbox').find('input'); angular.forEach(formCheck, function(input) { accepted = accepted && angular.element(input).prop("checked"); }); // if (upload) { if (accepted) { var required = 0; var $formregister = angular.element('.form-register').find(':input'); angular.forEach($formregister, function(input) { var $input = angular.element(input); if ($input.prop('required') && $input.val() == '') { if (!$input.prop('disabled')) { $input.addClass('invalid'); required++; } } }); // var $formselect = angular.element('.form-register').find('select'); angular.forEach($formselect, function(input) { var $input = angular.element(input); if ($input.prop('required') && $input.val() == null) { if (!$input.prop('disabled')) { $input.addClass('invalid'); required++; } } }); // check input length national id if (angular.element('input[name="id_card_number"]').length) { if (angular.element('input[name="id_card_number"]').val().length != 13) { national_id = false; scope.alert('เลขประจำตัวประชาชนไม่ครบ 13 หลัก'); } } //check confirm password if (angular.element('.promptskill-id').length) { if (angular.element('input[name="password"]').val() != angular.element('input[name="confirm_password"]').val()) { confirm_password = false; scope.alert('รหัสยืนยันไม่ถูกต้อง'); } } // var $forminput = angular.element('.promptskill-id').find('input'); angular.forEach($forminput, function(input) { var $input = angular.element(input); switch ($input.attr('name')) { case 'firstname': case 'lastname': var value = angular.element('input#' + $input.attr('name')).val(); $input.val(value); break; default: if ($input.prop('required') && $input.val() == '') { $input.addClass('invalid'); required++; } } }); // if (required != 0) { scope.alert('กรุณากรอกข้อมูลให้ครบทุกช่อง'); //scope.helper = {'msg':'กรุณากรอกข้อมูลให้ครบทุกช่อง'}; } else { if (national_id && confirm_password) { // check valid national id if (valid_national_id) { scope.alert('เลขประจำตัวประชาชนไม่ถูกต้อง'); } else { // save data registration var $btnReg = angular.element('.btn-register').find('button'); $btnReg.attr('disabled', true).text('กำลังบันทึกข้อมูล..'); // get files upload if ($('.file-field input[type="file"]').length) { $('.file-field input[type="file"]').each(function(i, e) { // if (e.files.length > 0) { // const inputName = $(e).attr('name'); // // const filename = e.files[0].name; // // const newFilename = inputName + '_' + filename; // // // files.push(new File([e.files[0]], inputName)); // } files.push(e.files[0]); }); } // console.log(files); if (scope.program.type != 'seminar') { scope.service.member.signup($forminput).then(function(data) { scope.service.member.registration($formregister, data.userid).then(function(id) { //console.log(id) files.forEach((e, i) => formdata.append(`file[${i}]`, e)); scope.service.member.upload(formdata, id).then(function(result) { //console.log(result) scope.result = result; scope.scrollToTop().then(function() { scope.state = 'completed'; scope.stateUrl = '/register-form/' + program + '.result'; }); }) }) }); } else { scope.service.member.registration(scope.program.program_id, $formregister, null).then(function(id) { // console.log(id) $btnReg.attr('disabled', true).text('กำลังอัปโหลดไฟล์..'); files.forEach((e, i) => formdata.append(`file[${i}]`, e)); scope.service.member.upload(formdata, id).then(function(result) { // console.log(result) scope.result = result; scope.scrollToTop().then(function() { scope.state = 'completed'; scope.stateUrl = '/register-form/' + program + '.result'; }); }) }) } } // } } } else scope.alert('ยอมรับเงื่อนไขการให้บริการ'); } else scope.alert('ต้องการอัปโหลดรูปภาพ'); } // scope.alert = function(text) { Swal.fire({ title: 'มีบางอย่างผิดพลาด', text: text, icon: 'warning', confirmButtonText: 'ตกลง', confirmButtonColor: "#9E9E9E", heightAuto: false }) } // } }; }]) /* form input */ .directive('formHelper', ['$window', function($window) { return { restrict: 'A', link: function(scope, element, attrs) { attrs.$observe('formHelper', function(text) { var helper = scope.helper; if (text) { if (!helper.hasOwnProperty('type')) { helper.title = 'มีบางอย่างผิดพลาด'; helper.type = 'warning'; } Swal.fire({ title: helper.title, text: text, icon: helper.type, confirmButtonText: 'ตกลง', confirmButtonColor: "#9E9E9E", heightAuto: false }).then(function() { //console.log(text) if (scope.helper.close) { $window.top.close(); } if (scope.helper.redirect) { $window.href = '/' + scope.helper.redirect; } scope.helper = {} }); } }); } } }]) .directive('textField', [function() { return { restrict: 'A', link: function(scope, element, attrs) { element.addClass('input-field') attrs.$observe('textField', function(label) { if (label) { var $input = $('').attr({ type: 'text' }).val(attrs.value); var $label = $('