/* global _, angular, i18n, Ladda, Odometer */ 'use strict'; angular.module('DinsorApp.directives', []) /* Layouts */ .directive('headBar', ['$location', '$timeout', function($location, $timeout) { return { restrict: 'A', templateUrl: 'templates/layouts/header.suchatvee', link: function(scope, element, attrs) { // navigation change position scope.navigationChange = function() { if($location.path() == '/') { $('header').removeClass('absolute').addClass('relative'); }else { $('header').removeClass('relative').addClass('absolute'); } } scope.$on('$viewContentLoaded', function (event, viewConfig) { $timeout(function() { scope.navigationChange(); }, 400); }); scope.$on("$routeChangeStart", function (event, route, current) { scope.navigationChange(); }); scope.menu = [ { "title": "หน้าหลัก", "path": "/", "state": "1" }, { "title": "บทความ / ความรู้", "path": "/article", "state": "1" } ]; $(document).ready(function(){ $('.sidenav').sidenav(); }); // } }; }]) .directive('sitemap', [function() { return { restrict: 'A', templateUrl: '/templates/layouts/sitemap.suchatvee', link: function(scope, element, attrs) { attrs.$observe('sitemap', function(program) { //scope.service.program.data('sitemap', institution).then(function(data) { //console.log(data) // scope.sitemap = data; //}); }); } }; }]) .directive('copyright', [function() { return { restrict: 'C', templateUrl: '/templates/layouts/copyright.suchatvee' }; }]) .directive('cookiePolicy', [function() { return { restrict: 'C', templateUrl: '/templates/layouts/cookie.suchatvee', }; }]) /* 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; // 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'); var img = new Image(); img.onload = function() { const height = this.height; element.css('height', height + 'px'); }; img.src = image; element.css('background-image', "url('"+image+"')"); 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() { var height = (this.height / this.width) * 100; if(attrs.height) { element.css('height', attrs.height + 'px'); }else { element.css('height', height + 'vw'); } } if (image.indexOf("http://") != 0 || image.indexOf("https://") != 0) { img.src = 'https:' + image; } img.src = image; element.css("background-image", "url('"+image+"')"); // background position if(attrs.position == 'center') { element.addClass('bg-cover-center'); }else { element.addClass('background-image width'); } element.removeAttr('background'); }); } } }]) .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); // } } }]) /* Web Content */ .directive('campaign', ['$timeout', '$window', function($timeout, $window) { return { restrict: 'A', link: function(scope, element, attrs) { // get campaign info scope.service.app.campaign().then(function(response) { // console.log(response); scope.campaign = response.data; const progress = scope.campaign.supporter * 100 / scope.campaign.target; const result_progress = Math.ceil(progress); let progress_bar= document.getElementById("progress_bar"); // progress_bar.style.setProperty('--progressAfter',result_progress+'%') }); // get article scope.service.app.article().then(function(respone) { // scope.article = respone.data; const articles = respone.data; var random = Math.floor(Math.random() * articles.length); scope.article = articles[random]; // console.log(scope.article); }); // registration scope.submitForm = function() { const formId = '#form_campaign'; let valid = 0; let formData = new FormData(); let register = {}; // $.each($(formId).serializeArray(), function (index, key) { if(key.name == 'email' || key.name == 'line_id' || key.name == 'phone'){ if(key.value != ''){ valid++; } } // register[key.name] = key.value; }); formData.append('form_id', "5b19566288045560c4e645ac2fda36fa"); formData.append('data', JSON.stringify(register)); if (valid > 0) { scope.service.app.registration(formData).then(function(response) { if(!response.error) { // scope.alert('toast', 'success', '', response.msg, '/register-complete'); $window.location.href = '/register-complete'; } else { scope.alert('basic', 'error', 'มีบางอย่างผิดพลาด', response.msg, null); } }); }else{ scope.alert('basic', 'error', 'มีบางอย่างผิดพลาด', 'กรอกช่องทางในการประสานงานอย่างน้อย 1 ช่องทาง', null); } } // } } }]) .directive('article', ['$routeParams', 'ngMeta', function($routeParams, ngMeta) { return { restrict: 'A', link: function(scope, element, attrs) { var id = $routeParams.id; // console.log(id); scope.service.app.article(id).then(function(respone) { scope.article = respone.data; // console.log(scope.article); // article highlight scope.article_highlight = []; for (let i=0; i<5; i++) { if(scope.article[i] != undefined) scope.article_highlight.push(scope.article[i]); } // console.log(scope.article_highlight); if(id) { // setting meta tags dynamically if(scope.article.article_title) { ngMeta.setTitle(scope.article.article_title); } if(scope.article.length == 0) { scope.alert('basic', 'warning', 'มีบางอย่างผิดพลาด', 'ไม่พบข้อมูล', '/article'); } } }); // } } }]) /* Plugin */ .directive('sharethisSocial', ['$timeout', '$window', function($timeout, $window) { return { restrict: 'A', link: function(scope, element, attrs) { // const $sharethis = $('
').addClass('sharethis-inline-share-buttons'); $timeout(function() { element.append($sharethis); }, 1000); } } }]) .directive('commentsPlugin', ['$timeout', '$location', function($timeout, $location) { return { restrict: 'A', link: function(scope, element, attrs) { const currentUrl = $location.absUrl(); const height = 500; // var url = '//services.dinsor.co.th/app/client/suchatvee/library/fb-plugin/fb-comments?url='+currentUrl; var $iframe = $('