(function () { 'use strict'; app.TicketCtrl = TicketCtrl; angular.module('app.controller').controller('TicketCtrl', TicketCtrl); TicketCtrl.$inject = ['$scope', '$state', '$stateParams', '$timeout', 'cfg', 'ticket', 'mapHelper']; function TicketCtrl($scope, $state, $stateParams, $timeout, cfg, ticket, mapHelper) { cfg.debug && console.log('TicketCtrl', $stateParams); var reId = /^(\d+)-/; $scope.data = null; $scope.form = { headMore: { map: true } }; $scope.onGroupClick = function (headId) { $scope.form.headMore[headId] = !$scope.form.headMore[headId]; }; var prepareTicket = function (ticket, tagList) { ticket.typeCode2Group = {}; ticket.attributeCode2Tag = {}; angular.forEach(tagList, function (group) { ticket.typeCode2Group[group.typeCode] = group; angular.forEach(group.list, function (tag) { if (tag.attributeCode) ticket.attributeCode2Tag[tag.attributeCode] = tag; }); }); ticket.mainTagsJoined = _.reduce(['rootgeo', 'estatetype', 'submissiontype'], function (str, type) { if (ticket.typeCode2Group[type]) { return (str ? str + ', ' : '') + $scope.joinTagNames(ticket.typeCode2Group[type].list); } return str; }, ''); }; $scope.joinTagNames = function (list) { return _.pluck(list, 'name').join(', '); }; var mapInstance; $scope.mapAfterInit = function (map) { mapInstance = map; cfg.debug && console.log(name, '.mapAfterInit'); }; var unWatchCoordsReady = $scope.$watch(function () { return !!($scope.data && $scope.data.ticket); }, function (coordsReady) { if (coordsReady && $scope.data.ticket.latitude && $scope.data.ticket.longitude) { $scope.data.ticket.mapPoint = { geometry: { type: 'Point', coordinates: mapHelper.coord2array($scope.data.ticket) } }; unWatchCoordsReady(); } }); var unWatchMapReady = $scope.$watch(function () { return !!(mapInstance && $scope.data && $scope.data.ticket.mapPoint); }, function (mapReady) { if (mapReady) { mapInstance.setCenter($scope.data.ticket.mapPoint.geometry.coordinates); unWatchMapReady(); } }); var initGallery = function () { jQuery('#ticket-gallery').royalSlider({ fullscreen: { enabled: true, nativeFS: true }, thumbs: { spacing: 6, arrowsAutoHide: true, firstMargin: false }, controlNavigation: 'thumbnails', autoScaleSlider: true, autoScaleSliderWidth: 300, autoScaleSliderHeight: 275, loop: true, loopRewind: true, imageScaleMode: 'fit-if-smaller', numImagesToPreload: 2, arrowsNav: true, arrowsNavAutoHide: true, arrowsNavHideOnTouch: true, keyboardNavEnabled: true, fadeinLoadedSlide: true, imageScalePadding: 0, slidesSpacing: 0, autoHeight: true, imgWidth: 300 }); }; var match; if ((match = $stateParams.alias.match(reId))) { ticket.newRead(match[1], {tags: true, images: true, users: true}).then(function (data) { if (data.ticket.alias !== $stateParams.alias) { $state.go('ticket', {alias: data.ticket.alias}); return; } $scope.data = data; prepareTicket($scope.data.ticket, $scope.data.tags); $timeout(initGallery, 100); }, function (data) { // todo: show 404 page }); } } })();