/* 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.modernbangkok', link: function(scope, element, attrs) { // scope.menu = [ { "title": "หน้าหลัก", "path": "/", "state": "1" }, { "title": "นโยบาย", "path": "/policy", "state": "1" }, { "title": "บทความ", "path": "/article", "state": "1" }, { "title": "ผู้สมัคร", "path": "/candidates", "state": "0" }, { "title": "ความเคลื่อนไหว", "path": "/news", "state": "1" }, { "title": "สมัครอาสา", "path": "/volunteer", "state": "1" }, { "title": "เกี่ยวกับเรา", "path": "/about-us", "state": "1" }, { "title": "ติดต่อเรา", "path": "/contact-us", "state": "1" } ]; // scope.$on('$viewContentLoaded', function(event, viewConfig) { $timeout(function() { scope.navigationChange(); }, 600); // }); scope.$on( "$routeChangeStart", function(event, route, current) { scope.navigationChange(); }); // navigation background change scope.navigationChange = function() { // $('.header-page').css('height', $('header').height()); var path = $location.path().split("/"); // // if($location.path() == '/' || path[1] == 'candidate') { if($location.path() == '/') { // $('header').css('position', 'absolute'); $('nav').css('color', 'white'); }else { // $('header').css('position', 'relative'); $('nav').css('color', 'black'); } } $(document).ready(function(){ $('.sidenav').sidenav(); }); // } }; }]) .directive('sitemap', [function() { return { restrict: 'A', templateUrl: '/templates/layouts/sitemap.modernbangkok', 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.modernbangkok' // }; // }]) .directive('cookiePolicy', [function() { return { restrict: 'C', templateUrl: '/templates/layouts/cookie.modernbangkok', }; }]) /* 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'); }); } } }]) .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' }); }, 300); // } } }]) .directive('portfolio', [function() { return { restrict: 'A', link: function(scope, element, attrs) { attrs.$observe('portfolio', function(id) { if(id) { /* Portfolio */ scope.service.app.portfolio(id).then(function(respone) { var portfolio = respone.data; // console.log(portfolio); if(portfolio.length) { var $container = $('
').addClass('container'); // gallery carousel var $carousel = $('').addClass('card-gallery carousel carousel-slider relative'); var $gradient = $('').addClass('background-gradient-left-right'); var $btnPrev = $('').addClass('btn-prev absolute').append($('').attr('src', '/public/img/icon/icon-prev-white.png')); var $btnNext = $('').addClass('btn-next absolute').append($('').attr('src', '/public/img/icon/icon-next-white.png')); angular.forEach(portfolio, function(item) { var $item = $('').addClass('carousel-item relative'); var $image = $('').attr('src', item.image); // $item.append($image); $carousel.append($item); }); $container.append($carousel.append($btnPrev).append($btnNext)); element.append($container); // element.ready(function() { // carousel $carousel.carousel({ fullWidth: true }); $carousel.find('.btn-prev').click(function(e) { e.preventDefault(); e.stopPropagation(); $carousel.carousel('prev'); }); $carousel.find('.btn-next').click(function(e) { e.preventDefault(); e.stopPropagation(); $carousel.carousel('next'); }); }); // } }); } // }); } } }]) .directive('video', ['$timeout', function($timeout) { return { restrict: 'A', link: function(scope, element, attrs) { attrs.$observe('video', function(id) { if(id) { /* Video & Media */ scope.service.app.media(id).then(function(respone) { var media = respone.data; // console.log(media); if(media.length) { var videoHight = 675; if(scope.mobile) videoHight = 200; // var $headerTitle = $('').addClass('card-title fc-flexica semi-bold margin-0').text('คลิป'); var $videoPreview = $('').addClass('video-preview'); // var $programVideo = $('').addClass('program-video owl-carousel'); var $programVideo = $('').addClass('program-media row'); var $container = $('').addClass('container'); $timeout(function() { scope.previewVideo(media[0].url); // angular.forEach(media, function(item) { var url = item.url; // var $col = $('').addClass('item relative cursor-pointer overflow-hidden'); // var $backgroundBlack = $('').addClass('background-black').css('opacity', '0.5').css('z-index', '2'); // var $icon = $('').addClass('card-icon absolute-center').attr('src', '/public/img/icon/icon-play-video.png'); // var $thumbnail = $('').addClass('card-thumbnail black').attr('src', scope.loadThumbnail(item.thumbnail, url)); var $col = $('').addClass('col s6 m4 l4'); var $card = $('').addClass('card-video relative cursor-pointer overflow-hidden'); var $thumbnail = $('').addClass('card-thumbnail').attr('src', scope.loadThumbnail(item.thumbnail, url)); var $backgroundBlack = $('').addClass('background-black').css('opacity', '0.5').css('z-index', '2'); var $icon = $('').addClass('card-icon absolute-center').attr('src', '/public/img/icon/icon-play-video.png'); // $col.on('click', function() { scope.previewVideo(url); }); $card.append($thumbnail).append($backgroundBlack).append($icon); $programVideo.append($col.append($card)); }); // element.ready(function() { // // owl carousel // $programVideo.owlCarousel({ // loop: false, // margin: 10, // nav: false, // responsive:{ // 0:{ // items: 2 // }, // 600:{ // items: 3 // }, // 1000:{ // items: 3 // } // } // }); // }); }, 1000); scope.previewVideo = function(videoUrl) { $videoPreview.empty(); // // $timeout(function() { if(videoUrl.search("youtube") != -1) { var url = new URL(videoUrl); var videoId = url.searchParams.get("v"); var $iframe = $('').attr('src', 'https://www.youtube.com/embed/' + videoId).attr('width', '100%').attr('height', videoHight).attr('frameborder', '0').attr('allowfullscreen', 'allowfullscreen'); $videoPreview.append($iframe); } else if(videoUrl.search("facebook") != -1 || videoUrl.search("fb") != -1) { var url = 'https://services.dinsor.co.th/app/client/modernbangkok/resource/fb-plugin/fb-player?url=' + videoUrl + '&height=' + videoHight; var $video = $(''); $video.css({width:'100%', height: videoHight + 'px', border:'none'}); $video.attr('src', url); $videoPreview.append($video); } else { var $source = $('').attr('type', 'video/mp4').attr('src', videoUrl); var $video = $('').addClass('card-video responsive-video').attr('controls', 'controls').attr('muted', 'muted').attr('width', '100%'); // $video.append($source); $videoPreview.append($video); } // }, 1000); } scope.loadThumbnail = function(image, url) { var thumbnail = ' '; // if(image.search("null") == -1) { thumbnail = image; } else { if(url.search("youtube") != -1) { var url = new URL(url); var videoId = url.searchParams.get("v"); // thumbnail = 'https://img.youtube.com/vi/'+ videoId +'/sddefault.jpg'; } else if(url.search("facebook") != -1 || url.search("fb") != -1) { } } return thumbnail; } // $container.append($headerTitle).append($videoPreview).append($programVideo); element.append($container); } else { element.remove(); } }); } // }); } } }]) .directive('feed', ['$timeout', function($timeout) { return { restrict: 'A', link: function(scope, element, attrs) { // $.ajax({ // url: "https://graph.facebook.com/v16.0/335978833560634/feed?fields=permalink_url&access_token=e45d9439d94a530e5af6d39ceaf274d0", // method: "GET", // dataType: "json", // success: function(response) { // console.log(response); // }, // error: function(error) { // console.log(error.responseJSON.error.message); // } // }); attrs.$observe('feed', function(id) { if(id) { /* Feed Facebook */ scope.service.app.feed(id).then(function(respone) { const feed = respone.data; // console.log(feed); if(feed.length) { angular.forEach(feed, function(item) { const height = item.feed_height; var urlObject = new URL($(item.feed_script).first().attr('src')); var urlSearchParams = urlObject.searchParams; var postUrl = urlSearchParams.get('href'); // console.log(postUrl); // var url = 'https://services.dinsor.co.th/app/client/modernbangkok/resource/fb-plugin/fb-posts?url='+postUrl; var $iframe = $(''); $iframe.css({width:'100%', height: height + 'px', border:'none', overflow:'hidden'}); $iframe.attr('src', url); // element.append($iframe); }); } else { element.remove(); } }); } // }); } } }]) .directive('social', [function() { return { restrict: 'A', link: function(scope, element, attrs) { attrs.$observe('social', function(social) { if(social) { /* Social */ var contact = [ { "key": "website", "icon": "/public/img/icon/icon-website-black.png" }, { "key": "facebook", "icon": "/public/img/icon/icon-facebook-black.png" }, { "key": "instagram", "icon": "/public/img/icon/icon-instagram-black.png" }, { "key": "twitter", "icon": "/public/img/icon/icon-twitter-black.png" }, { "key": "line", "icon": "/public/img/icon/icon-line-black.png" }, { "key": "youtube", "icon": "/public/img/icon/icon-youtube-black.png" }, { "key": "tiktok", "icon": "/public/img/icon/icon-tiktok-black.png" } ]; angular.forEach(contact, function(type) { if(scope.candidate.social[type.key]) { var $li = $(''); var $link = $('').attr('href', scope.candidate.social[type.key]).attr('target', '_blank'); var $icon = $('').attr('src', type.icon); $link.append($icon); element.append($li.append($link)); } }); } }); } } }]) /* Web Content */ .directive('bannerCarousel', [function() { return { restrict: 'A', link: function(scope, element, attrs) { attrs.$observe('bannerCarousel', function(type) { // scope.service.app.banners(type).then(function(respone) { const banners = respone.data; // console.log(banners); if(banners.length) { const $carousel = $('').addClass('carousel carousel-slider'); // angular.forEach(banners, function(item) { const $item = $('').addClass('carousel-item').attr('href', item.url); const $image = $('').attr('src', item.image); // $item.append($image); $carousel.append($item); const img = new Image(); img.onload = function() { const height = (this.height / this.width) * 100; $carousel.css('height', height + 'vw'); }; img.src = item.image; }); element.append($carousel); element.ready(function() { // carousel $carousel.carousel({ fullWidth: true, indicators: true }); // auto next slide banner setInterval(function () { $carousel.carousel('next'); }, 7000) }); // } }); // }); // } } }]) .directive('policy', ['$routeParams', 'ngMeta', function($routeParams, ngMeta) { return { restrict: 'A', link: function(scope, element, attrs) { var id = $routeParams.id; // console.log(id); scope.service.app.policy(id).then(function(respone) { scope.policy = respone.data; // console.log(scope.policy); // setting meta tags dynamically if(scope.policy.title) { ngMeta.setTitle(scope.policy.title); } if(scope.policy.length == 0) { scope.alert('basic', 'warning', 'มีบางอย่างผิดพลาด', 'ไม่พบข้อมูล', '/policy'); } }); // } } }]) .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'); } } }); // } } }]) .directive('candidates', [function() { return { restrict: 'A', link: function(scope, element, attrs) { // scope.service.app.candidate().then(function(respone) { scope.candidates = respone.data; // console.log(scope.candidates); }); // } } }]) .directive('committee', [function() { return { restrict: 'A', link: function(scope, element, attrs) { // scope.service.app.committee().then(function(respone) { scope.committee = respone.data; // console.log(scope.committee); }); // } } }]) .directive('news', ['$timeout', '$window', '$routeParams', 'ngMeta', function($timeout, $window, $routeParams, ngMeta) { return { restrict: 'A', link: function(scope, element, attrs) { // const id = $routeParams.id; // console.log(id); scope.service.app.news(id).then(function(respone) { scope.news = respone.data; // console.log(scope.news); // setting meta tags dynamically if(scope.news.activity_title) { ngMeta.setTitle(scope.news.activity_title); } if(_.isUndefined(id)) { /* Calendar */ mobiscroll.setOptions({ locale: mobiscroll.localeTh, theme: 'windows', themeVariant: 'light', showOuterDays: false }); let events = []; angular.forEach(scope.news, function(item) { const activityStart = new Date(item.activity_start); const activityEnd = new Date(item.activity_end); // console.log(date); events.push({ start: new Date(activityStart.getFullYear(), activityStart.getMonth(), activityStart.getDate()), end: new Date(activityEnd.getFullYear(), activityEnd.getMonth(), activityEnd.getDate()), color: '#000000' }); }); // console.log(events); const calendarId = '#calendar'; const agendaId = '#agenda'; mobiscroll.datepicker(calendarId, { controls: ['calendar'], display: 'inline', onPageLoaded: function (event, inst) { // console.log(event); const currentMonth = event.month.getMonth(); const currentYear = event.month.getFullYear(); $(agendaId).empty(); let countEvent = 0; // const agenda = scope.news; scope.news.sort((a, b) => new Date(a.activity_start) - new Date(b.activity_start)); // console.log(agenda); angular.forEach(scope.news, function(item) { const activity_title = item.activity_title; const activity_start = new Date(item.activity_start); const activity_end = new Date(item.activity_end); const date_start = activity_start.getDate(); const date_end = activity_end.getDate(); const month_start = activity_start.getMonth(); const month_end = activity_end.getMonth(); const year_start = activity_start.getFullYear(); const year_end = activity_end.getFullYear(); if(year_start==currentYear && year_end==currentYear) { if(month_start==currentMonth) { const $tr = $('