Skip to content

Instantly share code, notes, and snippets.

@Bane83
Created October 21, 2016 07:35
Show Gist options
  • Select an option

  • Save Bane83/c37e05f7dcac259273e75f0e7a7bfef0 to your computer and use it in GitHub Desktop.

Select an option

Save Bane83/c37e05f7dcac259273e75f0e7a7bfef0 to your computer and use it in GitHub Desktop.
var map;
var i=0;
var modelData = {};
var PlaceEditView = Backbone.View.extend({
el: $('#content'),
imgs: [],
initialize: function () {
this.render();
newPlace.on('change', this.render, this); //change:profile_img
},
events: {
'click .place-submit': 'submitForm',
'click .places-add': 'initMap',
'click .add-amenities': 'addTags',
'click .save-tags': 'saveTags',
'click .add-file': 'selectFile',
'change #profile-img': 'newCropUpload',
'click .change-bg': 'newUploadFiles',
'click .add-picture': 'newUploadFiles',
'click .remove-picture': 'removePicture',
'click .add-categories': 'addCategories',
'click .language-tabs div.btn': 'switchLanguage',
'click .toggle-publish-place':'togglePublishPlace',
'click .add-group': 'addGroup',
'click .toggle-choice.border-light-gray': 'toggleChoice',
'keypress input': 'changeSaveButton',
'keypress textarea': 'changeSaveButton',
'click .reze': 'changeSaveButton1',
//'submit #place_form_data' : 'temporaryData'
},
temporaryData:function(){
modelData = {},
Repertoire = {},
WorkingHour = {};
modelData.description00srp = $("#description00srp").val() && $("#description00srp").val().length !== 0 ? $("#description00srp").val() : "";
modelData.description00eng = $("#description00eng").val() && $("#description00eng").val().length!==0 ? $("#description00eng").val() : "";
modelData.email = $('#email').val() && $('#email').val().length !== 0 ? $('#email').val() : "";
modelData.email_to = $('#email_to').val() && $('#email_to').val().length !== 0 ? $('#email_to').val() : "";
modelData.name00eng = $("#name00eng").val() && $("#name00eng").val().length !==0 ? $("#name00eng").val() : "";
modelData.name00srp = $("#name00srp").val() && $("#name00srp").val().length !==0 ? $("#name00srp").val() : "";
modelData.phone = $("#phone").val() && $("#phone").val().length !==0 ? $("#phone").val() : "";
modelData.phone_cell = $("#phone_cell").val() && $("#phone_cell").val().length !==0 ? $("#phone_cell").val() : "";
modelData.promotions00eng = $("#promotions00eng").val() && $("#promotions00eng").val().length !==0 ? $("#promotions00eng").val() : "";
modelData.promotions00srp = $("#promotions00srp").val() && $("#promotions00srp").val().length !==0 ? $("#promotions00srp").val() : "";
modelData.address = $("#address").val() && $("#address").val().length !==0 ? $("#address").val() : "";
modelData.reservation_active = $("#reservation").val() && $("#reservation").val().length !==0 ? $("#reservation").val() : "";
modelData.website = $("#website").val() && $("#website").val().length !==0 ? $("#website").val() : "";
Repertoire.monday00eng = $("#Repertoire.monday00eng").val() && $("#Repertoire.monday00eng").val().length !==0 ? $("#Repertoire.monday00eng").val() : "";
Repertoire.tuesday00eng = $("#Repertoire.tuesday00eng").val() && $("#Repertoire.tuesday00eng").val().length !==0 ? $("#Repertoire.tuesday00eng").val() : "";
Repertoire.wednesday00eng = $("#Repertoire.wednesday00eng").val() && $("#Repertoire.wednesday00eng").val().length !==0 ? $("#Repertoire.wednesday00eng").val() : "";
Repertoire.thursday00eng = $("#Repertoire.thursday00eng").val() && $("#Repertoire.thursday00eng").val().length !==0 ? $("#Repertoire.thursday00eng").val() : "";
Repertoire.friday00eng = $("#Repertoire.friday00eng").val() && $("#Repertoire.friday00eng").val().length !==0 ? $("#Repertoire.friday00eng").val() : "";
Repertoire.saturday00eng = $("#Repertoire.saturday00eng").val() && $("#Repertoire.saturday00eng").val().length !==0 ? $("#Repertoire.saturday00eng").val() : "";
Repertoire.sunday00eng = $("#Repertoire.sunday00eng").val() && $("#Repertoire.sunday00eng").val().length !==0 ? $("#Repertoire.sunday00eng").val() : "";
Repertoire.monday00srp = $("#Repertoire.monday00srp").val() && $("#Repertoire.monday00srp").val().length !==0 ? $("#Repertoire.monday00srp").val() : "";
Repertoire.tuesday00srp = $("#Repertoire.tuesday00srp").val() && $("#Repertoire.tuesday00srp").val().length !==0 ? $("#Repertoire.tuesday00srp").val() : "";
Repertoire.wednesday00srp = $("#Repertoire.wednesday00srp").val() && $("#Repertoire.wednesday00srp").val().length !==0 ? $("#Repertoire.wednesday00srp").val() : "";
Repertoire.thursday00srp = $("#Repertoire.thursday00srp").val() && $("#Repertoire.thursday00srp").val().length !==0 ? $("#Repertoire.thursday00srp").val() : "";
Repertoire.friday00srp = $("#Repertoire.friday00srp").val() && $("#Repertoire.friday00srp").val().length !==0 ? $("#Repertoire.friday00srp").val() : "";
Repertoire.saturday00srp = $("#Repertoire.saturday00srp").val() && $("#Repertoire.saturday00srp").val().length !==0 ? $("#Repertoire.saturday00srp").val() : "";
Repertoire.sunday00srp = $("#Repertoire.sunday00srp").val() && $("#Repertoire.sunday00srp").val().length !==0 ? $("#Repertoire.sunday00srp").val() : "";
WorkingHour.mon_from = $("#mon_from").val() && $("#mon_from").val().length !==0 ? $("#mon_from").val() : "";
WorkingHour.mon_to = $("#mon_to").val() && $("#mon_to").val().length !==0 ? $("#mon_to").val() : "";
WorkingHour.tue_from = $("#tue_from").val() && $("#tue_from").val().length !==0 ? $("#tue_from").val() : "";
WorkingHour.tue_to = $("#tue_to").val() && $("#tue_to").val().length !==0 ? $("#tue_to").val() : "";
WorkingHour.wed_from = $("#wed_from").val() && $("#wed_from").val().length !==0 ? $("#wed_from").val() : "";
WorkingHour.wed_to = $("#wed_to").val() && $("#wed_to").val().length !==0 ? $("#wed_to").val() : "";
WorkingHour.thu_from = $("#thu_from").val() && $("#thu_from").val().length !==0 ? $("#thu_from").val() : "";
WorkingHour.thu_to = $("#thu_to").val() && $("#thu_to").val().length !==0 ? $("#thu_to").val() : "";
WorkingHour.fri_from = $("#fri_from").val() && $("#fri_from").val().length !==0 ? $("#fri_from").val() : "";
WorkingHour.fri_to = $("#fri_to").val() && $("#fri_to").val().length !==0 ? $("#fri_to").val() : "";
WorkingHour.sat_from = $("#sat_from").val() && $("#sat_from").val().length !==0 ? $("#sat_from").val() : "";
WorkingHour.sat_to = $("#sat_to").val() && $("#sat_to").val().length !==0 ? $("#sat_to").val() : "";
WorkingHour.sun_from = $("#sun_from").val() && $("#sun_from").val().length !==0 ? $("#sun_from").val() : "";
WorkingHour.sun_to = $("#sun_to").val() && $("#sun_to").val().length !==0 ? $("#sun_to").val() : "";
Repertoire.length !== 0 ? modelData.Repertoire = Repertoire : null;
WorkingHour.length !== 0 ? modelData.WorkingHour = WorkingHour : null;
return modelData;
},
render: function () {
var template = _.template($('#place-edit-template').html());
var html = template({place: this.model});
var idRepertoire;
i+1;
this.$el.html(html);
window.scrollTo(0, 0);
//this is
if (newPlace.has('longitude') && newPlace.has('latitude')) {
this.initMap();
}
var placeId=newPlace.get('id');
if(placeId==undefined){
if(this.model.data != undefined){
idRepertoire=this.model.data.idRep;
if(idRepertoire==undefined){
idRepertoire=0;
this.model.Repertoire.id=undefined;
}
var radi=this.model.data.radi;
if(radi==1){
this.model.Repertoire.id=undefined;
}
}
else{
idRepertoire=0;
}
// if (this.model.promotions00srp || this.model.promotions00eng) {
if(idRepertoire == 0 && this.model.Repertoire.id == undefined){
$('.repertoire').addClass('hide');
$('#epp').removeAttr('class');
$('#epp').addClass('btn btn-primary bg-red light toggle-choice');
} else {
$('#epr').removeAttr('class');
$('#epr').addClass('btn btn-primary bg-red light toggle-choice');
$('.promotions').addClass('hide');
}
}
else{
if(this.model.data != undefined){
idRepertoire=this.model.data.idRep;
if(idRepertoire==undefined){
idRepertoire=0;
this.model.Repertoire.id=undefined;
}
var radi=this.model.data.radi;
if(radi==1){
this.model.Repertoire.id=undefined;
}
}
else{
idRepertoire=0;
}
// this.model.promotions00srp == "" || this.model.promotions00eng == "" ||
if(idRepertoire == 0 && this.model.Repertoire.id == undefined){
$('.repertoire').addClass('hide');
$('.promotions').removeClass('hide');
$('#epp').removeAttr('class');
$('#epp').addClass('btn btn-primary bg-red light toggle-choice');
}
else{
$('.promotions').addClass('hide');
$('#epr').removeAttr('class');
$('#epr').addClass('btn btn-primary bg-red light toggle-choice');
$('#epp').removeAttr('class');
$('#epp').addClass('btn btn-outline border-light-gray ml1 light toggle-choice');
}
}
// validating hours in working hour section
$('.hours-input').find('input').each(function(){
$(this).change(function(){
var hour = $(this).val().split(':');
if(hour[0]>=24){
$(this).val('24:00');
} /*else if(hour[1]>59){
$(this).val('00:00')
} */
})
})
return this;
},
selectFile:function(e){
$('#profile-img').click();
$('#ajax-loading').removeClass('hide');
//this.loadingCheck();
},
loadingCheck: function(e){
$('#ajax-loading').addClass('hide');
/*document.body.onfocus = function(){
console.log('THIS IS PROBLEM:',$('#profile-img').val());
if($('#profile-img').val().length == 0){
}
};*/
},
newCropUpload: function(e){
var self = this;
var files = e.currentTarget.files;
if (files && files.length) {
var file = files[0];
var id = newPlace.get('id') != undefined ? newPlace.get('id') : '';
if (/^image\/\w+$/.test(file.type)) {
blobURL = URL.createObjectURL(file);
new NewCropModalView({
id:id,
model:'Place',
type: 'ProfileImage',
canvasHeight: 512,
canvasWidth: 512,
path:blobURL,
formData:self.temporaryData()
});
} else {
alertify.error('Please choose an image file.');
}
}
},
newUploadFiles: function (e) {
// ukolni div koji drzi file input koji koristi ssuploader
$('input[type=file][name=file]').parent().remove();
var $this = this;
var btn = $(e.currentTarget);
var progressOuter = '';
var progressBar = '';
var msgBox = $('#msgBox');
var multiple = false;
var id = newPlace.get('id');
console.log('ID:',id);
console.log(btn.data('type'));
switch (btn.data('type')){
case "background":
var url = '/api/places_v1/newUploadImage/Place';
var newData = {
id: '',
type: 'BackgroundImage',
width: 1400,
height: 326,
multiple: 0
};
if(typeof id != 'undefined'){
newData.id = id;
}
progressOuter = btn.next('.progress');
progressBar = progressOuter.find('progress');
break;
case "official":
var url = '/api/places_v1/newUploadImage/Place';
var newData = {
type: 'Pictures',
width: 151,
height: 151,
multiple: 1
};
if(typeof id != 'undefined'){
newData.id = id;
}
progressOuter = btn.next('.progress');
progressBar = progressOuter.find('progress');
multiple = true;
break;
}
new ss.SimpleUpload({
multiple: multiple,
button: btn,
url: url,
name: 'file',
hoverClass: 'hover',
focusClass: 'focus',
responseType: 'json',
data: newData,
maxUploads: 1,
onProgress: function (pct) {
progressBar.val(pct);
progressBar.attr('value', pct);
},
onSubmit: function () {
$('#ajax-loading').removeClass('hide');
msgBox.html('');
},
startXHR: function (filename, fileSize, uploadBtn) {
if(newPlace.get('id')){
this._opts.data.id = newPlace.get('id');
}
progressOuter.attr('style', 'display:block; position:absolute; width:190px;');
this.setProgressBar(progressBar);
},
onComplete: function (filename, response) {
$('#ajax-loading').addClass('hide');
var obj = {
id: response.data.id
};
console.log('OBJ',obj)
// newPlace.set('id', response.data.id);
switch (btn.data('type')){
case "background":
newPlace.set($.extend($this.temporaryData(), {'bg_thumb': response.data.thumb}));
//newPlace.set('bg_thumb', response.data.thumb);
newPlace.trigger('change');
break;
case "official":
var previousOfficals = newPlace.get('official');
var newOfficial = {
id: response.data.attachment_id,
thumb: response.data.thumb
};
previousOfficals.push(newOfficial);
obj.official = previousOfficals;
var formData = $this.temporaryData();
newPlace.set($.extend(formData,obj));
//newPlace.set('official', previousOfficals);
newPlace.trigger('change');
break;
}
alertify.success('Image successfully uploaded!');
router.navigate(response.data.model.toLowerCase() + "/edit/" + response.data.id, {trigger: false, replace: true});
},
onError: function () {
progressBar.val(0);
progressBar.attr('value', 0);
progressOuter.attr('style', 'display:none');
msgBox.html('Unable to upload file');
}
});
// trigeruj upload za kreirani ssuploader, stavi fokus i hover klasu da bi fukcionisao
btn.addClass('focus hover');
$('input[type=file][name=file]').trigger('click');
},
uploadFiles: function (e) {
// ukolni div koji drzi file input koji koristi ssuploader
$('input[type=file][name=file]').parent().remove();
var $this = this;
var btn = $(e.currentTarget);
var progressOuter = '';
var progressBar = '';
var msgBox = $('#msgBox');
var multiple = false;
switch (btn.data('type')){
case "profile":
var url = '/api/places_v1/uploadImage/Place/';
progressOuter = btn.parent().find('.progress-container').find('.progress');
progressBar = progressOuter.find('progress');
break;
case "background":
var url = '/api/places_v1/uploadImage/Place/1400/326';
progressOuter = btn.next('.progress');
progressBar = progressOuter.find('progress');
break;
case "official":
var url = '/api/places_v1/uploadImage/Place/151/151';
progressOuter = btn.next('.progress');
progressBar = progressOuter.find('progress');
multiple = true;
break;
}
var uploader = new ss.SimpleUpload({
multiple: multiple,
button: btn,
url: url,
name: 'file',
hoverClass: 'hover',
focusClass: 'focus',
responseType: 'json',
onProgress: function (pct) {
progressBar.val(pct);
progressBar.attr('value', pct);
},
onSubmit: function () {
msgBox.html('');
},
startXHR: function () {
alert('start');
progressOuter.attr('style', 'display:block; position:absolute; width:190px;');
this.setProgressBar(progressBar);
},
onComplete: function (filename, response) {
var placeId = newPlace.get('id');
switch (btn.data('type')){
case "profile":
$.ajax({
url: '/api/places_v1/deleteOldImage/Place/'+placeId,
type: 'GET',
})
.success(function() {
console.log("success");
});
new CropModalView({info:response.data,id:placeId,model:'Place'});
$('.save-crop').on('click', function(event) {
event.preventDefault();
return;
setTimeout(function(){
var options = {
btn: btn,
progressBar: progressBar,
progressOuter: progressOuter,
msgBox: msgBox,
placeId: placeId,
file:response.data.tmpData
};
console.log(place_id);
if (placeId == undefined) {
console.log('COMPLETE RESPONSE: ', response);
console.log('COMPLETE OPTIONS: ', options);
$this.submitForm($this.uploadCallback, response, options, 1);
} else {
$this.uploadCallback(response, options);
}
}, 1000);
});
break;
case "background":
case "official":
var options = {
btn: btn,
progressBar: progressBar,
progressOuter: progressOuter,
msgBox: msgBox,
placeId: placeId,
file:response.data.tmpData,
};
if (placeId == undefined) {
$this.submitForm($this.uploadCallback, response, options,1);
} else {
$this.uploadCallback(response, options);
}
break;
}
},
onError: function () {
progressBar.val(0);
progressBar.attr('value', 0);
progressOuter.attr('style', 'display:none');
msgBox.html('Unable to upload file');
}
});
// trigeruj upload za kreirani ssuploader, stavi fokus i hover klasu da bi fukcionisao
btn.addClass('focus hover');
$('input[type=file][name=file]').trigger('click');
},
uploadCallback: function (response, options) {
var picture = new Picture();
var thumb = response.data.thumb;
var locationURL = location.protocol + '//' + location.hostname + '/upload/Place/tmp/';
var tmpImage = (response.data.tmpData != undefined) ? response.data.tmpData : options.file;
var currentTmp = locationURL + tmpImage.name;
var data = {};
switch (options.btn.data('type')) {
case "profile":
data = {'modelId': options.placeId, 'model': 'Place', 'type': 'ProfileImage', 'file': tmpImage};
newPlace.set('thumb', currentTmp+'?t='+new Date().getTime());
setTimeout(function(){
picture.save(data);
}, 1000);
break;
case "background":
data = {'modelId': options.placeId, 'model': 'Place', 'type': 'BackgroundImage', 'file': options.file};
picture.save(data);
newPlace.set('bg_thumb', thumb+'?t='+new Date().getTime());
//$('.bg-cover').attr('style', 'background-image:url(' + thumb + ')');
break;
case "official":
data = {'modelId': options.placeId, 'model': 'Place', 'type': 'Pictures', 'file': options.file};
var images = newPlace.get('official');
picture.save(data, {success: function (response) {
item = {};
item['id'] = response.changed.success;
item['thumb'] = thumb;
images.push(item);
var imgHtml = '<div class="col col-4 sm-col-4 md-col-2 px1 center mb2 relative"><img alt="" data-id="' + response.changed.success + '" src="/img/remove-new-image.png" class="pointer relative z4 opacity remove-picture"><a class="absolute left-0 top-0 z1 px1" href="javascript:void(0)"><img alt="" src="' + thumb + '" class=""></a></div>';
$('.official-images').append(imgHtml);
}});
newPlace.set('official', images);
break;
default:
}
options.progressBar.val(0);
options.progressBar.attr('value', 0);
options.progressOuter.attr('style', 'display:none');
if (!response.status) {
options.msgBox.html(response.message);
}
},
removePicture: function (e) {
var id = $(e.currentTarget).data('id');
var official = newPlace.get('official');
var picture = new Picture({id: id});
picture.destroy();
if(official){
for(i=0; official.length > i; i++){
if (official[i].id && official[i].id == id) {
official.splice(i, 1);
}
}
}
newPlace.set('official', official);
newPlace.trigger('change');
},
stringStartsWith: function(string, prefix) {
return string.slice(0, prefix.length) == prefix;
},
submitForm: function (callback, res, options, prom, e) {
// if(!$('.place-submit').hasClass('bg-red')){return false;};
var data = {};
var workingHourData = {};
var repertoire = {};
var $this = this;
// get repertoire or promotion
$('.place-data input,textarea').each(function () {
if($this.stringStartsWith($(this).attr('id'),"Repertoire.")){
var name = $(this).attr('id').replace("Repertoire.", "");
repertoire[name] = $(this).val();
}else{
data[$(this).attr('id')] = $(this).val();
}
});
// get all hours data from input fields
$('.hours-input').find('input').each(function () {
workingHourData[$(this).attr('id')] = $(this).val();
});
var reservation = $('#reservation').val();
if ((reservation == 1) || (reservation == true)) {
data['reservation_active'] = 1;
} else {
data['reservation_active'] = 0;
}
//set model data
newPlace.set($.extend({'Repertoire': repertoire,'WorkingHour': workingHourData}, data));
//get id from model
var oldId = newPlace.get('id');
if ((data['name00srp'].length == 0) && (data['name00eng'].length == 0)) {
alertify.error('Location title can not be empty!');
return 1;
} else {
$('#ajax-loading').removeClass('hide');
newPlace.save(null, {
success: function (response,res) {
console.log('RESPONSE SAVE: ',response);
$('.place-submit').removeClass('btn-primary bg-red');
if(newPlace.hasChanged('is_active')){
console.log(res.message);
}
$('#ajax-loading').addClass('hide');
if (response.changed.error == 0) {
var placeId = response.changed.data.id;
newPlace.set('id', response.changed.data.id);
if (oldId == undefined && options != undefined) {
options.placeId = placeId;
callback(res, options);
}
alertify.success('Place successfully saved!');
router.navigate('place/edit/' + placeId, {trigger: true, replace: true});
}
},
error: function (error) {
console.log(error);
}
});
}
},
initMap: function () {
$('.map-overlay').addClass('hide');
$('#map').removeClass('hide');
var that = this;
var defaultLng = newPlace.has('longitude') ? parseFloat(newPlace.get('longitude')) : 20.4605354;
var defaultLat = newPlace.has('latitude') ? parseFloat(newPlace.get('latitude')) : 44.8128569;
map = new GMaps({
div: '#map',
lat: defaultLat,
lng: defaultLng,
icon: '/marker_default.png',
markers: [{lat: defaultLat,lng: defaultLng}],
// click: function(e) {
// console.log('ooo');
// // that.geocodePosition(e.latLng.lat(),e.latLng.lng(),true);
// },
});
map.addMarker({
lat: defaultLat,
lng: defaultLng,
draggable: false,
icon: '/marker_default.png',
// dragendadd-amenities: function(e) {
// that.geocodePosition(e.latLng.lat(),e.latLng.lng(),true);
// }
});
map.addListener('click',function(e){
map.removeMarkers();
map.addMarker({
lat: e.latLng.lat(),
lng: e.latLng.lng(),
draggable: false,
icon: '/marker_default.png',
});
var rav = $('#reservation').val();
// console.log($('#reservation').val());
that.geocodePosition(e.latLng.lat(),e.latLng.lng(),true);
$('#reservation').val(rav);
alertify.success('Map location successfully changed!');
// console.log($('#reservation').val());
});
},
geocodePosition: function (lat,lng, drag) {
var that= this;
GMaps.geocode({
lat:lat,
lng:lng,
callback: function(results, status) {
if (status == 'OK') {
var location_data = that.getMapData(results[0].address_components);
var city_id="";
var country_id ="";
var pid = newPlace.get('id');
var placeURL = '/api/places_v1/getCityId/'+location_data['city']+'/'+lat+'/'+lng+'/';
if (pid != undefined) {
placeURL = '/api/places_v1/getCityId/'+location_data['city']+'/'+lat+'/'+lng+'/'+pid;
}
$.ajax({
url:placeURL,
success:function(response){
var obj,
city_id=response.city_id,
city = response.city,
country_id = response.country_id,
country = response.country,
place_id = response.id,
tempFormData = {};
obj = {
'id': place_id,
'latitude': lat,
'longitude': lng,
'city': city,
'country': country,
'city_id':city_id,
'country_id': country_id
};
tempFormData = that.temporaryData();
newPlace.set($.extend(tempFormData,obj));
router.navigate("place/edit/" + place_id, {trigger: false, replace: true});
}
});
}
}
});
},
getMapData: function (addrComponents) {
//this.submitForm("","","",1);
var data = [];
for (var i = 0; i < addrComponents.length; i++) {
if (addrComponents[i].types[0] == "country") {
data['code'] = addrComponents[i].short_name;
data['country'] = addrComponents[i].long_name;
}
if (addrComponents[i].types[0] == "route") {
data['address'] = addrComponents[i].long_name;
}
if (addrComponents[i].types[0] == "street_number") {
data['number'] = addrComponents[i].long_name;
}
if (addrComponents[i].types[0] == "locality") {
data['city'] = addrComponents[i].long_name;
}
//sklonjen uslov da bi prikazivao najblize mesto
if(data['city']==undefined){
if (addrComponents[i].types[0] == "administrative_area_level_2") {
data['city'] = addrComponents[i].long_name;
}
}
}
if(data['city'] == 'Grad Beograd' || data['city'] == 'Beograd'){
data['city'] = 'Belgrade';
}
return data;
},
addTags: function () {
var self = this;
var tags = new Tags([], {id: newPlace.get('id')});
tags.fetch({
success: function (collection) {
new TagsModalView({collection: collection.toJSON(), formData:self.temporaryData()});
}
});
},
addCategories: function () {
var self = this;
var categories = new Categories([], {id: newPlace.get('id')});
categories.fetch({
success: function (collection) {
new CategoriesModalView({collection: collection.toJSON(), formData:self.temporaryData()});
}
});
},
addGroup: function () {
var self = this;
var groups = new PlaceGroups();
groups.url = '/api/places_v1/getGroupsList/';
groups.fetch({
success: function (collection) {
new GroupModalView({collection: collection.toJSON(), formData:self.temporaryData()});
}
});
},
switchLanguage: function (e) {
var tab = $(e.currentTarget);
$('.language-tabs div').attr('class', 'btn btn-outline ml1 light border-light-gray');
tab.attr('class', 'btn btn-primary bg-red white light');
$('[data-lang]').addClass('hide');
$('[data-lang="' + tab.data('lang-id') + '"]').removeClass('hide');
},
togglePublishPlace: function(){
$('.publish_message').empty();
var act = newPlace.get('is_active');
if (newPlace.get('id')==null) {
var message = 'Save place details first.';
alertifyPopUp('<em> Warnning! </em> ', message, 'Ok'); // alertifyPopUp function is in main.js
} else {
if (!newPlace.get('is_active')) {
var msg =[];
//checkout if place image exist in database
$.ajax({
async: false,
url: '/api/pictures_v1/hasPicture/'+newPlace.get('id'),
})
.success(function(response) {
if (response == 0) {
msg.push('Profile picture must not be empty.<br />');
}
})
.error(function(response) {
console.log(response);
});
if ($('#name00eng').val().length === 0) {
msg.push('English place name must not be empty.</br>');
}
if ($('#description00eng').val().length === 0) {
msg.push('English description must not be empty.</br>');
}
if ($('#address').val().length === 0) {
msg.push('Address must not be empty.<br />');
}
if ((newPlace.attributes.latitude == null) && (newPlace.attributes.longitude == null)) {
msg.push('Location must not be empty.</br>');
}
if (newPlace.attributes.categoriesList.length <=0) {
msg.push('Categories must not be empty.</br>');
}
var working_hour_exists = 0;
$.each(newPlace.attributes.WorkingHour, function(index, val) {
if(index != 'id'){
if(val.length != 0){
working_hour_exists++;
return false;
}
}
});
if (working_hour_exists === 0) {
//message += 'Working hours must not be empty.</br>';
msg.push('Working hours must not be empty.</br>');
}
if(msg.length>0){
$('.publish_message').html('');
var message = '';
$.each(msg, function(i, item){
message += item;
});
alertifyPopUp('<em> Warning! </em> ', message, 'Ok');// alertifyPopUp function is in main.js
} else {
newPlace.set('is_active',!newPlace.get('is_active'));
this.submitForm();
newPlace.save();
var button = $('.btn.toggle-publish-place');
button.html(button.attr('published-'+newPlace.get('is_active').toString()));
var row = button.prev();
row.html(row.attr('published-'+newPlace.get('is_active').toString()));
$('.publish-message').empty();
$('.publish-message').html('Unpublish this Location');
}
} else {
newPlace.set('is_active',!newPlace.get('is_active'));
this.submitForm();
newPlace.save();
var button = $('.btn.toggle-publish-place');
button.html(button.attr('published-'+newPlace.get('is_active').toString()));
var row = button.prev();
row.html(row.attr('published-'+newPlace.get('is_active').toString()));
$('.publish-message').empty();
$('.publish-message').html('Publish this Location');
}
}
},
toggleChoice: function(e) {
var currentChoice = $(e.currentTarget);
var toggleChoice = $('.choices .bg-red');
if (confirm("You can add only one option promotions or repertoire.") == true) {
currentChoice.attr('class','btn btn-primary bg-red light toggle-choice');
toggleChoice.attr('class','btn btn-outline border-light-gray ml1 light toggle-choice');
$('.choice').not('.hide').find('textarea').each(function () {
$(this).val('');
});
$('.choice').not('.hide').addClass('hide');
$('.choice.'+currentChoice.attr('rel')).removeClass('hide');
}
},
changeSaveButton: function () {
$('.place-submit').addClass('btn btn-primary bg-red light');
},
changeSaveButton1: function () {
var stat = $('#reservation_active').attr("value");
if (stat==1) {
$('#reservation').attr("value",0);
$('#reservation_active').prop('checked', false);
$('#reservation_active').attr("value",0)
} else {
$('#reservation').attr("value",1);
$('#reservation_active').prop('checked', true);
$('#reservation_active').attr("value",1)
}
$('.place-submit').addClass('btn btn-primary bg-red light');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment