$(document).foundation();
$(document).ready(function(){
$(".insert-file").on('click', function(){
var media_element = $(this);
var file = media_element.closest('tr').data('media-file');
var modal = $('#mediaModal');
var field_id = modal.data('field_id');
if(modal.hasClass('tinymce')){
var args = top.tinymce.activeEditor.windowManager.getParams();
win = (args.window);
input = (args.input);
win.document.getElementById(input).value = '/uploads/media/' + file;
top.tinymce.activeEditor.windowManager.close();
} else {
if(media_element.data('type') == 'image') {
if(typeof field_id != 'undefined'){
parent.$("#"+field_id).val(file);
parent.$("#"+field_id+"Preview").attr('src', '/uploads/media/' + file);
} else {
parent.$("#featuredImage").val(file);
parent.$("#imagePreview").attr('src', '/uploads/media/' + file);
}
parent.$(".notice").addClass('alert-box warning').data('alert', '').html('The new image will only be saved when clicking the "Save" button below.
If you want to keep the old image, you can click the "Cancel" button below or click here to refresh.×');
}
if(media_element.data('type') == 'document') {
if(media_element.data('format')) {
parent.$("#" + media_element.data('format')).val(file);
} else {
parent.$("#attachedDocument").val(file);
}
}
}
parent.$('.fancybox-close').trigger('click');
});
$(".insert-link").on('click', function(event){
event.preventDefault();
var element = $(this);
var link = element.attr('href');
var args = top.tinymce.activeEditor.windowManager.getParams();
win = (args.window);
input = (args.input);
win.document.getElementById(input).value = link;
top.tinymce.activeEditor.windowManager.close();
});
$("body").on('click', '.additional-recipe-image', function(){
var target = $(this);
var image = target.closest('tr').data('media-file');
var recipe_id = target.data('resource_id');
var jqXHR = $.ajax({
method: 'POST',
url: '/admin/recipes/'+recipe_id+'/add-image',
data: { recipe_id: recipe_id, image: image },
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
jqXHR.done(function(results){
parent.$('#recipeImages').html(results);
parent.$('.fancybox-close').trigger('click');
});
});
$("body").on('click', '.main-resource-image', function() {
var target = $(this);
var resource_id = target.data('resource_id');
var resource = target.data('resource');
var main_image = target.data('main_image');
parent.$("#featuredImage").val(main_image);
parent.$('.notice').addClass('alert-box warning').data('alert', '').html('The new image will only be saved when clicking the "Save" button below.
If you want to keep the old image, you can click the "Cancel" button below or click here to refresh.×');
parent.$('.fancybox-close').trigger('click');
});
$("body").on('click', '.additional-resource-image', function() {
var target = $(this);
var route;
var resource = target.data('resource');
var media_id = target.closest('tr').data('media-id');
var resource_id = parent.$("#resourceImages").data('resource-id');
var jqXHR = $.ajax({
method: 'POST',
url: '/admin/'+resource+'/'+resource_id+'/add-image',
data: { media_id: media_id, resource: resource },
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
jqXHR.done(function(results){
console.log(results);
parent.$('#resourceImages').html(results);
parent.$('.fancybox-close').trigger('click');
});
});
$('#pageLinkSearch').on('keyup', function(){
var target = $(this);
var jqXHR = $.ajax({
method: 'POST',
url: '/admin/link_modal',
data: { s: target.val() },
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
jqXHR.done(function(results){
$('#linkList').html(results);
});
});
});
Dropzone.options.mediaDropZone = {
acceptedFiles: '.jpg, .jpeg, .png, .gif, .pdf, .doc, .docx, .xls, .xlsx, .eps, .ai, .mp4',
init: function () {
this.on("complete", function (file) {
if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
//window.location.href = "media_items";
location.reload();
}
});
}
};