| 1 |
/* globals wpdc */ |
| 2 |
/** |
| 3 |
* Toggles the 'hidden' class for the publishing_options metabox. |
| 4 |
* |
| 5 |
* @package WPDiscourse |
| 6 |
*/ |
| 7 |
|
| 8 |
(function( $ ) { |
| 9 |
$( 'input[type=radio][name=wpdc_publish_options]' ).change( |
| 10 |
function() { |
| 11 |
var val = this.value, |
| 12 |
$newTopicOptions = $( '.wpdc-new-discourse-topic' ), |
| 13 |
$linkPostOptions = $( '.wpdc-link-to-topic' ); |
| 14 |
if ( 'new' === val ) { |
| 15 |
$newTopicOptions.removeClass( 'hidden' ); |
| 16 |
$linkPostOptions.addClass( 'hidden' ); |
| 17 |
} else if ( 'link' === val ) { |
| 18 |
$newTopicOptions.addClass( 'hidden' ); |
| 19 |
$linkPostOptions.removeClass( 'hidden' ); |
| 20 |
} |
| 21 |
} |
| 22 |
); |
| 23 |
|
| 24 |
$( '.wpdc-advanced-options-toggle' ).click( |
| 25 |
function() { |
| 26 |
$( '.wpdc-advanced-options' ).toggleClass( 'hidden' ); |
| 27 |
} |
| 28 |
); |
| 29 |
|
| 30 |
$( '#update_discourse_topic' ).click( |
| 31 |
function() { |
| 32 |
var response; |
| 33 |
if ( $( this ).is( ':checked' ) ) { |
| 34 |
response = window.confirm( 'Updating the Discourse topic will overwrite the existing topic content on Discourse. Do you wish to proceed?' ); |
| 35 |
if ( ! response ) { |
| 36 |
$( this ).prop( 'checked', false ); |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
); |
| 41 |
|
| 42 |
$( '#unlink_from_discourse' ).click( |
| 43 |
function() { |
| 44 |
var response; |
| 45 |
if ( $( this ).is( ':checked' ) ) { |
| 46 |
response = window.confirm( 'Unlinking the post will remove all Discourse data from the post. You will need to update the post to complete the unlinking process. Do you wish to proceed?' ); |
| 47 |
if ( ! response ) { |
| 48 |
$( this ).prop( 'checked', false ); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
); |
| 53 |
|
| 54 |
$( '#pin_discourse_topic' ).click( |
| 55 |
function() { |
| 56 |
var $pinUntil = $( '.wpdc-pin-topic-time' ); |
| 57 |
$( this ).is( ':checked' ) ? $pinUntil.show() : $pinUntil.hide(); |
| 58 |
} |
| 59 |
); |
| 60 |
|
| 61 |
$( '#wpdc-tagadd' ).click( |
| 62 |
function() { |
| 63 |
var $tagInput = $( '#discourse-topic-tags' ), |
| 64 |
$tagList = $( '#wpdc-tagchecklist' ), |
| 65 |
$tagListErrors = $( '.wpdc-taglist-errors' ), |
| 66 |
tags = $tagInput.val(), |
| 67 |
maxTags = wpdc.maxTags, |
| 68 |
tooManyTags = false, |
| 69 |
tagArr; |
| 70 |
|
| 71 |
$tagInput.val( '' ); |
| 72 |
$tagListErrors.empty(); |
| 73 |
|
| 74 |
if ( tags ) { |
| 75 |
tagArr = tags.split( ',' ).map( |
| 76 |
function( e ) { |
| 77 |
// Only allow alphanumeric characters, dashes, underscores, and spaces. |
| 78 |
var allowedChars = new RegExp('^[a-zA-Z0-9\-\_ ]+$'); |
| 79 |
if ( allowedChars.test( e ) ) { |
| 80 |
|
| 81 |
return e.trim().replace( / /g, '-' ); |
| 82 |
} else { |
| 83 |
|
| 84 |
return ''; |
| 85 |
} |
| 86 |
} |
| 87 |
).filter( function( tag ) { |
| 88 |
|
| 89 |
return tag.length > 0; |
| 90 |
}); |
| 91 |
|
| 92 |
if ( tagArr ) { |
| 93 |
tagArr.forEach( |
| 94 |
function( tag ) { |
| 95 |
if ( $tagList.children( 'li' ).length < maxTags ) { |
| 96 |
$tagList.append( |
| 97 |
'<li class="wpdc-tag-item"><button type="button" class="wpdc-remove-tag">' + |
| 98 |
'<span class="wpdc-remove-tag-icon" aria-hidden="true"></span><span class="screen-reader-text">Remove term: ' + tag + '</span></button>' + |
| 99 |
' ' + tag + '<input name="wpdc_topic_tags[]" type="hidden" value="' + tag + '"></li>' |
| 100 |
); |
| 101 |
} else { |
| 102 |
tooManyTags = true; |
| 103 |
} |
| 104 |
} |
| 105 |
); |
| 106 |
|
| 107 |
if ( tooManyTags ) { |
| 108 |
$tagListErrors.append( 'You are only allowed ' + maxTags + ' tags per topic.' ); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
); |
| 114 |
|
| 115 |
$( '.wpdc-advanced-options' ).on( |
| 116 |
'click', '.wpdc-remove-tag', function() { |
| 117 |
$( this ).parent().remove(); |
| 118 |
$( '.wpdc-taglist-errors' ).empty(); |
| 119 |
} |
| 120 |
); |
| 121 |
|
| 122 |
// Toggle the Commenting Settings comment-type input. |
| 123 |
$( '#discourse-enable-discourse-comments' ).click( |
| 124 |
function() { |
| 125 |
$( '.discourse-comment-type' ).toggleClass( 'hidden' ); |
| 126 |
} |
| 127 |
); |
| 128 |
|
| 129 |
var $logControls = $('#wpdc-log-viewer-controls'); |
| 130 |
var $logViewer = $('#wpdc-log-viewer'); |
| 131 |
|
| 132 |
function handleLogResponse(response, logKey, meta) { |
| 133 |
if (response && response.data) { |
| 134 |
var title = (meta ? '' : 'Log for ') + response.data.name; |
| 135 |
$logControls.find('h3').html(title); |
| 136 |
$logViewer.find('pre').html(response.data.contents); |
| 137 |
} |
| 138 |
|
| 139 |
if (logKey) { |
| 140 |
$logViewer.data('log-key', logKey); |
| 141 |
} |
| 142 |
|
| 143 |
$logControls.toggleClass('meta', meta); |
| 144 |
$logViewer.removeClass('loading'); |
| 145 |
} |
| 146 |
|
| 147 |
$logControls.find('select').on('change', function() { |
| 148 |
var logKey = $logControls.find('select').val(); |
| 149 |
|
| 150 |
if (logKey) { |
| 151 |
$logViewer.addClass('loading'); |
| 152 |
|
| 153 |
$.ajax({ |
| 154 |
url: wpdc.ajax, |
| 155 |
type: 'post', |
| 156 |
data: { |
| 157 |
action: 'wpdc_view_log', |
| 158 |
nonce: wpdc.nonce, |
| 159 |
key: logKey |
| 160 |
}, |
| 161 |
success: function(response) { |
| 162 |
if (response.success) { |
| 163 |
handleLogResponse(response, logKey, false); |
| 164 |
} |
| 165 |
} |
| 166 |
}); |
| 167 |
} |
| 168 |
}); |
| 169 |
|
| 170 |
$logControls.find('.load-log').on('click', function() { |
| 171 |
var logKey = $logViewer.data('log-key'); |
| 172 |
|
| 173 |
if (logKey) { |
| 174 |
$logViewer.addClass('loading'); |
| 175 |
|
| 176 |
$.ajax({ |
| 177 |
url: wpdc.ajax, |
| 178 |
type: 'post', |
| 179 |
data: { |
| 180 |
action: 'wpdc_view_log', |
| 181 |
nonce: wpdc.nonce, |
| 182 |
key: logKey |
| 183 |
}, |
| 184 |
success: function(response) { |
| 185 |
if (response.success) { |
| 186 |
handleLogResponse(response, logKey, false); |
| 187 |
} |
| 188 |
} |
| 189 |
}); |
| 190 |
} |
| 191 |
}); |
| 192 |
|
| 193 |
$logControls.find('.button.view-meta').on('click', function() { |
| 194 |
$logViewer.addClass('loading'); |
| 195 |
$.ajax({ |
| 196 |
url: wpdc.ajax, |
| 197 |
type: 'post', |
| 198 |
data: { |
| 199 |
action: 'wpdc_view_logs_metafile', |
| 200 |
nonce: wpdc.nonce |
| 201 |
}, |
| 202 |
success: function(response) { |
| 203 |
if (response.success) { |
| 204 |
handleLogResponse(response, null, true); |
| 205 |
} |
| 206 |
} |
| 207 |
}); |
| 208 |
}); |
| 209 |
|
| 210 |
$logControls.find('.button.download-logs').on('click', function() { |
| 211 |
var xhr = new XMLHttpRequest(); |
| 212 |
xhr.open('POST', wpdc.ajax + `?action=wpdc_download_logs&nonce=${wpdc.nonce}`, true); |
| 213 |
xhr.onload = function() { |
| 214 |
if (xhr.readyState === 4 && xhr.status === 200) { |
| 215 |
var blob = new Blob([ xhr.response ], { type: 'application/zip' }); |
| 216 |
var url = window.URL.createObjectURL(blob); |
| 217 |
var a = document.createElement('a'); |
| 218 |
|
| 219 |
document.body.appendChild(a); |
| 220 |
a.style = 'display:none'; |
| 221 |
a.href = url; |
| 222 |
a.download = xhr.getResponseHeader('Content-Disposition').split('filename=')[1]; |
| 223 |
a.click(); |
| 224 |
a.remove(); |
| 225 |
setTimeout(function() { |
| 226 |
window.URL.revokeObjectURL(url); |
| 227 |
}); |
| 228 |
} |
| 229 |
}; |
| 230 |
xhr.responseType = 'arraybuffer'; |
| 231 |
xhr.send(); |
| 232 |
}); |
| 233 |
|
| 234 |
if ( $('.tagsdiv').length ) { |
| 235 |
window.tagBox && window.tagBox.init(); |
| 236 |
} |
| 237 |
})( jQuery ); |
| 238 |
|