cobuild-angular-stack
Version:
Base stack angular sass jade gulp
371 lines (326 loc) • 14.5 kB
JavaScript
(function () {
'use strict';
angular.module('uniko.authSatellizer')
.controller('ProfileCtrl', ProfileCtrl)
.controller('UpdatePasswordController', ProfileUpdatePasswordCtrl);
ProfileCtrl.$inject = ['$scope', 'CoupleAccount', 'LoopBackAuth', 'toastr', '$uibModal', 'uiGmapGoogleMapApi', '$analytics', '$translate', '$timeout','lodash'];
function ProfileCtrl($scope, CoupleAccount, LoopBackAuth, toastr, $uibModal, GoogleMapApi, $analytics, $translate, $timeout,lodash) {
var controller = this;
var originalUrl = null;
$scope.editUrl = false;
var cDate = new Date();
cDate.setTime(cDate.getTime() + (1000 * 60 * 60 * 24 * 7));
cDate.setHours(0, 0, 0, 0);
$scope.dateOptions = {
dateDisabled: false,
formatYear: 'yyyy',
minDate: new Date(),
initDate: cDate,
startingDay: 1
};
$scope.searchActive = false;
$scope.weddingHour = null;
$scope.url = null;
$scope.user = CoupleAccount.getCurrent(function () {
originalUrl = $scope.user.url;
$scope.url = 'http://front.uniko.co/' + originalUrl;
console.log($scope.user.weddingData.gpsaddress);
if ($scope.user.weddingData.gpsaddress) {
$scope.map.markers = [$scope.user.weddingData.gpsaddress];
$scope.map.zoom = 16;
$scope.map.center = {
latitude: $scope.user.weddingData.gpsaddress.latitude,
longitude: $scope.user.weddingData.gpsaddress.longitude
};
}
$scope.$watch('user.url', function (newVal, oldVal) {
$scope.profileForm.url.$setValidity('custom', true);
if (newVal !== oldVal) {
CoupleAccount
.isValidURL({url: newVal})
.$promise
.then(function (response) {
if (!response.isValid) {
CoupleAccount.prototype$getURLSuggestion({
id: $scope.user.id,
url: newVal,
limit: 3
}, function (response) {
$scope.profileForm.url.$setValidity('custom', false);
$scope.urlSuggestions = response.suggestions;
}, function () {
});
}
});
}
});
$scope.$watch('weddingHour', function (newVal, oldVal) {
if (!newVal)return;
var d = $scope.user.weddingData.date;
if (!d) {
d = $scope.user.weddingData.date = new Date();
}
d.setHours(newVal.getHours(), newVal.getMinutes(), newVal.getSeconds(), newVal.getMilliseconds());
});
$scope.$watch(function () {
return JSON.stringify($scope.user);
}, function (newValue, oldValue) {
newValue = JSON.parse(newValue);
oldValue = JSON.parse(oldValue);
var wedding = $scope.user.weddingData;
if (newValue.url === oldValue.url && $scope.user.id === originalUrl && (wedding.nameP1 || wedding.nameP2)) {
$scope.user.url = lodash.toLower(lodash.deburr(wedding.nameP1 + wedding.nameP2)).replace(/\s/g, '');
}
});
});
$scope.$watch('copied', function (newVal) {
if (newVal) {
$translate('copy-url').then(function (translation) {
toastr.success(translation);
});
$scope.copied = false;
}
});
$scope.setUrl = function (url) {
$scope.user.url = url;
};
//validar que la URL sea valida
$scope.user.$promise.then(function () {
if ($scope.user.weddingData.date) {
var d = $scope.user.weddingData.date = new Date($scope.user.weddingData.date);
$scope.weddingHour = new Date();
$scope.weddingHour.setHours(d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds());
}
});
$scope.update = function () {
var u = CoupleAccount.updateMainAttributes($scope.user, function () {
if (recentlyChanged.name) {
getMixpanel().people.set({
'$firstname': recentlyChanged.name,
'$lastname': recentlyChanged.name
});
}
if (recentlyChanged.phone) {
getMixpanel().people.set({'$cellphone': recentlyChanged.phone});
}
if (recentlyChanged.email) {
getMixpanel().people.set({'$email': recentlyChanged.email});
}
if (recentlyChanged.weddingLocation) {
getMixpanel().people.set({'Wedding Location': recentlyChanged.weddingLocation.templateparameter.adr_address});
}
originalUrl = $scope.user.url;
$scope.editUrl = false;
$scope.url = 'http://front.uniko.co/' + originalUrl;
if ($scope.user.weddingData.date) {
var d = $scope.user.weddingData.date = new Date($scope.user.weddingData.date);
$scope.weddingHour = new Date();
$scope.weddingHour.setHours(d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds());
}
$translate('success-save-data').then(function (translation) {
toastr.success(translation);
});
}, function (resp) {
if (resp.status === 422) {
var codes = resp.data.error.details.codes;
lodash.forEach(codes, function (errorArray, key) {
lodash.forEach(errorArray, function (error) {
$scope.profileForm[key].$setValidity(error, false)
});
});
if ($scope.profileForm.url.$error.custom) {
CoupleAccount.prototype$getURLSuggestion({
id: $scope.user.id,
url: $scope.user.url,
limit: 3
}, function (response) {
$scope.urlSuggestions = response.suggestions;
}, function () {
});
}
}
});
};
$scope.updatePassword = function () {
var modalInstance = $uibModal.open({
animation: true,
templateUrl: 'partials/auth.updatepassword.html',
controller: 'UpdatePasswordController',
size: 'md',
backdrop: 'static',
resolve: {
user: function () {
return $scope.user;
}
}
});
modalInstance.result
.then(function (result) {
});
};
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());
});
$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;
}
}
};
$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.user.weddingData.gpsaddress = marker;
} else {
console.log("do something else with the search string: " + place.name);
}
}
}
};
var recentlyChanged = {
name: false,
phone: false,
weddingLocation: false,
email: false
};
$scope.$watchGroup([
'user',
'user.weddingData.nameP1',
'user.weddingData.nameP2',
'user.phoneNumber1',
'user.phoneNumber2',
'user.weddingData.gpsaddress',
'user.email',
'user.email2'
], 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];
}
if (!recentlyChanged.email && newVal[6] != oldVals[6]) {
recentlyChanged.email = newVal[6];
}
if (!recentlyChanged.email && newVal[7] != oldVals[7]) {
recentlyChanged.email = newVal[7];
}
}
}
ProfileUpdatePasswordCtrl.$inject = ['$scope', 'CoupleAccount', 'LoopBackAuth', 'toastr', '$uibModalInstance', 'user', '$translate', '$timeout'];
function ProfileUpdatePasswordCtrl($scope, CoupleAccount, LoopBackAuth, toastr, $uibModalInstance, user, $translate, $timeout) {
var controller = this;
$scope.update = function () {
$timeout(function () {
user.password = $scope.password;
if (!user.password) {
$translate('error-new-password').then(function (translation) {
return toastr.success(translation);
});
} else if (user.password !== $scope.passwordConfirm) {
$translate('error-differents-passwords').then(function (translation) {
return toastr.success(translation);
});
}
CoupleAccount.updateMainAttributes(user, function () {
$translate('success-password').then(function (translation) {
toastr.success(translation);
});
$uibModalInstance.close();
}, function (err) {
toastr.error(err);
});
}, 500);
};
$scope.dismiss = function () {
$uibModalInstance.dismiss();
};
}
})();