| 1 |
var IJ_Post_Attachments; |
| 2 |
(function($) { |
| 3 |
$(document).ready(function() { |
| 4 |
IJ_Post_Attachments = new InJoin_PostAttachments(); |
| 5 |
}); |
| 6 |
|
| 7 |
/** |
| 8 |
* @class |
| 9 |
* @constructor |
| 10 |
*/ |
| 11 |
function InJoin_PostAttachments() { |
| 12 |
"use strict"; |
| 13 |
|
| 14 |
var self = this; |
| 15 |
|
| 16 |
/** |
| 17 |
* Container of the items list |
| 18 |
* @type {jQuery} |
| 19 |
*/ |
| 20 |
this.container = $("#ij-post-attachments"); |
| 21 |
|
| 22 |
/** |
| 23 |
* The URL of the plugin's directory |
| 24 |
* @type {String} |
| 25 |
*/ |
| 26 |
this.pluginUrl = userSettings.url + 'wp-content/plugins/ij-post-attachments/'; |
| 27 |
|
| 28 |
/** |
| 29 |
* The "Edit Media" title. |
| 30 |
* @type {String} |
| 31 |
*/ |
| 32 |
this.editMediaTitle = IJ_Post_Attachments_Vars.editMedia; |
| 33 |
|
| 34 |
/** |
| 35 |
* Current post ID |
| 36 |
* @type {Number} |
| 37 |
*/ |
| 38 |
this.postID = IJ_Post_Attachments_Vars.postID; |
| 39 |
|
| 40 |
/** |
| 41 |
* Defines whether we're dragging around or not |
| 42 |
* @type {Boolean} |
| 43 |
*/ |
| 44 |
this.dragging = false; |
| 45 |
|
| 46 |
/** |
| 47 |
* Event fired on "sortstart" |
| 48 |
* @type {Function} |
| 49 |
* @return {void} |
| 50 |
*/ |
| 51 |
this.onStartSorting = function() { |
| 52 |
self.dragging = true; |
| 53 |
}; |
| 54 |
|
| 55 |
/** |
| 56 |
* Event fired on "sortstop" |
| 57 |
* @type {Function} |
| 58 |
* @return {void} |
| 59 |
*/ |
| 60 |
this.onStopSorting = function() { |
| 61 |
self.dragging = false; |
| 62 |
}; |
| 63 |
|
| 64 |
/** |
| 65 |
* Event fired on "sortupdate" |
| 66 |
* @type {Function} |
| 67 |
* @return {void} |
| 68 |
*/ |
| 69 |
this.onUpdateSorting = function() { |
| 70 |
setTimeout(function() { |
| 71 |
if (!self.dragging) { |
| 72 |
var LI = $('li', self.container), i = 0, |
| 73 |
alignment = [], |
| 74 |
loader = $('<img />'); |
| 75 |
|
| 76 |
for (; i < LI.length; i++) { |
| 77 |
alignment.push(LI.eq(i).data('attachmentid')); |
| 78 |
} |
| 79 |
|
| 80 |
loader |
| 81 |
.attr('src', userSettings.url + 'wp-admin/images/wpspin_light.gif') |
| 82 |
.addClass('loader') |
| 83 |
.appendTo(self.container.find('h3.hndle')); |
| 84 |
|
| 85 |
$.ajax({ |
| 86 |
url : ajaxurl, |
| 87 |
data : { |
| 88 |
action : 'ij_realign', |
| 89 |
alignment : alignment |
| 90 |
} |
| 91 |
}).always(function() { |
| 92 |
self.container.find('h3.hndle img').remove(); |
| 93 |
}); |
| 94 |
} |
| 95 |
}, 1500); |
| 96 |
}; |
| 97 |
|
| 98 |
/** |
| 99 |
* Insert an attachment in the editor. |
| 100 |
* @type {Function} |
| 101 |
* @return {Boolean} |
| 102 |
*/ |
| 103 |
this.insertAttachment = function() { |
| 104 |
var LI = $(this).parents().filter('li:first'), |
| 105 |
el; |
| 106 |
|
| 107 |
if (LI.data('mimetype').indexOf('image/') === 0) { |
| 108 |
el = $('<img />') |
| 109 |
.addClass('alignnone size-full wp-image-' + LI.data('attachmentid')) |
| 110 |
.attr({ |
| 111 |
alt : LI.data('alt'), |
| 112 |
src : LI.data('url'), |
| 113 |
title : LI.data('title') |
| 114 |
}); |
| 115 |
} else { |
| 116 |
el = $('<a/>') |
| 117 |
.text(LI.data('title')) |
| 118 |
.attr('href', LI.data('url')); |
| 119 |
} |
| 120 |
|
| 121 |
send_to_editor(el[0].outerHTML); |
| 122 |
return false; |
| 123 |
}; |
| 124 |
|
| 125 |
/** |
| 126 |
* Opens the ThickBox with the media editing screen |
| 127 |
* @type {Function} |
| 128 |
* @return {Boolean} |
| 129 |
*/ |
| 130 |
this.showAttachment = function() { |
| 131 |
var ID = $(this).parents().filter('li:first').data('attachmentid'); |
| 132 |
|
| 133 |
// Because ThickBox removes everything after the TB_iframe parameter, |
| 134 |
// its better to keep it at the last position |
| 135 |
tb_show( |
| 136 |
self.editMediaTitle, |
| 137 |
ajaxurl + '?action=ij_attachment_edit&width=630&height=440&attachment_id=' + ID + '&post_id=' + self.postID + 'TB_iframe=1' |
| 138 |
); |
| 139 |
|
| 140 |
return false; |
| 141 |
}; |
| 142 |
|
| 143 |
/** |
| 144 |
* Deletes a media item |
| 145 |
* @type {Function} |
| 146 |
* @return {Boolean} |
| 147 |
*/ |
| 148 |
this.removeAttachment = function() { |
| 149 |
$.ajax({ |
| 150 |
url : $(this).attr('href'), |
| 151 |
// The line below will make WP redirect to our plugin after the deletion. |
| 152 |
// That way, less data will be downloaded. |
| 153 |
data : { _wp_http_referer : self.pluginUrl + 'ij-post-attachments.php' } |
| 154 |
}).done(function(ret) { |
| 155 |
if (!ret) { |
| 156 |
$(this.parentNode.parentNode).fadeOut(300, function() { |
| 157 |
$(this).remove(); |
| 158 |
}); |
| 159 |
} |
| 160 |
}); |
| 161 |
return false; |
| 162 |
}; |
| 163 |
|
| 164 |
/** |
| 165 |
* Sets up the widgets and events |
| 166 |
* @type {Function} |
| 167 |
* @return {void} |
| 168 |
*/ |
| 169 |
this.setup = function() { |
| 170 |
// Let's do some jQuerying! |
| 171 |
// Firstly, apply the sortable interaction. |
| 172 |
$("#ij-post-attachments > ul").sortable({ |
| 173 |
update : this.onUpdateSorting, |
| 174 |
start : this.onStartSorting, |
| 175 |
stop : this.onStopSorting |
| 176 |
}).disableSelection(); |
| 177 |
|
| 178 |
// Apply the syoHint plugin |
| 179 |
$('li.ij-post-attachment').autoHint(); |
| 180 |
|
| 181 |
// Bind the Insert Attachment behavior |
| 182 |
$('a.ij-post-attachment-insert').click(function() { |
| 183 |
self.insertAttachment.call(this); |
| 184 |
return false; |
| 185 |
}); |
| 186 |
|
| 187 |
// Bind the Show Attachment behavior to the title and to the Edit links |
| 188 |
$('a.ij-post-attachment-edit').click(function() { |
| 189 |
self.showAttachment.call(this); |
| 190 |
return false; |
| 191 |
}); |
| 192 |
|
| 193 |
// Bind the Remove Attachment behavior to the Remove link |
| 194 |
$('a.ij-post-attachment-delete').click(function() { |
| 195 |
self.removeAttachment.call(this); |
| 196 |
return false; |
| 197 |
}); |
| 198 |
}; |
| 199 |
|
| 200 |
this.setup(); |
| 201 |
} |
| 202 |
|
| 203 |
})(jQuery); |