(function () { 'use strict'; var streamApp = angular.module('streamApp'); streamApp.directive('commentsDirective', ['streamService', 'FileUploader', 'apiProxy', CommentsDirective]); function CommentsDirective(streamService, FileUploader, apiProxy) { return { restrict: 'E', scope: { moment: '=', dropId: '@', stream: '=', zoom: '=' }, templateUrl: '/App/Directives/commentsDirective.html?v=6.0.0c', link: function (scope, element, attrs, tabsCtrl) { scope.thank = function () { if (!scope.moment.editable) { streamService.thank(scope.dropId).then(result => { scope.youThanked = result.kind == 1; let top = scope.thanks[0]; if (!top || top.foreign) { if (result.kind == 1) { scope.thanks.unshift(result); } } else { if (result.kind == 2) { scope.thanks.shift(); } } }); } }; let comments = scope.moment.comments || []; scope.comments = comments.filter(({ kind }) => !kind); scope.thanks = comments.filter(({ kind }) => kind == 1).sort((a, b) => a.foreign - b.foreign); scope.youThanked = scope.moment.editable || (scope.thanks[0] && !scope.thanks[0].foreign); scope.show = () => scope.showWho = !scope.showWho; scope.comment = { text: '', commenting: false }; scope.uploading = []; scope.model = function () { this.commentId = 0; this.dropId = 0; }; scope.addComment = function () { scope.comment.commenting = true; }; scope.editComment = function (comment) { scope.comments.forEach(x => { if (x.editing) { x.comment = scope.temporaryText || ''; } x.editing = false; }); scope.temporaryText = comment.comment; comment.editing = true; scope.model.commentId = comment.commentId; }; scope.finishEditComment = function (comment) { if (validateComment(comment.comment)) { streamService.updateComment(comment.commentId, comment.comment).then(function (data) { scope.temporaryText = data.data.comment; uploadPhotos(data.data, scope.dropId); }) .catch(error => alert('Error saving comment.')) .finally(() => { comment.editing = false; comment.comment = scope.temporaryText; }); } }; scope.finishAddComment = function () { var comment = scope.comment.text; if (validateComment(comment)) { streamService.addComment(scope.comment.text, scope.dropId).then(function (data) { uploadPhotos(data.data, scope.dropId); scope.comments.push({ comment: comment, commentId: data.data.commentId, foreign: data.data.foreign, ownerName: data.data.ownerName }); }).catch(error => alert('Error saving comment.')); scope.comment.text = ''; scope.comment.commenting = false; } }; function validateComment(comment) { return (comment.length <= 750 && comment.length > 0) || scope.uploader.queue.length > 0; } function uploadPhotos(result, dropId) { scope.model.commentId = result.commentId; scope.model.dropId = dropId; finishUploading(scope.uploader, dropId); if (scope.uploader.queue.length > 0) { scope.uploading.push(true); scope.uploader.uploadAll(); } } scope.refresh = function (dropId) { const dropIdInt = parseInt(dropId); if (scope.stream) { const index = scope.stream.findIndex(moment => moment.drop === dropIdInt); if (index >= 0) { streamService.getMemory(dropId).then(crud => scope.stream[index] = crud); } } else if (scope.callback) { scope.callback(); } }; scope.removeComment = function (commentId) { streamService.removeComment(commentId).then(function (data) { var index = scope.comments.findIndex(c => c.commentId === commentId); scope.comments.splice(index, 1); }) .catch(error => alert('Error deleting comment.')); }; scope.cancel = function () { scope.comment.commenting = false; scope.comment.text = ''; cleanupUploader(); }; scope.cancelEdit = function (comment) { comment.comment = scope.temporaryText; comment.editing = false; cleanupUploader(); }; scope.pop = function (comment) { comment.showAll = !comment.showAll; }; scope.getStyle = function (name) { return { 'background-color': scope.styleDictionary[name[2].toUpperCase()] }; }; scope.styleDictionary = { "A": "#112233", "B": "#552233", "C": "#992233", "D": "#115511", "E": "#559911", "F": "#662211", "G": "#116633", "H": "#888833", "I": "#992288", "J": "#113388", "K": "#332211", "L": "#553311", "M": "#661133", "N": "#331155", "O": "#663333", "P": "#773311", "Q": "#883333", "R": "#993311", "S": "#114433", "T": "#224411", "U": "#334433", "V": "#444411", "W": "#554433", "X": "#664411", "Y": "#774433", "Z": "#884411" }; const re = /([0-9])+/; if (scope.comments) { scope.comments.forEach((item, i) => { var date = item.date; if (!(date instanceof Date)) { const dateInt = parseInt(item.date.match(re)); date = new Date(dateInt); } item.Date = date.toDateString(); }); } refreshUploader(); function refreshUploader() { scope.uploader = new FileUploader({ url: apiProxy.getBaseUrl() + '/api/images', formData: [scope.model], headers: apiProxy.getAuthHeader(), name: 'comment-uploader' }); } function finishUploading(uploader, dropId) { uploader.onCompleteAll = function () { cleanupUploader(); scope.uploading.pop(); scope.refresh(dropId); }; } function cleanupUploader() { scope.uploader.cancelAll(); if (scope.uploader.queue.length) { scope.uploader.clearQueue(); } }; } }; }; })();