cobuild-angular-stack
Version:
Base stack angular sass jade gulp
568 lines (505 loc) • 23.1 kB
JavaScript
(function () {
'use strict';
angular.module('uniko.authSatellizer')
.controller('WindowCtrl', function ($scope) {
$scope.place = {};
$scope.showPlaceDetails = function (param) {
$scope.place = param;
}
})
.controller('WeddingCtrl', WeddingCtrl)
.controller('ShareCtrl', ShareCtrl);
WeddingCtrl.$inject = ['lodash', 'Utils', '$scope', '$rootScope', '$stateParams', '$state', 'DiscountCode','CoupleAccount', 'LoopBackAuth', 'toastr', 'Upload', 'uiGmapLogger', '$http', 'uiGmapGoogleMapApi', '$uibModal', 'Auth', '$analytics', '$translate', '$timeout'];
function WeddingCtrl(lodash , Utils, $scope, $rootScope, $stateParams, $state, DiscountCode, CoupleAccount, LoopBackAuth, toastr, Upload, $log, $http, GoogleMapApi, $uibModal, Auth, $analytics, $translate, $timeout) {
var controller = this;
var height = null;
$scope.searchActive = false;
$scope.editableWeddingData = false;
$scope.url = null;
$scope.default_message = 'Escribe un mensaje de bienvenida a tus invitados, puedes contarles tus planes de luna de miel o una breve historia' +
' sobre ustedes. Si quieres algunas ideas geniales de mensajes de bienvenida visita nuestro blog.'
$scope.showEdit = false;
$scope.checkHeight = function () {
height = $('textarea.editable-input')[0].scrollHeight;
angular.element($('.textarea')[0]).height(height + 30 + 'px');
};
$scope.getSubcategories = function() {
DiscountCode.findById({
id: $stateParams.descountId,
filter: {
include: {
relation: 'discountSubCategory'
}
}
}).$promise.then(function(discount) {
swal({
title: "¡Bienvenido!",
text:'<span style="text-transform: uppercase">'+discount.discountSubCategory.description+' y UNIKO</span> te desean un excelente comienzo con tu planeación de boda, por esto nada mejor que tener tu dinero en efectivo.',
type: "success",
html: true
})
}, function(err){
console.log(err);
}).catch(function(err) {
console.log(err);
})
}
$scope.openEdit = function () {
$scope.messageIsEditable = true
};
$scope.$watch('account.weddingData.welcomeMessage', function (newVal, oldValue) {
$scope.messageIsEditable = !newVal;
});
$scope.updatePlace = function () {
$scope.account.weddingData = $scope.weddingData;
$scope.account.weddingData.date = $scope.date;
$scope.account.weddingData.isSetted = true;
$scope.editableWeddingData = false;
}
$scope.exportRSVP = function () {
$http({
url: 'http://uniko.co:3000/api/v2/coupleaccounts/' + $scope.account.id + '/rsvpAsxlsx?access_token=' + Auth.getAuthData().accessTokenId,
method: 'get',
responseType: "blob"
}).then(function (data) {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
var blob = data.data,
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = 'rsvp.xlsx';
a.click();
window.URL.revokeObjectURL(url);
$(a).remove();
});
};
$scope.date = new Date();
$scope.weddingData = {};
$scope.account = CoupleAccount.getCurrent();
$scope.account
.$promise
.then(function () {
if($scope.account.isAdmin){
$state.go('admin.coupleAccounts');
}
Utils.list().then(function (response) {
var list = response.data;
if (!$scope.account.is2FA && lodash.find(list, {name: 'is2FA'}).value.is2FA){
$state.go('activate');
}
})
$scope.getSubcategories();
$scope.weddingData = lodash.cloneDeep($scope.account.weddingData);
$scope.weddingData.coverPhoto = $scope.weddingData.coverPhoto ? $scope.weddingData.coverPhoto : "https://s3-us-west-2.amazonaws.com/uniko-prod/products/undefined/images/1463434858873?timestamp=1463434861201";
$('.bg-cover').css('background-image', 'url("' + $scope.weddingData.coverPhoto + '")');
$scope.url = 'http://front.uniko.co/' + $scope.account.url;
if ($scope.account.weddingData.date) {
$scope.weddingData.date = new Date($scope.account.weddingData.date);
$scope.date = new Date($scope.account.weddingData.date);
}
$rootScope.$broadcast('imageProfile', $scope.account.weddingData.profilePhoto);
if ($scope.account.weddingData.gpsaddress) {
$scope.map.markers = [$scope.account.weddingData.gpsaddress];
$scope.map.zoom = 16;
$scope.map.center = {
latitude: $scope.account.weddingData.gpsaddress.latitude,
longitude: $scope.account.weddingData.gpsaddress.longitude
};
}
var originalUrl = $scope.account.url;
$scope.$watch(function () {
return $scope.account ? JSON.stringify($scope.account.toJSON()) : null;
}, function (oldVal, newVal) {
if (oldVal === newVal) {
return;
}
oldVal = JSON.parse(oldVal);
newVal = JSON.parse(newVal);
delete oldVal.weddingData.profilePhoto;
delete oldVal.updatedAt;
delete newVal.weddingData.profilePhoto;
delete newVal.updatedAt;
var wedding = $scope.account.weddingData;
if (newVal.url === oldVal.url && $scope.account.id === originalUrl && (wedding.nameP1 || wedding.nameP2)) {
$scope.account.url = lodash.toLower(lodash.deburr(wedding.nameP1 + wedding.nameP2)).replace(/\s/g, '');
}
var toldVal = JSON.stringify(oldVal);
var tnewVal = JSON.stringify(newVal);
if (toldVal !== tnewVal) {
$scope.update();
}
});
});
var cDate = new Date();
cDate.setTime(cDate.getTime() + (1000 * 60 * 60 * 24 * 7));
cDate.setHours(0, 0, 0, 0);
$scope.saveMessage = function () {
$scope.account.weddingData.welcomeMessage = $scope.weddingData.welcomeMessage;
}
$scope.dateOptions = {
dateDisabled: false,
formatYear: 'yyyy',
minDate: new Date(),
initDate: cDate,
startingDay: 1,
showWeeks: false
};
$scope.newCover = null;
$scope.$watch('weddingData.date', function () {
if ($scope.weddingData && $scope.weddingData.date) {
$scope.account.weddingData.date = $scope.weddingData.date;
}
});
$scope.uploadPhotoProfile = function ($file) {
if ($file) {
$scope.profilePhoto = $file;
Upload
.upload({
url: 'http://uniko.co:3000/api/v2/coupleaccounts/' + $scope.account.id + '/wedding/addimageprofile',
data: {
image: $file
}
})
.then(function (response) {
getMixpanel().people.set({'Profile Photo Added': true});
$scope.account.weddingData.profilePhoto = response.data.url;
$rootScope.$broadcast('imageProfile', $scope.account.weddingData.profilePhoto);
showUpdateSuccess();
});
}
};
$scope.editBanner = function () {
var modalInstance = $uibModal.open({
animation: true,
templateUrl: 'partials/auth.wedding.banner.add.html',
controller: 'BannerNewController',
size: 'lg',
backdrop: 'static',
resolve: {
currentAccount: function () {
return $scope.account;
}
}
});
modalInstance.result
.then(function (result) {
getMixpanel().people.set({'Cover photo Added': true});
// if (result.isUpdated) {
// $scope.newCover = result.coverPhoto;
// } else {
$scope.account.weddingData.coverPhoto = result.coverPhoto;
// }
});
};
$scope.editPhoto = function () {
var modalInstance = $uibModal.open({
animation: true,
templateUrl: 'partials/auth.wedding.photo.add.html',
controller: 'PhotoNewController',
size: 'lg',
backdrop: 'static',
resolve: {
currentAccount: function () {
return $scope.account;
}
}
});
modalInstance.result
.then(function (result) {
});
};
$scope.update = function () {
var zpad = function(number) {
return String(number).length < 2 ? ("0"+number) : number;
}
var dateText = "";
var mesesText = ["enero", "febrero", "marzo", "abril", "mayo", "junio", "jilio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"];
if($scope.account.weddingData && $scope.account.weddingData.date ) {
var hours = zpad(new Date($scope.account.weddingData.date).getHours());
var minutes = zpad(new Date($scope.account.weddingData.date).getMinutes());
var day = zpad(new Date($scope.account.weddingData.date).getDate());
var mes = mesesText[new Date($scope.account.weddingData.date).getMonth()];
var year = new Date($scope.account.weddingData.date).getFullYear();
var iso = hours > 12 ? " p.m." : " a.m.";
dateText = day + " " + mes + " " + year + " " + hours + ":" + minutes + iso ;
console.log(day);
$scope.account.weddingData.dateText = dateText;
}
$timeout(function () {
CoupleAccount.updateMainAttributes($scope.account, showUpdateSuccess, function (err) {
});
}, 500);
};
function showUpdateSuccess() {
// toastr.success('Los datos se han almacenado correctamente');
$scope.url = 'http://front.uniko.co/' + $scope.account.url;
$scope.weddingData = lodash.cloneDeep($scope.account.weddingData);
if ($scope.account.weddingData.date) {
$scope.weddingData.date = $scope.date = new Date($scope.account.weddingData.date);
getMixpanel().people.set({'wedding date': $scope.weddingData.date});
getMixpanel().people.set({'wedding time': $scope.weddingData.date});
}
if (recentlyChanged.name) {
getMixpanel().people.set({'$firstname': recentlyChanged.name});
getMixpanel().people.set({'$lastname': recentlyChanged.name});
}
if (recentlyChanged.phone) {
getMixpanel().people.set({'$cellphone': recentlyChanged.phone});
}
if (recentlyChanged.weddingLocation) {
getMixpanel().people.set({'Wedding Location': recentlyChanged.weddingLocation.templateparameter.adr_address});
}
}
GoogleMapApi.then(function (maps) {
maps.visualRefresh = true;
$scope.defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(40.82148, -73.66450),
new google.maps.LatLng(40.66541, -74.31715));
$scope.map.bounds = {
northeast: {
latitude: $scope.defaultBounds.getNorthEast().lat(),
longitude: $scope.defaultBounds.getNorthEast().lng()
},
southwest: {
latitude: $scope.defaultBounds.getSouthWest().lat(),
longitude: -$scope.defaultBounds.getSouthWest().lng()
}
};
$scope.searchbox.options.bounds = new google.maps.LatLngBounds($scope.defaultBounds.getNorthEast(), $scope.defaultBounds.getSouthWest());
});
var styleMap = [{
"featureType": "water",
"elementType": "geometry",
"stylers": [{"color": "#e9e9e9"}, {"lightness": 17}]
}, {
"featureType": "landscape",
"elementType": "geometry",
"stylers": [{"color": "#f5f5f5"}, {"lightness": 20}]
}, {
"featureType": "road.highway",
"elementType": "geometry.fill",
"stylers": [{"color": "#ffffff"}, {"lightness": 17}]
}, {
"featureType": "road.highway",
"elementType": "geometry.stroke",
"stylers": [{"color": "#ffffff"}, {"lightness": 29}, {"weight": 0.2}]
}, {
"featureType": "road.arterial",
"elementType": "geometry",
"stylers": [{"color": "#ffffff"}, {"lightness": 18}]
}, {
"featureType": "road.local",
"elementType": "geometry",
"stylers": [{"color": "#ffffff"}, {"lightness": 16}]
}, {
"featureType": "poi",
"elementType": "geometry",
"stylers": [{"color": "#f5f5f5"}, {"lightness": 21}]
}, {
"featureType": "poi.park",
"elementType": "geometry",
"stylers": [{"color": "#dedede"}, {"lightness": 21}]
}, {
"elementType": "labels.text.stroke",
"stylers": [{"visibility": "on"}, {"color": "#ffffff"}, {"lightness": 16}]
}, {
"elementType": "labels.text.fill",
"stylers": [{"saturation": 36}, {"color": "#333333"}, {"lightness": 40}]
}, {"elementType": "labels.icon", "stylers": [{"visibility": "off"}]}, {
"featureType": "transit",
"elementType": "geometry",
"stylers": [{"color": "#f2f2f2"}, {"lightness": 19}]
}, {
"featureType": "administrative",
"elementType": "geometry.fill",
"stylers": [{"color": "#fefefe"}, {"lightness": 20}]
}, {
"featureType": "administrative",
"elementType": "geometry.stroke",
"stylers": [{"color": "#fefefe"}, {"lightness": 17}, {"weight": 1.2}]
}]
$scope.window = {
show: false,
options: {
pixelOffset: {width: 0, height: -40}
},
templateurl: 'window.tpl.html',
templateparameter: {},
closeClick: function () {
$scope.window.show = false;
}
};
$scope.map = {
control: {},
center: {
latitude: 19.4284700,
longitude: -99.1276600
},
zoom: 7,
dragging: false,
bounds: {},
markers: [],
idkey: 'place_id',
events: {
idle: function (map) {
},
dragend: function (map) {
//update the search box bounds after dragging the map
var bounds = map.getBounds();
var ne = bounds.getNorthEast();
var sw = bounds.getSouthWest();
$scope.searchbox.options.bounds = new google.maps.LatLngBounds(sw, ne);
//$scope.searchbox.options.visible = true;
}
},
options: {
styles: styleMap
}
};
$scope.searchbox = {
template: 'searchbox.tpl.html',
options: {
autocomplete: true,
types: ['(regions)'],
country: 'mx'
},
events: {
places_changed: function (autocomplete) {
var place = autocomplete.getPlaces()[0];
if (place.address_components) {
var bounds = new google.maps.LatLngBounds();
var marker = {
idKey: place.place_id,
place_id: place.place_id,
name: place.address_components[0].long_name,
latitude: place.geometry.location.lat(),
longitude: place.geometry.location.lng(),
templateurl: 'window.tpl.html',
templateparameter: place,
events: {
click: function (marker) {
$scope.window.templateparameter = marker.model.templateparameter;
$scope.window.show = true;
}
}
};
bounds.extend(place.geometry.location);
$scope.map.center = {
latitude: place.geometry.location.lat(),
longitude: place.geometry.location.lng()
};
$scope.map.zoom = 16;
$scope.map.markers = [marker];
$scope.weddingData.gpsaddress = marker;
} else {
console.log("do something else with the search string: " + place.name);
}
}
}
};
var recentlyChanged = {
name: false,
phone: false,
weddingLocation: false
};
$scope.$watchGroup([
'account',
'account.weddingData.nameP1',
'account.weddingData.nameP2',
'account.phoneNumber1',
'account.phoneNumber2',
'account.weddingData.gpsaddress'
], watchEvents);
function watchEvents(newVal, oldVals) {
if (!newVal[0] || oldVals[0]) {
return;
}
if (!recentlyChanged.name && newVal[1] != oldVals[1]) {
recentlyChanged.name = newVal[1];
}
if (!recentlyChanged.name && newVal[2] != oldVals[2]) {
recentlyChanged.name = newVal[2];
}
if (!recentlyChanged.phone && newVal[3] != oldVals[3]) {
recentlyChanged.phone = newVal[3];
}
if (!recentlyChanged.phone && newVal[4] != oldVals[4]) {
recentlyChanged.phone = newVal[4];
}
if (!recentlyChanged.weddingLocation && newVal[5] != oldVals[5]) {
recentlyChanged.weddingLocation = newVal[5];
}
}
}
ShareCtrl.$inject = ['$scope', '$uibModal', 'toastr', '$translate', '$state', 'CoupleAccount'];
function ShareCtrl($scope, $uibModal, toastr, $translate, $state, CoupleAccount) {
var execShare = function () {
var modalInstance;
if (!($scope.coupleAccount || $scope.account).payInformationData.isPaid) {
modalInstance = $uibModal.open({
animation: true,
templateUrl: 'partials/auth.pay.modal.html',
controller: 'CoupleCheckoutModalController',
controllerAs: 'ccCtrl',
backdrop: 'static',
size: 'lg'
});
return modalInstance.result
.then(function (result) {
$translate('copy-url').then(function (translation) {
toastr.success(translation);
});
});
}
modalInstance = $uibModal.open({
animation: true,
templateUrl: 'partials/auth.share.html',
controller: ['$scope', '$uibModalInstance', 'url', 'Socialshare', '$translate', 'coupleAccount', function ($scope, $uibModalInstance, url, Socialshare, $translate, coupleAccount) {
$scope.url = url;
$scope.coupleAccount = coupleAccount;
console.log(coupleAccount);
$scope.copied = false;
$scope.$watch('copied', function (newVal) {
if (newVal) {
$scope.copied = false;
$uibModalInstance.close();
}
});
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
$scope.shareMedia = function (provider) {
};
}],
resolve: {
url: function () {
return $scope.url;
},
coupleAccount: function () {
return $scope.coupleAccount || $scope.account;
}
}
});
modalInstance.result
.then(function (result) {
$translate('copy-url').then(function (translation) {
toastr.success(translation);
});
});
};
$scope.share = function () {
if ($scope.coupleAccount || $scope.account) {
$scope.coupleAccount = $scope.coupleAccount || $scope.account;
return execShare();
}
$scope.coupleAccount = CoupleAccount.getCurrent();
$scope.coupleAccount
.$promise
.then(function () {
$scope.url = 'http://front.uniko.co/' + $scope.coupleAccount.url;
execShare();
});
};
}
})();