File: /home/imensosw/www/mpl.imenso.co/public/js/arcampaigns.js
jQuery(function ($) {
var getRatingInterval;
function setLinkedProfile(name, id) {
if ($(('.typeahead input.hidden_id')).val() != '') {
$('.typeahead i.fa-close').click();
}
var typeahead = $(".typeahead");
var hidden_input = typeahead.find('input.hidden_id');
var flyout = typeahead.find('.flyout');
var input = typeahead.find('input[type="text"]');
var ids = [id];
hidden_input.val(JSON.stringify(ids));
input.addClass('hidden');
input.val('');
flyout.html('');
flyout.removeClass('active');
typeahead.find('.selected').append('<div class="selected-capsule" data-id="' + id + '"><span class="name">' + name + '</span><i class="fa fa-close" onclick="removeFromTypeahead(this)"></i></div>');
}
function getRating(showAlert) {
$.ajax({
url: getRatingRoute,
method: 'POST',
data: {
'target_id': $('#target_id').val(),
'marker_id': $('#marker_id').val(),
'_token': csrf_token,
},
dataType: 'JSON',
beforeSend: function () {
$('.marker-rating__pending.icon i').addClass('fa-spin');
},
success: function (data) {
// console.log(data);
$('.marker-rating__pending.icon i').removeClass('fa-spin');
if (data.status === 'pending') {
if (showAlert) {
alert(data.message);
}
} else if (data.status === 'duplicate') {
alert(data.message);
$('.marker-rating__pending').hide();
clearInterval(getRatingInterval);
$('.create-arcampaign-submit-button').prop('disabled', false).removeClass('get-marker-rating');
$('.uploaded-marker-preview').css('background-image', 'none');
} else {
$('.marker-rating__pending').hide();
$('.marker-rating > div .text span').text(data.rating.target_record.tracking_rating);
if (data.rating.target_record.tracking_rating > 2) {
$('.marker-rating__accepted').css('display', 'inline-block');
} else {
$('.marker-rating__rejected').css('display', 'inline-block');
}
clearInterval(getRatingInterval);
$('.create-arcampaign-submit-button').prop('disabled', false).removeClass('get-marker-rating');
}
},
error: function (data) {
alert(data.responseJSON.message);
$('.marker-rating__pending.icon i').removeClass('fa-spin');
}
});
}
if (linkedProfile) {
// console.log("ID: ", linkedProfile);
setLinkedProfile(linkedProfile.name, linkedProfile.id);
}
$('.set-as-my-profile').click(function () {
setLinkedProfile(myProfile.name, myProfile.id);
});
$('#vimeo_upload').change(function () {
var fileName = this.files[0].name;
$('span.video-title').html(fileName).show();
$('.video-clear').show();
$('#youtube_url').prop('disabled', true);
$('.ar-campaigns__video-wrapper .upload-video').show();
});
$('#upload_marker').change(function () {
var allowedExtensions = new RegExp(/(\.pdf|\.jpg|\.jpeg)$/i);
var fileInput = this;
var ext = fileInput.value.toLowerCase();
if (ext) {
var allowed = allowedExtensions.exec(ext);
// console.log("A: ", allowed);
if (!allowed) {
alert('Please upload a file with extension .jpg, .jpeg or .pdf.');
return false;
}
if (this.files[0].size > 2000000) {
alert('File size too large. Please upload a smaller image.\r' + 'File Size: ' + Math.round(this.files[0].size / 1000000) + "mb\r" + "Allowed Size: 2mb");
return false;
}
if(
$('input[name="name"]').val() == '' ||
$('input[name="profile_id"]').val() == '' ||
$('input[name="sales_url"]').val() == '' ||
$('input[name="button_text"]').val() == ''
) {
alert('Please fill out the form above before adding a custom marker. The Campaign Name, Profile Link, Campaign Sales URL and Campaign Button Text are are all required.');
return false;
}
var formData = new FormData();
var targetId = $('#target_id');
formData.append('marker_image', fileInput.files[0]);
formData.append('file_name', markerId);
if (targetId.val().length) {
// console.log('target_id', targetId.val());
formData.append('target_id', targetId.val());
}
$.ajax({
url: uploadMarkerRoute,
method: 'POST',
data: formData,
dataType: 'JSON',
processData: false,
contentType: false,
beforeSend: function () {
$(fileInput).parent().addClass('loading');
$('.create-arcampaign-submit-button').prop('disabled', true);
},
success: function (data) {
// console.log(data);
if (data.target.result_code === "TargetCreated") {
targetId.val(data.target.target_id);
var time = new Date().getTime();
$('.uploaded-marker-preview').css('background-image', 'url("' +
customMarkerPath + data.ext + '?v=' + time +
'")'
);
$('.marker-rating > div').css('display', 'none');
if (data.success == 'pending') {
$('.marker-rating__pending').css('display', 'inline-block');
$('.create-arcampaign-submit-button').prop('disabled', true).addClass('get-marker-rating');
getRatingInterval = setInterval(function () {
getRating(false);
}, 30000);
} else {
$('.marker-rating > div .text span').text(data.rating.target_record.tracking_rating);
if (data.rating.target_record.tracking_rating > 2) {
$('.marker-rating__accepted').css('display', 'inline-block');
} else {
$('.marker-rating__rejected').css('display', 'inline-block');
}
$('.create-arcampaign-submit-button').prop('disabled', false).removeClass('get-marker-rating');
}
} else {
alert('There was an error. Please try again.');
}
$(fileInput).parent().removeClass('loading');
$(fileInput).val('');
},
error: function (data) {
try {
// console.log(data);
$(fileInput).parent().removeClass('loading');
$(fileInput).val('');
// alert(data.responseJSON.message);
} catch (err) {
alert(err.message);
}
}
});
}
});
$('.get-marker-rating').click(function (e) {
e.preventDefault();
getRating(true);
});
$('.video-clear').click(function (e) {
e.preventDefault();
$('.video-title').html('');
$('#vimeo_upload').val('');
$('#youtube_url').prop('disabled', false);
$(this).hide();
});
$('.remove-video').click(function (e) {
e.preventDefault();
$('.ar-campaigns__video-wrapper iframe').remove();
$('#vimeo_upload, #video').val('');
$('#youtube_url').prop('disabled', false);
$('.upload-video').show();
$('.upload-video-link').removeClass('hidden');
$(this).addClass('hidden');
});
$('#youtube_url').on('keyup', function () {
var chooseVideoButton = $('.choose-video-button');
if ($(this).val() !== '') {
chooseVideoButton.addClass('disabled');
chooseVideoButton.prop('disabled', true);
chooseVideoButton.attr('disabled', 'disabled');
} else {
chooseVideoButton.removeClass('disabled');
chooseVideoButton.prop('disabled', false);
chooseVideoButton.attr('disabled', '');
}
});
$('.choose-video-button').click(function () {
return !$(this).hasClass('disabled');
});
$('#marker-color-select').change(function () {
var color = this.value;
var markerImg = $('.marker-wrapper img');
var dlMarkerBtns = $('.download-marker-buttons a');
var originalSrc = markerImg.attr('src');
var newSrc;
if (this.value === "W") {
$.each(dlMarkerBtns, function () {
this.href = this.href.replace("B", color);
});
newSrc = originalSrc.replace("B", color);
} else {
$.each(dlMarkerBtns, function () {
this.href = this.href.replace("W", color);
});
newSrc = originalSrc.replace("W", color);
}
// console.log(color, newSrc);
markerImg.attr('src', newSrc);
});
$('.ar-campaigns__video-wrapper input').change(function () {
$('.ar-campaigns__video-wrapper iframe').hide();
$('input#youtube_url').val('');
});
});