|
1 |
(function($){
|
|
2 |
$(document).ready(function(){
|
|
3 |
var selection,
|
|
4 |
$altInput,
|
|
5 |
$mediaInsert,
|
|
6 |
$toolbarEl;
|
|
7 |
|
|
8 |
var removeWarning = function() {
|
|
9 |
$toolbarEl.find('.alt-tag-warning').remove();
|
|
10 |
}
|
|
11 |
|
|
12 |
var disableSubmit = function() {
|
|
13 |
$mediaInsert.attr('disabled', 'disabled');
|
|
14 |
}
|
|
15 |
|
|
16 |
var enableSubmit = function() {
|
|
17 |
$mediaInsert.removeAttr('disabled');
|
|
18 |
}
|
|
19 |
|
|
20 |
var bindSelectionEvents = function() {
|
|
21 |
selection = wp.media.frame.state().get('selection');
|
|
22 |
|
|
23 |
$toolbarEl = wp.media.frame.toolbar.view.$el;
|
|
24 |
|
|
25 |
$mediaInsert = $toolbarEl.find('button.media-button-insert');
|
|
26 |
|
|
27 |
selection.on( 'selection:single selection:multiple', function() {
|
|
28 |
// This element is not available until 'selection'.
|
|
29 |
$('label[data-setting="alt"] input').on('keyup', checkForAltText);
|
|
30 |
|
|
31 |
checkForAltText();
|
|
32 |
});
|
|
33 |
|
|
34 |
var uploaderView = wp.media.frame.views.get('.media-frame-uploader')[0]
|
|
35 |
uploaderView.on('ready', function() {
|
|
36 |
uploaderView.uploader.success = function( attData ) {
|
|
37 |
console.log(1);
|
|
38 |
visibleSidebarCallback();
|
|
39 |
wp.media.frame.views.get('.media-frame-content')[0].sidebar.$el.addClass( 'visible' );
|
|
40 |
}
|
|
41 |
})
|
|
42 |
}
|
|
43 |
|
|
44 |
var checkForAltText = function() {
|
|
45 |
var hasAltText = true;
|
|
46 |
var imageIsSelected = false;
|
|
47 |
|
|
48 |
selection.each(function(item){
|
|
49 |
if ( ! hasAltText ) {
|
|
50 |
return;
|
|
51 |
}
|
|
52 |
|
|
53 |
if ( 'image' !== item.attributes.type ) {
|
|
54 |
return;
|
|
55 |
} else {
|
|
56 |
imageIsSelected = true;
|
|
57 |
}
|
|
58 |
|
|
59 |
if ( ! item.attributes.hasOwnProperty( 'alt' ) ) {
|
|
60 |
hasAltText = false;
|
|
61 |
return;
|
|
62 |
}
|
|
63 |
|
|
64 |
var $altInput = $('label[data-setting="alt"] input');
|
|
65 |
|
|
66 |
hasAltText = $altInput.length > 0 && 0 !== $altInput.val().length;
|
|
67 |
});
|
|
68 |
|
|
69 |
// Existing warning should be removed in all cases.
|
|
70 |
removeWarning();
|
|
71 |
|
|
72 |
// Nothing more to do.
|
|
73 |
if ( ! imageIsSelected ) {
|
|
74 |
return;
|
|
75 |
}
|
|
76 |
|
|
77 |
if ( hasAltText ) {
|
|
78 |
enableSubmit();
|
|
79 |
|
|
80 |
} else {
|
|
81 |
var warning = '<div id="alt-tag-warning" class="alt-tag-warning">You must supply alt text before inserting this image.</div>';
|
|
82 |
|
|
83 |
$toolbarEl.find('.media-frame-toolbar .media-toolbar-primary').append(warning);
|
|
84 |
var sidebar = wp.media.frame.views.get('.media-frame-content')[0].sidebar;
|
|
85 |
sidebar.$el.find('label.setting[data-setting="alt"]').addClass('has-error');
|
|
86 |
|
|
87 |
disableSubmit();
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
// Bind our events to successful uploads.
|
|
92 |
$(document).ready(function(){
|
|
93 |
if ( typeof wp.Uploader === 'function' ) {
|
|
94 |
$.extend( wp.Uploader.prototype, {
|
|
95 |
success: function( attachment ) {
|
|
96 |
// Selection events must be rebound after redraw.
|
|
97 |
bindSelectionEvents();
|
|
98 |
|
|
99 |
checkForAltText();
|
|
100 |
}
|
|
101 |
} );
|
|
102 |
}
|
|
103 |
});
|
|
104 |
|
|
105 |
// Bind to the 'selection' events.
|
|
106 |
wp.media.view.Modal.prototype.on( 'ready', function() {
|
|
107 |
|
|
108 |
|
|
109 |
// cannot make this work - have to hide the message when switching to Upload Files tab
|
|
110 |
var router = wp.media.frame.views.get('.media-frame-router')[0];
|
|
111 |
|
|
112 |
console.log(router.views.view);
|
|
113 |
router.on('ready',function(){
|
|
114 |
console.log('no no no');
|
|
115 |
});
|
|
116 |
router.on('browse',function(){
|
|
117 |
console.log('amybe');
|
|
118 |
});
|
|
119 |
wp.media.view.Modal.prototype.on( 'open', bindSelectionEvents );
|
|
120 |
});
|
|
121 |
});
|
|
122 |
}(jQuery));
|