| 1 |
(function ($) { |
| 2 |
"use strict"; |
| 3 |
$.borderlessTrigger = $.borderlessTrigger || {}; |
| 4 |
$.borderlessTrigger.wp_media = $.borderlessTrigger.wp_media || []; |
| 5 |
$.borderlessTrigger.media_new = function () { |
| 6 |
var $body = $("body"); |
| 7 |
$body.on('click', '.borderless-upload-icon', $.borderlessTrigger.media_new_activate); |
| 8 |
}; |
| 9 |
//intended for zip files only. if needed should be easy to expand in the future |
| 10 |
$.borderlessTrigger.media_new_activate = function (event) { |
| 11 |
event.preventDefault(); |
| 12 |
var clicked = $(this), options = clicked.data(); |
| 13 |
options.input_target = $('#' + options.target); |
| 14 |
// Create the media frame. |
| 15 |
var file_frame = wp.media( |
| 16 |
{ |
| 17 |
frame: options.frame, |
| 18 |
library: {type: options.type}, |
| 19 |
button: {text: options.button}, |
| 20 |
className: options['class'] |
| 21 |
}); |
| 22 |
file_frame.on('select update insert', function () { |
| 23 |
$.borderlessTrigger.media_new_insert(file_frame, options); |
| 24 |
}); |
| 25 |
//open the media frame |
| 26 |
file_frame.open(); |
| 27 |
}; |
| 28 |
//insert the url of the zip file |
| 29 |
$.borderlessTrigger.media_new_insert = function (file_frame, options) { |
| 30 |
var state = file_frame.state(), selection = state.get('selection').first().toJSON(); |
| 31 |
options.input_target.val(selection.id).trigger('change'); |
| 32 |
$("body").trigger(options.trigger, [selection, options]); |
| 33 |
}; |
| 34 |
$(document).ready(function () { |
| 35 |
$.borderlessTrigger.media_new(); |
| 36 |
//Fonts Zip file upload |
| 37 |
$("body").on('borderless_insert_zip', $.borderlessTrigger.icon_insert); |
| 38 |
//font manager |
| 39 |
$("body").on('click', '.borderless_del_icon', $.borderlessTrigger.icon_remove); |
| 40 |
}); |
| 41 |
/************************************************************************ |
| 42 |
EXTRA FUNCTIONS, NOT NECESSARY FOR THE DEFAULT UPLOAD |
| 43 |
*************************************************************************/ |
| 44 |
$.borderlessTrigger.icon_insert = function (event, selection, options) { |
| 45 |
// clean the options field, we dont need to save a value |
| 46 |
options.input_target.val(""); |
| 47 |
var manager = $('.borderless_iconfont_manager'); |
| 48 |
var msg = $('#msg'); |
| 49 |
if (selection.subtype !== 'zip') { |
| 50 |
$('.spinner').hide(); |
| 51 |
msg.html("<div class='error'><p>Please upload a valid ZIP file.<br/>You can create the file on icomoon.io</p></div>"); |
| 52 |
msg.show(); |
| 53 |
setTimeout(function () { |
| 54 |
msg.slideUp(); |
| 55 |
}, 5000); |
| 56 |
return; |
| 57 |
} |
| 58 |
// send request to server to extract the zip file, re arrange the content and save a config file |
| 59 |
$.ajax({ |
| 60 |
type: "POST", |
| 61 |
url: ajaxurl, |
| 62 |
data: { |
| 63 |
action: 'borderless_ajax_add_zipped_font', |
| 64 |
values: selection, |
| 65 |
}, |
| 66 |
beforeSend: function () { |
| 67 |
$('.spinner').css({ |
| 68 |
opacity: 0, |
| 69 |
display: "block", |
| 70 |
visibility: 'visible', |
| 71 |
position: 'absolute', |
| 72 |
top: '21px', |
| 73 |
left: '345px' |
| 74 |
}).animate({opacity: 1}); |
| 75 |
}, |
| 76 |
/*error: function() |
| 77 |
{ |
| 78 |
alert('Couldn\'t add the font because the server didn’t respond.<br/>Please reload the page, then try again'); |
| 79 |
},*/ |
| 80 |
success: function (response) { |
| 81 |
$('.spinner').hide(); |
| 82 |
if (response.match(/borderless_font_added/)) { |
| 83 |
msg.html("<div class='updated'><p>Icon Pack added successfully. Reloading the page.</p></div>"); |
| 84 |
msg.show(); |
| 85 |
setTimeout(function () { |
| 86 |
msg.slideUp(); |
| 87 |
location.reload(); |
| 88 |
}, 5000); |
| 89 |
} |
| 90 |
else { |
| 91 |
msg.html("<div class='error'><p>Couldn't add the Icon Pack.<br/>The script returned the following error: " + response + "</p></div>"); |
| 92 |
msg.show(); |
| 93 |
setTimeout(function () { |
| 94 |
msg.slideUp(); |
| 95 |
}, 5000); |
| 96 |
} |
| 97 |
if (typeof console !== 'undefined') console.log(response); |
| 98 |
//msg.fadeOut('slow'); |
| 99 |
} |
| 100 |
}); |
| 101 |
}; |
| 102 |
$.borderlessTrigger.icon_remove = function (event) { |
| 103 |
event.preventDefault(); |
| 104 |
var button = $(this), |
| 105 |
parent = button.parents('.borderless-available-font:eq(0)'), |
| 106 |
manager = button.parents('.borderless_iconfont_manager:eq(0)'), |
| 107 |
all_fonts = manager.find('.borderless-available-font'), |
| 108 |
del_font = button.data('delete'); |
| 109 |
//alert(del_font); |
| 110 |
var msg = $('#msg'); |
| 111 |
// send request to server to remove the folder and the database entry |
| 112 |
$.ajax({ |
| 113 |
type: "POST", |
| 114 |
url: ajaxurl, |
| 115 |
data: { |
| 116 |
action: 'borderless_ajax_remove_zipped_font', |
| 117 |
del_font: del_font, |
| 118 |
}, |
| 119 |
beforeSend: function () { |
| 120 |
$('.spinner').css({ |
| 121 |
opacity: 0, |
| 122 |
display: "block", |
| 123 |
visibility: 'visible', |
| 124 |
position: 'absolute', |
| 125 |
top: '21px', |
| 126 |
left: '345px' |
| 127 |
}).animate({opacity: 1}); |
| 128 |
}, |
| 129 |
error: function () { |
| 130 |
$('.spinner').hide(); |
| 131 |
msg.html("<div class='error'><p>Couldn't remove the font because the server didn’t respond.<br/>Please reload the page, then try again</p></div>"); |
| 132 |
msg.show(); |
| 133 |
setTimeout(function () { |
| 134 |
msg.slideUp(); |
| 135 |
}, 5000); |
| 136 |
}, |
| 137 |
success: function (response) { |
| 138 |
$('.spinner').hide(); |
| 139 |
if (response.match(/borderless_font_removed/)) { |
| 140 |
msg.html("<div class='updated'><p>Icon Pack deleted successfully! Reloading the page...</p></div>"); |
| 141 |
msg.show(); |
| 142 |
setTimeout(function () { |
| 143 |
msg.slideUp(); |
| 144 |
location.reload(); |
| 145 |
}, 5000); |
| 146 |
} |
| 147 |
else { |
| 148 |
msg.html("<p><div class='error'><p>Couldn't remove the font.<br/>Reloading the page...</p></div>"); |
| 149 |
msg.show(); |
| 150 |
setTimeout(function () { |
| 151 |
msg.slideUp(); |
| 152 |
location.reload(); |
| 153 |
}, 5000); |
| 154 |
} |
| 155 |
if (typeof console !== 'undefined') console.log(response); |
| 156 |
//msg.fadeOut('slow'); |
| 157 |
}, |
| 158 |
complete: function (response) { |
| 159 |
//alert(response); |
| 160 |
} |
| 161 |
}); |
| 162 |
}; |
| 163 |
})(jQuery); |