| 1 |
(function($){ |
| 2 |
"use strict"; |
| 3 |
|
| 4 |
$.kttkgallery = $.kttkgallery || {}; |
| 5 |
|
| 6 |
$(document).ready(function () { |
| 7 |
$.kttkgallery(); |
| 8 |
}); |
| 9 |
|
| 10 |
$.kttkgallery = function(){ |
| 11 |
// When the user clicks on the Add/Edit gallery button, we need to display the gallery editing |
| 12 |
$('body').on({ |
| 13 |
click: function(event){ |
| 14 |
var current_gallery = $(this).closest('.kttk_meta_image_gallery'); |
| 15 |
|
| 16 |
if (event.currentTarget.id === 'clear-gallery') { |
| 17 |
//remove value from input |
| 18 |
|
| 19 |
var rmVal = current_gallery.find('.gallery_values').val(''); |
| 20 |
|
| 21 |
//remove preview images |
| 22 |
current_gallery.find(".gallery_images").html(""); |
| 23 |
|
| 24 |
return; |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
// Make sure the media gallery API exists |
| 29 |
if ( typeof wp === 'undefined' || ! wp.media || ! wp.media.gallery ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
event.preventDefault(); |
| 33 |
|
| 34 |
// Activate the media editor |
| 35 |
var $$ = $(this); |
| 36 |
|
| 37 |
var val = current_gallery.find('.gallery_values').val(); |
| 38 |
wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({ |
| 39 |
template: function(view){ |
| 40 |
return; |
| 41 |
}, |
| 42 |
}); |
| 43 |
var final; |
| 44 |
if (!val) { |
| 45 |
var options = { |
| 46 |
frame: 'post', |
| 47 |
state: 'gallery', |
| 48 |
multiple: true |
| 49 |
}; |
| 50 |
|
| 51 |
var frame = wp.media.editor.open('gallery_values',options); |
| 52 |
} else { |
| 53 |
final = '[gallery ids="' + val + '"]'; |
| 54 |
frame = wp.media.gallery.edit(final); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
// When the gallery-edit state is updated, copy the attachment ids across |
| 60 |
frame.state('gallery-edit').on( 'update', function( selection ) { |
| 61 |
|
| 62 |
//clear screenshot div so we can append new selected images |
| 63 |
current_gallery.find(".gallery_images").html(""); |
| 64 |
|
| 65 |
var element, preview_html= "", preview_img, img_id; |
| 66 |
var ids = selection.models.map(function(e){ |
| 67 |
element = e.toJSON(); |
| 68 |
preview_img = typeof element.sizes.thumbnail !== 'undefined' ? element.sizes.thumbnail.url : element.url ; |
| 69 |
img_id = element.id; |
| 70 |
preview_html = '<a class="of-uploaded-image edit-kttk-meta-gal" data-attachment-id="'+img_id+'" href="#"><img class="gallery-widget-image" src="'+preview_img+'" /></a>'; |
| 71 |
current_gallery.find(".gallery_images").append(preview_html); |
| 72 |
return e.id; |
| 73 |
}); |
| 74 |
current_gallery.find('.gallery_values').val(ids.join(',')); |
| 75 |
current_gallery.find( '.gallery_values' ); |
| 76 |
|
| 77 |
}); |
| 78 |
|
| 79 |
|
| 80 |
return false; |
| 81 |
} |
| 82 |
}, '.kttk-gallery-attachments'); |
| 83 |
}; |
| 84 |
})(jQuery); |
| 85 |
|
| 86 |
(function($){ |
| 87 |
"use strict"; |
| 88 |
|
| 89 |
$.kttk_attachment_gallery = $.kttk_attachment_gallery || {}; |
| 90 |
|
| 91 |
$(document).ready(function () { |
| 92 |
$.kttk_attachment_gallery(); |
| 93 |
}); |
| 94 |
|
| 95 |
$.kttk_attachment_gallery = function(){ |
| 96 |
// When the user clicks on the Add/Edit gallery button, we need to display the gallery editing |
| 97 |
$('body').on({ |
| 98 |
click: function(event){ |
| 99 |
var current_gallery = $(this).closest('.kttk_meta_image_gallery'); |
| 100 |
var selected = $(this).data('attachment-id'); |
| 101 |
|
| 102 |
// Make sure the media gallery API exists |
| 103 |
if ( typeof wp === 'undefined' || ! wp.media || ! wp.media.gallery ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
event.preventDefault(); |
| 108 |
// Activate the media editor |
| 109 |
wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({ |
| 110 |
template: function(view){ |
| 111 |
return; |
| 112 |
}, |
| 113 |
}); |
| 114 |
var $$ = $(this); |
| 115 |
var val = current_gallery.find('.gallery_values').val(); |
| 116 |
var final = '[gallery ids="' + val + '"]'; |
| 117 |
var frame = wp.media.gallery.edit(final); |
| 118 |
|
| 119 |
// When the gallery-edit state is updated, copy the attachment ids across |
| 120 |
frame.state('gallery-edit').on( 'update', function( selection ) { |
| 121 |
|
| 122 |
//clear screenshot div so we can append new selected images |
| 123 |
current_gallery.find(".gallery_images").html(""); |
| 124 |
|
| 125 |
var element, preview_html= "", preview_img, img_id; |
| 126 |
var ids = selection.models.map(function(e){ |
| 127 |
element = e.toJSON(); |
| 128 |
preview_img = typeof element.sizes.thumbnail !== 'undefined' ? element.sizes.thumbnail.url : element.url ; |
| 129 |
img_id = element.id; |
| 130 |
preview_html = '<a class="of-uploaded-image edit-kttk-meta-gal" data-attachment-id="'+img_id+'" href="#"><img class="gallery-widget-image" src="'+preview_img+'" /></a>'; |
| 131 |
current_gallery.find(".gallery_images").append(preview_html); |
| 132 |
return e.id; |
| 133 |
}); |
| 134 |
current_gallery.find('.gallery_values').val(ids.join(',')); |
| 135 |
current_gallery.find( '.gallery_values' ); |
| 136 |
|
| 137 |
}); |
| 138 |
|
| 139 |
|
| 140 |
return false; |
| 141 |
} |
| 142 |
}, '.edit-kttk-meta-gal'); |
| 143 |
}; |
| 144 |
})(jQuery); |
| 145 |
|
| 146 |
|
| 147 |
|