/*! * Webogram v0.4.1 - messaging web application for MTProto * https://github.com/zhukov/webogram * Copyright (C) 2014 Igor Zhukov * https://github.com/zhukov/webogram/blob/master/LICENSE */ 'use strict'; /* Directives */ angular.module('myApp.directives', ['myApp.filters']) .directive('myHead', function() { return { restrict: 'AE', templateUrl: templateUrl('head') }; }) .directive('myLangFooter', function() { return { restrict: 'AE', templateUrl: templateUrl('lang_footer') }; }) .directive('myFooter', function() { return { restrict: 'AE', templateUrl: templateUrl('footer') }; }) .directive('myDialog', function() { return { restrict: 'AE', templateUrl: templateUrl('dialog') }; }) .directive('myMessage', function($filter, _) { var dateFilter = $filter('myDate'), dateSplitHtml = '
', unreadSplitHtml = '
' + _('unread_messages_split') + '
', selectedClass = 'im_message_selected', focusClass = 'im_message_focus', unreadClass = 'im_message_unread', errorClass = 'im_message_error', pendingClass = 'im_message_pending'; return { templateUrl: templateUrl('message'), link: link }; function link($scope, element, attrs) { var selected = false, grouped = false, focused = false, error = false, pending = false, needDate = false, unreadAfter = false, applySelected = function () { if (selected != ($scope.selectedMsgs[$scope.historyMessage.id] || false)) { selected = !selected; element.toggleClass(selectedClass, selected); } }, needDateSplit, applyGrouped = function () { if (grouped != $scope.historyMessage.grouped) { if (grouped) { element.removeClass(grouped); } grouped = $scope.historyMessage.grouped; if (grouped) { element.addClass(grouped); } } if (needDate != ($scope.historyMessage.needDate || false)) { needDate = !needDate; if (needDate) { if (needDateSplit) { needDateSplit.show(); } else { needDateSplit = $(dateSplitHtml); $(needDateSplit[0].firstChild).text(dateFilter($scope.historyMessage.date)); if (unreadAfterSplit) { needDateSplit.insertBefore(unreadAfterSplit) } else { needDateSplit.prependTo(element); } } } else { needDateSplit.hide(); } } }, unreadAfterSplit; applySelected(); applyGrouped(); $scope.$on('messages_select', applySelected); $scope.$on('messages_regroup', applyGrouped); $scope.$on('messages_focus', function (e, focusedMsgID) { if ((focusedMsgID == $scope.historyMessage.id) != focused) { focused = !focused; element.toggleClass(focusClass, focused); } }); var deregisterUnreadAfter; if (!$scope.historyMessage.out && ($scope.historyMessage.unread || $scope.historyMessage.unreadAfter)) { var applyUnreadAfter = function () { if ($scope.peerHistory.peerID != $scope.historyPeer.id) { return; } if (unreadAfter != ($scope.historyUnreadAfter == $scope.historyMessage.id)) { unreadAfter = !unreadAfter; if (unreadAfter) { if (unreadAfterSplit) { unreadAfterSplit.show(); } else { unreadAfterSplit = $(unreadSplitHtml).prependTo(element); } } else { unreadAfterSplit.hide(); if (deregisterUnreadAfter) { deregisterUnreadAfter(); } } } }; applyUnreadAfter(); deregisterUnreadAfter = $scope.$on('messages_unread_after', applyUnreadAfter); } if ($scope.historyMessage.unread) { element.addClass(unreadClass); var deregisterUnread = $scope.$on('messages_read', function () { if (!$scope.historyMessage.unread) { element.removeClass(unreadClass); deregisterUnread(); if (deregisterUnreadAfter && !unreadAfter) { deregisterUnreadAfter(); } } }); } if ($scope.historyMessage.error || $scope.historyMessage.pending) { var applyPending = function () { if (pending != ($scope.historyMessage.pending || false)) { pending = !pending; element.toggleClass(pendingClass, pending); } if (error != ($scope.historyMessage.error || false)) { error = !error; element.toggleClass(errorClass, error); } if (!error && !pending) { deregisterPending(); } }, deregisterPending = $scope.$on('messages_pending', applyPending); applyPending(); } } }) .directive('myExternalEmbed', function () { var twitterAttached = false; var facebookAttached = false; var gplusAttached = false; var twitterPendingWidgets = []; var facebookPendingWidgets = []; var embedTag = Config.Modes.chrome_packed ? 'webview' : 'iframe'; function link ($scope, element, attrs) { var embedData = $scope.$eval(attrs.myExternalEmbed); if (!embedData) { return; } var html = ''; var callback = false; var needTwitter = false; switch (embedData[0]) { case 'youtube': var videoID = embedData[1]; html = '
<' + embedTag + ' type="text/html" frameborder="0" ' + 'src="https://www.youtube.com/embed/' + videoID + '?autoplay=0&controls=2" webkitallowfullscreen mozallowfullscreen allowfullscreen>
'; break; case 'vimeo': var videoID = embedData[1]; html = '
<' + embedTag + ' type="text/html" frameborder="0" ' + 'src="https://player.vimeo.com/video/' + videoID + '?title=0&byline=0&portrait=0" webkitallowfullscreen mozallowfullscreen allowfullscreen>
'; break; case 'instagram': var instaID = embedData[1]; html = '
<' + embedTag + ' type="text/html" frameborder="0" ' + 'src="https://instagram.com/p/' + instaID + '/embed/">
'; break; case 'vine': var vineID = embedData[1]; html = '
<' + embedTag + ' type="text/html" frameborder="0" ' + 'src="https://vine.co/v/' + vineID + '/embed/simple">
'; break; case 'soundcloud': var soundcloudUrl = embedData[1]; html = '
<' + embedTag + ' type="text/html" frameborder="0" ' + 'src="https://w.soundcloud.com/player/?url=' + encodeEntities(encodeURIComponent(soundcloudUrl)) + '&auto_play=false&hide_related=true&show_comments=false&show_user=true&show_reposts=false&visual=true">
'; break; case 'spotify': var spotifyUrl = embedData[1]; html = '
<' + embedTag + ' type="text/html" frameborder="0" allowtransparency="true" ' + 'src="https://embed.spotify.com/?uri=spotify:' + encodeEntities(encodeURIComponent(spotifyUrl)) + '">
'; break; case 'twitter': html = '
'; callback = function () { if (!twitterAttached) { twitterAttached = true; $('