| 1 |
// JavaScript |
| 2 |
jQuery(function ($) { |
| 3 |
setTimeout(removeLoaderBoxImg, 1000); //wait for page load PLUS 1 second. |
| 4 |
$(window).on('load', function () { |
| 5 |
removeLoaderBoxImg(); |
| 6 |
}); |
| 7 |
function removeLoaderBoxImg(){ |
| 8 |
$('#item-main-pict #wel-item-image-loading').hide(); |
| 9 |
$('#item-main-pict #uscestabs_item_images').show(); |
| 10 |
}; |
| 11 |
$("#uscestabs_item_images").tabs({ |
| 12 |
active: 0, |
| 13 |
}); |
| 14 |
// load popup show message. |
| 15 |
$("#wel_item_image_dialog_wrap").dialog({ |
| 16 |
autoOpen: false, |
| 17 |
height: 200, |
| 18 |
width: 500, |
| 19 |
modal: true, |
| 20 |
buttons: [{ |
| 21 |
text: usces_item_images_js_setting.text_close, |
| 22 |
click: function () { |
| 23 |
$(this).dialog('close'); |
| 24 |
} |
| 25 |
}], |
| 26 |
close: function () { |
| 27 |
$("#wel_item_image_dialog_content").html(''); |
| 28 |
} |
| 29 |
}); |
| 30 |
$('#uscestabs_item_images input#wel_item_img_media_manager').click(function (e) { |
| 31 |
e.preventDefault(); |
| 32 |
var image_frame; |
| 33 |
if (image_frame) { |
| 34 |
image_frame.open(); |
| 35 |
} |
| 36 |
// Define image_frame as wp.media object |
| 37 |
image_frame = wp.media({ |
| 38 |
title: usces_item_images_js_setting.title_image_frame, |
| 39 |
multiple: true, |
| 40 |
library: { |
| 41 |
type: 'image' |
| 42 |
} |
| 43 |
}); |
| 44 |
|
| 45 |
image_frame.on('select', function () { |
| 46 |
// On select, get selections and save to the hidden input |
| 47 |
// plus other AJAX stuff to refresh the image preview |
| 48 |
var selection = image_frame.state().get('selection'); |
| 49 |
var gallery_ids = new Array(); |
| 50 |
var my_index = 0; |
| 51 |
selection.each(function (attachment) { |
| 52 |
gallery_ids[my_index] = attachment['id']; |
| 53 |
my_index++; |
| 54 |
}); |
| 55 |
var ids = gallery_ids.join(","); |
| 56 |
wel_item_images.choose_images_from_media(ids); |
| 57 |
}); |
| 58 |
image_frame.on('close',function() { |
| 59 |
// handel load image alt, caption in case if have any change on the media. |
| 60 |
var pict_ids = $("#uscestabs_item_images #wel-img-pict-ids").val(); |
| 61 |
if (pict_ids) { |
| 62 |
var ar_pict_ids = pict_ids.split(","); |
| 63 |
if (0 == ar_pict_ids.length) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
var queryargs = {post__in: ar_pict_ids}; |
| 67 |
wp.media.query(queryargs) // set the query |
| 68 |
.more() // execute the query, this will return an deferred object |
| 69 |
.done(function(){ // attach callback, executes after the ajax call succeeded |
| 70 |
var attachments = this.toJSON(); |
| 71 |
if (attachments) { |
| 72 |
$.each(attachments, function( index, value ) { |
| 73 |
var pict_id = value.id; |
| 74 |
var pict_alt = value.alt; |
| 75 |
var pict_caption = value.caption; |
| 76 |
// load info for main pict. |
| 77 |
var main_pict_id = $("#uscestabs_item_img #wel-img-pict-id").val(); |
| 78 |
if (main_pict_id == pict_id) { |
| 79 |
$("#uscestabs_item_img #wel-img-alt").val(pict_alt); |
| 80 |
$("#uscestabs_item_img #wel-img-caption").val(pict_caption); |
| 81 |
} |
| 82 |
// load info for sub pict. |
| 83 |
$("#uscestabs_item_img subpict#wel-pict-"+pict_id).attr('data-alt', pict_alt); |
| 84 |
$("#uscestabs_item_img subpict#wel-pict-"+pict_id).attr('data-caption', pict_caption); |
| 85 |
}); |
| 86 |
} |
| 87 |
}); |
| 88 |
} |
| 89 |
}); |
| 90 |
|
| 91 |
image_frame.open(); |
| 92 |
}); |
| 93 |
|
| 94 |
$("body #uscestabs_item_images").on("click",".wel-view-main-img", function(){ |
| 95 |
var elm_img = $(this); |
| 96 |
wel_item_images.change_item_pict( elm_img ); |
| 97 |
}); |
| 98 |
|
| 99 |
$("#uscestabs_item_file #wrapper_tab_file_item_pict").sortable({ |
| 100 |
update: function () { |
| 101 |
var item_pict_ids = $("#wrapper_tab_file_item_pict").sortable("toArray"); |
| 102 |
wel_item_images.sort_order_item_image(item_pict_ids); |
| 103 |
} |
| 104 |
}); |
| 105 |
$("#uscestabs_item_file #wrapper_tab_file_item_pict").disableSelection(); |
| 106 |
|
| 107 |
wel_item_images = { |
| 108 |
settings: { |
| 109 |
url: uscesL10n.requestFile, |
| 110 |
type: 'POST', |
| 111 |
cache: false |
| 112 |
}, |
| 113 |
choose_images_from_media: function (ids) { |
| 114 |
var post_id = $('#uscestabs_item_images #wel_image_post_id').val(); |
| 115 |
var s = Object.assign({}, wel_item_images.settings); |
| 116 |
s.data = { |
| 117 |
'action': 'wel_item_image_ajax', |
| 118 |
'mode': 'choose_images_from_media', |
| 119 |
'post_id': post_id, |
| 120 |
'str_pict_ids': ids, |
| 121 |
'_ajax_nonce': usces_item_images_js_setting._ajax_nonce |
| 122 |
}; |
| 123 |
|
| 124 |
// add loading. |
| 125 |
$("#item-main-pict #wel-tab-file-loading").show(); |
| 126 |
$.ajax(s).done(function (res) { |
| 127 |
console.log(res); |
| 128 |
// remove effect loading. |
| 129 |
$("#item-main-pict #wel-tab-file-loading").hide(); |
| 130 |
if (res.status) { |
| 131 |
// Load list on the tab file. |
| 132 |
$('#uscestabs_item_images #uscestabs_item_file #wrapper_tab_file_item_pict').html(res.data_tab_file); |
| 133 |
// Load list on the tab image. |
| 134 |
$('#uscestabs_item_images #uscestabs_item_img').html(res.data_tab_image); |
| 135 |
// active tab file. |
| 136 |
$("#uscestabs_item_images #usces_tabs_item_file").click(); |
| 137 |
// add more scoll. |
| 138 |
$("#uscestabs_item_file #wrapper_tab_file_item_pict").animate({ scrollTop: $("#uscestabs_item_file #wrapper_tab_file_item_pict")[0].scrollHeight}, 700); |
| 139 |
} else if ('' != res.msg) { |
| 140 |
$("#wel_item_image_dialog_content").html(res.msg); |
| 141 |
$("#wel_item_image_dialog_wrap").dialog("open"); |
| 142 |
} |
| 143 |
}).fail(function (msg) { |
| 144 |
// remove loading. |
| 145 |
$("#item-main-pict #wel-tab-file-loading").hide(); |
| 146 |
$("#wel_item_image_dialog_content").html(usces_item_images_js_setting.msg_nonce_expried); |
| 147 |
$("#wel_item_image_dialog_wrap").dialog("open"); |
| 148 |
}); |
| 149 |
}, |
| 150 |
sort_order_item_image: function (item_pict_ids) { |
| 151 |
var post_id = $('#uscestabs_item_images #wel_image_post_id').val(); |
| 152 |
if (0 < item_pict_ids.length) { |
| 153 |
var s = Object.assign({}, wel_item_images.settings); |
| 154 |
s.data = { |
| 155 |
'action': 'wel_item_image_ajax', |
| 156 |
'mode': 'sort_order_item_image', |
| 157 |
'post_id': post_id, |
| 158 |
'item_pict_ids': item_pict_ids, |
| 159 |
'_ajax_nonce': usces_item_images_js_setting._ajax_nonce |
| 160 |
}; |
| 161 |
$("#item-main-pict #item-select-pict").addClass('div-item-img-loading'); |
| 162 |
$.ajax(s).done(function (res) { |
| 163 |
$("#item-main-pict #item-select-pict").removeClass('div-item-img-loading'); |
| 164 |
if (res.status) { |
| 165 |
// Load list on the tab file. |
| 166 |
$('#uscestabs_item_images #uscestabs_item_file #wrapper_tab_file_item_pict').html(res.data_tab_file); |
| 167 |
// Load list on the tab image. |
| 168 |
$('#uscestabs_item_images #uscestabs_item_img').html(res.data_tab_image); |
| 169 |
} else if ('' != res.msg) { |
| 170 |
$("#wel_item_image_dialog_content").html(res.msg); |
| 171 |
$("#wel_item_image_dialog_wrap").dialog("open"); |
| 172 |
} |
| 173 |
}).fail(function (msg) { |
| 174 |
$("#item-main-pict #item-select-pict").removeClass('div-item-img-loading'); |
| 175 |
$("#wel_item_image_dialog_content").html(usces_item_images_js_setting.msg_nonce_expried); |
| 176 |
$("#wel_item_image_dialog_wrap").dialog("open"); |
| 177 |
}); |
| 178 |
} |
| 179 |
}, |
| 180 |
exclude_item_image: function () { |
| 181 |
var post_id = $('#uscestabs_item_images #wel_image_post_id').val(); |
| 182 |
var item_pict_ids = []; |
| 183 |
$("body #uscestabs_item_file input[name*='file_item_picts']:checked").each(function () { |
| 184 |
item_pict_ids.push($(this).val()); |
| 185 |
}); |
| 186 |
if (0 == item_pict_ids.length) { |
| 187 |
// show popup message. |
| 188 |
$("#wel_item_image_dialog_content").text(usces_item_images_js_setting.msg_choose_item_image); |
| 189 |
$("#wel_item_image_dialog_wrap").dialog("open"); |
| 190 |
} else { |
| 191 |
var img_loading = '<div id="wrap_img_icon_loading"><img src="' + uscesL10n.USCES_PLUGIN_URL + 'images/loading.gif" /></div>'; |
| 192 |
$("#uscestabs_item_file #wel_file_exclude").prop("disabled", true); |
| 193 |
$(img_loading).insertBefore("#uscestabs_item_file #wel_file_exclude"); |
| 194 |
|
| 195 |
var s = Object.assign({}, wel_item_images.settings); |
| 196 |
s.data = { |
| 197 |
'action': 'wel_item_image_ajax', |
| 198 |
'mode': 'exclude_item_image', |
| 199 |
'post_id': post_id, |
| 200 |
'item_pict_ids': item_pict_ids, |
| 201 |
'_ajax_nonce': usces_item_images_js_setting._ajax_nonce |
| 202 |
}; |
| 203 |
$.ajax(s).done(function (res) { |
| 204 |
$("#wrap_img_icon_loading").remove(); |
| 205 |
$("#uscestabs_item_file #wel_file_exclude").removeAttr('disabled'); |
| 206 |
if (res.status) { |
| 207 |
// Load list on the tab file. |
| 208 |
$('#uscestabs_item_images #uscestabs_item_file #wrapper_tab_file_item_pict').html(res.data_tab_file); |
| 209 |
// Load list on the tab image. |
| 210 |
$('#uscestabs_item_images #uscestabs_item_img').html(res.data_tab_image); |
| 211 |
} else if ('' != res.msg) { |
| 212 |
$("#wel_item_image_dialog_content").html(res.msg); |
| 213 |
$("#wel_item_image_dialog_wrap").dialog("open"); |
| 214 |
} |
| 215 |
}).fail(function (msg) { |
| 216 |
$("#wrap_img_icon_loading").remove(); |
| 217 |
$("#uscestabs_item_file #wel_file_exclude").removeAttr('disabled'); |
| 218 |
$("#wel_item_image_dialog_content").html(usces_item_images_js_setting.msg_nonce_expried); |
| 219 |
$("#wel_item_image_dialog_wrap").dialog("open"); |
| 220 |
}); |
| 221 |
} |
| 222 |
}, |
| 223 |
check_before_delete_item_image: function () { |
| 224 |
var post_id = $('#uscestabs_item_images #wel_image_post_id').val(); |
| 225 |
var pict_ids = []; |
| 226 |
$("body #uscestabs_item_file input[name*='file_item_picts']:checked").each(function () { |
| 227 |
pict_ids.push($(this).val()); |
| 228 |
}); |
| 229 |
if (0 == pict_ids.length) { |
| 230 |
// show popup message. |
| 231 |
$("#wel_item_image_dialog_content").text(usces_item_images_js_setting.msg_choose_item_image); |
| 232 |
$("#wel_item_image_dialog_wrap").dialog("open"); |
| 233 |
} else { |
| 234 |
var img_loading = '<div id="wrap_img_icon_loading"><img src="' + uscesL10n.USCES_PLUGIN_URL + 'images/loading.gif" /></div>'; |
| 235 |
$("#uscestabs_item_file #wel_file_delete").prop("disabled", true); |
| 236 |
$(img_loading).insertAfter("#uscestabs_item_file #wel_file_delete"); |
| 237 |
|
| 238 |
var s = Object.assign({}, wel_item_images.settings); |
| 239 |
s.data = { |
| 240 |
'action': 'wel_item_image_ajax', |
| 241 |
'mode': 'validate_item_image_before_delete', |
| 242 |
'post_id': post_id, |
| 243 |
'item_pict_ids': pict_ids, |
| 244 |
'_ajax_nonce': usces_item_images_js_setting._ajax_nonce |
| 245 |
}; |
| 246 |
$.ajax(s).done(function (res) { |
| 247 |
if (res.status) { |
| 248 |
if (res.check_in_other_product) { |
| 249 |
// show confirm. |
| 250 |
if (confirm(res.msg_show_confirm)) { |
| 251 |
wel_item_images.delete_item_image(res.none_item_delete); |
| 252 |
} else { |
| 253 |
$("#wrap_img_icon_loading").remove(); |
| 254 |
$("#uscestabs_item_file #wel_file_delete").removeAttr('disabled'); |
| 255 |
} |
| 256 |
} else { |
| 257 |
// continue run delete item. |
| 258 |
wel_item_images.delete_item_image(res.none_item_delete); |
| 259 |
} |
| 260 |
} |
| 261 |
}).fail(function (msg) { |
| 262 |
$("#wel_item_image_dialog_content").html(usces_item_images_js_setting.msg_nonce_expried); |
| 263 |
$("#wel_item_image_dialog_wrap").dialog("open"); |
| 264 |
$("#wrap_img_icon_loading").remove(); |
| 265 |
$("#uscestabs_item_file #wel_file_delete").removeAttr('disabled'); |
| 266 |
}); |
| 267 |
} |
| 268 |
}, |
| 269 |
delete_item_image: function (ajax_nonce) { |
| 270 |
var post_id = $('#uscestabs_item_images #wel_image_post_id').val(); |
| 271 |
var pict_ids = []; |
| 272 |
$("body #uscestabs_item_file input[name*='file_item_picts']:checked").each(function () { |
| 273 |
pict_ids.push($(this).val()); |
| 274 |
}); |
| 275 |
if (0 == pict_ids.length) { |
| 276 |
// show popup message. |
| 277 |
$("#wel_item_image_dialog_content").text(usces_item_images_js_setting.msg_choose_item_image); |
| 278 |
$("#wel_item_image_dialog_wrap").dialog("open"); |
| 279 |
} else { |
| 280 |
var s = Object.assign({}, wel_item_images.settings); |
| 281 |
s.data = { |
| 282 |
'action': 'wel_item_image_ajax', |
| 283 |
'mode': 'delete_item_image', |
| 284 |
'post_id': post_id, |
| 285 |
'item_pict_ids': pict_ids, |
| 286 |
'_ajax_nonce': ajax_nonce |
| 287 |
}; |
| 288 |
$.ajax(s).done(function (res) { |
| 289 |
$("#wrap_img_icon_loading").remove(); |
| 290 |
$("#uscestabs_item_file #wel_file_delete").removeAttr('disabled'); |
| 291 |
if (res.status) { |
| 292 |
// Load list on the tab file. |
| 293 |
$('#uscestabs_item_images #uscestabs_item_file #wrapper_tab_file_item_pict').html(res.data_tab_file); |
| 294 |
// Load list on the tab image. |
| 295 |
$('#uscestabs_item_images #uscestabs_item_img').html(res.data_tab_image); |
| 296 |
} else if ('' != res.msg) { |
| 297 |
$("#wel_item_image_dialog_content").html(res.msg); |
| 298 |
$("#wel_item_image_dialog_wrap").dialog("open"); |
| 299 |
} |
| 300 |
}).fail(function (msg) { |
| 301 |
$("#wel_item_image_dialog_content").html(usces_item_images_js_setting.msg_nonce_expried); |
| 302 |
$("#wel_item_image_dialog_wrap").dialog("open"); |
| 303 |
$("#wrap_img_icon_loading").remove(); |
| 304 |
$("#uscestabs_item_file #wel_file_delete").removeAttr('disabled'); |
| 305 |
}); |
| 306 |
} |
| 307 |
}, |
| 308 |
change_item_pict: function ( elm_img ) { |
| 309 |
$("#uscestabs_item_img #item-select-pict").html(elm_img.attr('data-img')); |
| 310 |
$("#uscestabs_item_img #wel-img-alt").val(elm_img.attr('data-alt')); |
| 311 |
$("#uscestabs_item_img #wel-img-caption").val(elm_img.attr('data-caption')); |
| 312 |
$("#uscestabs_item_img #wel-img-pict-id").val(elm_img.attr('data-id')); |
| 313 |
}, |
| 314 |
save_pict_info: function ( field ) { |
| 315 |
var pict_id = $("#uscestabs_item_img #wel-img-pict-id").val(); |
| 316 |
var ol_elm = $("#uscestabs_item_img .subpict #wel-pict-" + pict_id); |
| 317 |
var old_val = ol_elm.attr('data-' + field); |
| 318 |
var elm_field = $("#uscestabs_item_img #wel-img-" + field); |
| 319 |
var val = elm_field.val(); |
| 320 |
if ( old_val !== val ) { |
| 321 |
elm_field.prop('disabled', true); |
| 322 |
var s = Object.assign({}, wel_item_images.settings); |
| 323 |
s.data = { |
| 324 |
'action': 'wel_item_image_ajax', |
| 325 |
'mode': 'save_pict_info', |
| 326 |
'pict_id': pict_id, |
| 327 |
'field': field, |
| 328 |
'value' : val, |
| 329 |
'_ajax_nonce': usces_item_images_js_setting._ajax_nonce |
| 330 |
}; |
| 331 |
$.ajax(s).done(function (res) { |
| 332 |
elm_field.prop('disabled', false); |
| 333 |
if (res.status) { |
| 334 |
ol_elm.attr('data-' + field, val); |
| 335 |
} |
| 336 |
}).fail(function (msg) { |
| 337 |
elm_field.prop('disabled', false); |
| 338 |
$("#wel_item_image_dialog_content").html(usces_item_images_js_setting.msg_nonce_expried); |
| 339 |
$("#wel_item_image_dialog_wrap").dialog("open"); |
| 340 |
}); |
| 341 |
} |
| 342 |
} |
| 343 |
}; |
| 344 |
}); |
| 345 |
|