1/* global wpforo */ 2var $wpf = Object.assign(window.jQuery); 3 4// Mute jQuery Migrate Warnings 5$wpf.migrateMute = true; 6 7$wpf.fn.extend({ 8 visible: function () { 9 return this.css('visibility', 'visible'); 10 }, 11 invisible: function () { 12 return this.css('visibility', 'hidden'); 13 }, 14 visibilityToggle: function () { 15 return this.css('visibility', function (i, visibility) { 16 return (visibility === 'visible') ? 'hidden' : 'visible'; 17 }); 18 }, 19 showFlex: function () { 20 return this.css('display', 'flex'); 21 }, 22 wpfInsertAtCaret: function (myValue) { 23 return this.each(function () { 24 if (document.selection) { 25 //For browsers like Internet Explorer 26 this.focus(); 27 var sel = document.selection.createRange(); 28 sel.text = myValue; 29 } else if (this.selectionStart || this.selectionStart === 0) { 30 //For browsers like Firefox and Webkit based 31 var startPos = this.selectionStart; 32 var endPos = this.selectionEnd; 33 this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length); 34 this.selectionStart = startPos + myValue.length; 35 this.selectionEnd = startPos + myValue.length; 36 } else { 37 this.value += myValue; 38 } 39 }); 40 }, 41 isOnScreen: function (cbTrue, cbFalse) { 42 var win = $wpf(window); 43 44 var viewport = { 45 top: win.scrollTop(), 46 left: win.scrollLeft(), 47 }; 48 viewport.right = viewport.left + win.width(); 49 viewport.bottom = viewport.top + win.height(); 50 51 var bounds = this.offset(); 52 bounds.right = bounds.left + this.outerWidth(); 53 bounds.bottom = bounds.top + this.outerHeight(); 54 55 var isOnScreen = (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom)); 56 57 if (!cbTrue && !cbFalse) { 58 return isOnScreen; 59 } else { 60 return isOnScreen 61 ? cbTrue && cbTrue(viewport, bounds) 62 : cbFalse && cbFalse(viewport, bounds); 63 } 64 }, 65}); 66 67// returns the cookie with the given name, 68// or undefined if not found 69function wpforo_getCookie (name) { 70 let matches = document.cookie.match(new RegExp( 71 '(?:^|; )' + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + '=([^;]*)', 72 )); 73 return matches ? decodeURIComponent(matches[1]) : undefined; 74} 75 76function wpforo_setCookie (name, value, options = {}) { 77 78 options = { 79 path: '/', 80 // add other defaults here if necessary 81 ...options, 82 }; 83 84 if (options.expires instanceof Date) { 85 options.expires = options.expires.toUTCString(); 86 } 87 88 let updatedCookie = encodeURIComponent(name) + '=' + encodeURIComponent(value); 89 90 for (let optionKey in options) { 91 updatedCookie += '; ' + optionKey; 92 let optionValue = options[optionKey]; 93 if (optionValue !== true) { 94 updatedCookie += '=' + optionValue; 95 } 96 } 97 98 document.cookie = updatedCookie; 99} 100 101function wpforo_deleteCookie (name) { 102 wpforo_setCookie(name, '', { 103 'max-age': -1, 104 }); 105} 106 107function wpforo_get_browser_timezone () { 108 if (typeof Intl === 'object' && typeof Intl.DateTimeFormat === 'function') { 109 try { 110 const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; 111 if (timeZone !== undefined && timeZone !== null) return timeZone; 112 } catch (error) { 113 console.error('Error accessing time zone:', error); 114 } 115 } else { 116 console.error('Intl.DateTimeFormat is not supported in this environment.'); 117 } 118 119 return ''; 120} 121 122/** 123 * Trigger a custom event. 124 * 125 * @param {Element|Document} target HTML element to dispatch the event on. 126 * @param {string} name Event name. 127 * @param [detail = null] Event addintional data information. 128 */ 129function wpforo_trigger_custom_event (target, name, detail) { 130 if (typeof detail === 'undefined') detail = null; 131 var event; 132 if (typeof CustomEvent === 'function') { 133 event = new CustomEvent(name, { bubbles: true, cancelable: true, detail: detail }); 134 } else { 135 event = document.createEvent('Event'); 136 event.initEvent(name, true, true); 137 event.detail = detail; 138 } 139 140 target.dispatchEvent(event); 141 window['wpforo_break_after_custom_event_'.name] = false; 142} 143 144function wpforo_tinymce_initializeIt (selector, do_not_focus) { 145 if (wpforo_editor.is_tinymce_loaded()) { 146 $wpf(selector).each(function () {
147 if (this.id) { 148 var editor_settings = window['wpforo_editor_settings_' + this.id] || wpforo['editor_settings']; 149 var settings = { 150 selector: '#' + this.id, 151 relative_urls: false, 152 remove_script_host: false, 153 convert_urls: false, 154 keep_styles: false, 155 end_container_on_empty_block: true, 156 wpeditimage_html5_captions: true, 157 force_br_newlines: false, 158 force_p_newlines: true, 159 menubar: false, 160 branding: false, 161 elementpath: true, 162 statusbar: true, 163 fix_list_elements: true, 164 browser_spellcheck: true, 165 entities: '38,amp,60,lt,62,gt', 166 entity_encoding: 'raw', 167 resize: 'vertical', 168 preview_styles: 'font-family font-size font-weight font-style text-decoration text-transform', 169 forced_root_block: '', 170 plugins: editor_settings['plugins'], 171 external_plugins: editor_settings['external_plugins'], 172 min_height: editor_settings['editor_height'], 173 indent: editor_settings['indent'], 174 height: editor_settings['editor_height'], 175 wpautop: editor_settings['wpautop'], 176 wp_keep_scroll_position: editor_settings['wp_keep_scroll_position'], 177 add_unload_trigger: editor_settings['add_unload_trigger'], 178 }; 179 settings = Object.assign(settings, editor_settings['tinymce']); 180 tinymce.init(settings).then(function (e) { 181 if (!do_not_focus && e.length) { 182 wpforo_editor.focus(e[0].id); 183 wpforo_editor.set_active(e[0].id); 184 } 185 }); 186 187 } 188 }); 189 } 190} 191 192function wpforo_tinymce_setup (editor) { 193 editor.on('init', function (e) { 194 wpforo_trigger_custom_event(document, 'wpforo_tinymce_init', e); 195 }); 196 editor.on('focus', function (e) { 197 wpforo_trigger_custom_event(document, 'wpforo_tinymce_focus', e); 198 wpforo_editor.set_active(editor.id); 199 }); 200 editor.on('Dirty ExecCommand KeyPress SetContent', function (e) { 201 wpforo_trigger_custom_event(document, 'wpforo_tinymce_content_changed', e); 202 }); 203 editor.on('paste', function (e) { 204 wpforo_trigger_custom_event(document, 'wpforo_tinymce_paste', e); 205 }); 206 editor.shortcuts.add('ctrl+13', 'submit', function (e) { 207 wpforo_trigger_custom_event(document, 'wpforo_tinymce_ctrl_enter', e); 208 $wpf('form[data-textareaid="' + editor.id + '"]').find('[type=submit]').trigger('click'); 209 }); 210 editor.shortcuts.add('ctrl+s', 'Save Draft', function (e) { 211 wpforo_trigger_custom_event(document, 'wpforo_tinymce_ctrl_s', e); 212 }); 213} 214 215var wpforo_editor = { 216 active_textareaid: '', 217 main_textareaid: '', 218 fix_textareaid: function (textareaid) { 219 if (typeof textareaid !== 'undefined') { 220 return textareaid; 221 } else if (this.active_textareaid) { 222 return this.active_textareaid; 223 } else { 224 var tinymce_active_editor_id = this.get_tinymce_active_editor_id(); 225 if (tinymce_active_editor_id) { 226 this.active_textareaid = tinymce_active_editor_id; 227 return tinymce_active_editor_id; 228 } 229 } 230 return ''; 231 }, 232 get_active_textareaid: function () { 233 return this.fix_textareaid(); 234 }, 235 set_active: function (textareaid) { 236 if (this.is_exists(textareaid)) { 237 this.active_textareaid = textareaid; 238 if (this.is_tinymce(textareaid)) tinymce.setActive(tinymce.get(textareaid)); 239 } 240 }, 241 clear_active: function () { 242 this.active_textareaid = ''; 243 }, 244 set_main: function (textareaid, also_set_active) { 245 if (!textareaid) { 246 var wpforo_main_form = $wpf('form.wpforo-main-form[data-textareaid]'); 247 if (wpforo_main_form.length) textareaid = wpforo_main_form.data('textareaid'); 248 } 249 if (this.is_exists(textareaid)) { 250 this.main_textareaid = textareaid; 251 if (also_set_active) this.set_active(textareaid); 252 } 253 }, 254 get_main: function () { 255 if (!this.main_textareaid) this.set_main(); 256 return this.main_textareaid; 257 }, 258 clear_main: function () { 259 this.main_textareaid = ''; 260 }, 261 get_tinymce_active_editor_id: function () { 262 if (this.is_tinymce_loaded() && typeof tinymce.activeEditor === 'object' && tinymce.activeEditor && tinymce.activeEditor.id) { 263 return tinymce.activeEditor.id; 264 } 265 return ''; 266 }, 267 is_tinymce_loaded: function () { 268 return typeof tinymce !== 'undefined'; 269 }, 270 is_tinymce: function (textareaid) { 271 textareaid = this.fix_textareaid(textareaid);
272 return !!(textareaid && this.is_tinymce_loaded() && tinymce.get(textareaid)); 273 }, 274 is_textarea: function (textareaid) { 275 textareaid = this.fix_textareaid(textareaid); 276 return !!(textareaid && !this.is_tinymce(textareaid) && $wpf('textarea#' + textareaid).length); 277 }, 278 is_exists: function (textareaid) { 279 return !!(textareaid && this.is_tinymce(textareaid) || this.is_textarea(textareaid)); 280 }, 281 get_form: function (textareaid) { 282 textareaid = this.fix_textareaid(textareaid); 283 var textarea = $wpf('textarea[id="' + textareaid + '"]'); 284 if (textarea.length) { 285 var form = textarea.closest('form'); 286 if (form.length) return form; 287 } 288 return null; 289 }, 290 get_form_body_wrap: function (textareaid) { 291 textareaid = this.fix_textareaid(textareaid); 292 var ed; 293 var body_wrap = null; 294 if (this.is_tinymce(textareaid)) { 295 ed = tinymce.get(textareaid); 296 if (ed && ed.iframeElement) { 297 body_wrap = ed.iframeElement.parentElement; 298 } 299 } else { 300 ed = document.getElementById(textareaid); 301 if (ed) { 302 body_wrap = ed.parentElement; 303 } 304 } 305 return body_wrap ? $wpf(body_wrap) : null; 306 }, 307 scrollto: function (textareaid) { 308 textareaid = this.fix_textareaid(textareaid); 309 var scrollto = null; 310 var bodyWrap = this.get_form_body_wrap(textareaid); 311 if (bodyWrap !== null) scrollto = bodyWrap.offset().top - 100; 312 if (scrollto !== null && !bodyWrap.isOnScreen()) { 313 $wpf('html').scrollTop(scrollto); 314 } 315 }, 316 tinymce_focus: function (textareaid, caret_to_end) { 317 textareaid = this.fix_textareaid(textareaid); 318 if (this.is_tinymce(textareaid)) { 319 this.scrollto(textareaid); 320 var focus_mce = tinymce.get(textareaid); 321 focus_mce.focus(); 322 if (caret_to_end) { 323 focus_mce.selection.select(focus_mce.getBody(), true); 324 focus_mce.selection.collapse(false); 325 } 326 } 327 }, 328 textarea_focus: function (textareaid, caret_to_end) { 329 textareaid = this.fix_textareaid(textareaid); 330 if (this.is_textarea(textareaid)) { 331 this.scrollto(textareaid); 332 var textarea = $wpf('textarea#' + textareaid); 333 var textarea_val = textarea.val(); 334 textarea.trigger('focus'); 335 if (caret_to_end) { 336 textarea.val(''); 337 textarea.val(textarea_val); 338 } 339 } 340 }, 341 focus: function (textareaid, caret_to_end) { 342 textareaid = this.fix_textareaid(textareaid); 343 if (this.is_tinymce(textareaid)) { 344 this.tinymce_focus(textareaid, caret_to_end); 345 } else if (this.is_textarea(textareaid)) { 346 this.textarea_focus(textareaid, caret_to_end); 347 } 348 }, 349 insert_content: function (content, textareaid, format, focus) { 350 textareaid = this.fix_textareaid(textareaid); 351 format = format ? format : 'raw'; 352 if (focus === undefined) focus = true; 353 if (this.is_tinymce(textareaid)) { 354 tinymce.get(textareaid).insertContent(content, { format: format }); 355 if (focus) this.tinymce_focus(textareaid); 356 } else if (this.is_textarea(textareaid)) { 357 $wpf('textarea#' + textareaid).wpfInsertAtCaret(content); 358 if (focus) this.textarea_focus(textareaid); 359 } 360 }, 361 set_content: function (content, textareaid, format, focus) { 362 textareaid = this.fix_textareaid(textareaid); 363 format = format ? format : 'raw'; 364 if (focus === undefined) focus = true; 365 if (this.is_tinymce(textareaid)) { 366 tinymce.get(textareaid).setContent(content, { format: format }); 367 if (focus) this.tinymce_focus(textareaid, true); 368 } else if (this.is_textarea(textareaid)) { 369 $wpf('textarea#' + textareaid).val(content); 370 if (focus) this.textarea_focus(textareaid, true); 371 } 372 }, 373 get_content: function (format, textareaid) { 374 textareaid = this.fix_textareaid(textareaid); 375 format = format ? format : 'text'; 376 var content = ''; 377 if (this.is_tinymce(textareaid)) { 378 content = tinymce.get(textareaid).getContent({ format: format }); 379 } else if (this.is_textarea(textareaid)) { 380 content = $wpf('textarea#' + textareaid).val(); 381 if (format === 'text' && content) { 382 content = content.replace(/<(iframe|embed)[^<>]*?>.*?<\/\1>/gi, ''); 383 content = content.replace(/(<([^<>]+?)>)/gi, ''); 384 } 385 } 386 return content.trim(); 387 }, 388 get_stats: function (textareaid) { 389 textareaid = this.fix_textareaid(textareaid); 390 391 var text = this.get_content('text', textareaid); 392 var raw_text = this.get_content('raw', textareaid); 393 var chars = text.length; 394 var words = text.split(/[\w\u2019'-]+/).length - 1; 395 var imgs = (raw_text.match(/<img[^<>]*?src=['"][^'"]+?['"][^<>]*?>/gi) || []).length; 396 var links = (raw_text.match(/<a[^<>]*?href=['"][^'"]+?['"][^<>]*?>.+?<\/a>/gi) || []).length; 397 var embeds = (raw_text.match(/<(iframe|embed)[^<>]*?>.*?<\/\1>/gi) || []).length; 398 399 return { 400 chars: chars, 401 words: words, 402 imgs: imgs, 403 links: links, 404 embeds: embeds, 405 has_content: !!(chars || imgs || links || embeds), 406 }; 407 }, 408}; 409 410function wpforo_notice_get_timeout (type) { 411 if (!type) type = 'neutral'; 412 if (wpforo.notice.timeouts[type] !== undefined) return parseInt(wpforo.notice.timeouts[type]); 413 return 8000; 414} 415 416function wpforo_notice_clear () { 417 var msg_box = $wpf('#wpf-msg-box'); 418 msg_box.hide(); 419 msg_box.empty(); 420} 421 422function wpforo_notice_show (notice, type) { 423 if (!notice) return; 424 type = (type === 'success' || type === 'error' ? type : 'neutral'); 425 426 var n = notice.search(/<p(?:\s[^<>]*?)?>/i); 427 if (n < 0) { 428 var phrase = wpforo_phrase(notice); 429 if (arguments.length > 2) { 430 for (var i = 2; i < arguments.length; i++) { 431 if (arguments[i] !== undefined) phrase = phrase.replace(/%[dfs]/, arguments[i]); 432 } 433 } 434 notice = '<p class="' + type + '">' + phrase + '</p>'; 435 } 436 437 notice = $wpf(notice); 438 $wpf('#wpforo-notifications-bar').appendTo('body'); 439 var msg_box = $wpf('#wpf-msg-box'); 440 msg_box.append(notice); 441 msg_box.show(150); 442 var timeout = wpforo_notice_get_timeout(type); 443 if (timeout) { 444 notice.delay(timeout).fadeOut(200, function () { 445 $wpf(this).remove(); 446 }); 447 } 448} 449 450function wpforo_notice_hide () { 451 $wpf('#wpf-msg-box').hide(); 452} 453 454function wpforo_load_show (msg) { 455 msg = typeof msg !== 'undefined' ? msg : 'Working'; 456 msg = String(msg); 457 msg = wpforo_phrase(msg); 458 $wpf('#wpforo-notifications-bar').appendTo('body'); 459 var load = $wpf('#wpforo-load'); 460 $wpf('.loadtext', load).text(msg); 461 load.showFlex(); 462} 463 464function wpforo_load_hide () { 465 $wpf('#wpforo-load').hide(); 466} 467 468function wpforo_init_dialog () { 469 $wpf('#wpforo-dialog-extra-wrap').on('click', '#wpforo-dialog-close', function () { 470 wpforo_dialog_hide(); 471 }); 472 $wpf(document).on('mousedown', '#wpforo-dialog-extra-wrap', function (e) { 473 if (!$wpf(e.target).closest('#wpforo-dialog').length) wpforo_dialog_hide(); 474 }); 475 $wpf(document).on('keydown', function (e) { 476 if (e.code === 'Escape') wpforo_dialog_hide(); 477 }); 478} 479 480function wpforo_dialog_show (title, content, w, h) { 481 var dialog = $wpf('#wpforo-dialog'); 482 if (content) { 483 var dialog_body = $wpf('#wpforo-dialog-body', dialog); 484 dialog_body.children().appendTo('#wpforo-dialog-backups'); 485 dialog_body.empty(); 486 if (content instanceof $wpf || content instanceof Element) { 487 content = $wpf(content); 488 content.appendTo(dialog_body); 489 content.show(); 490 content.css('visibility', 'visible'); 491 if (!title) title = content.data('title'); 492 } else if (typeof content === 'string') { 493 dialog_body.html(content); 494 } 495 } 496 if (title) $wpf('#wpforo-dialog-title', dialog).html(wpforo_phrase(title)); 497 if (w) dialog.css('width', w); 498 if (h) dialog.css('height', h); 499 $wpf('#wpforo-dialog-extra-wrap').appendTo('body'); 500 $wpf('html').addClass('wpforo-dialog-visible'); 501 $wpf('body').addClass('wpforo-dialog-visible animated fadeIn'); 502} 503 504function wpforo_dialog_hide () { 505 $wpf('html').removeClass('wpforo-dialog-visible'); 506 $wpf('body').removeClass('wpforo-dialog-visible animated fadeIn'); 507} 508 509function wpforo_phrase (phrase_key) { 510 // if( !(window.wpforo_phrases && typeof window.wpforo_phrases === 'object' && Object.keys(window.wpforo_phrases).length) ) wpforo_init_phrases(); 511 if (window.wpforo_phrases && typeof window.wpforo_phrases === 'object' && Object.keys(window.wpforo_phrases).length) { 512 var phrase_key_lower = phrase_key.toLowerCase(); 513 if (window.wpforo_phrases[phrase_key_lower] !== undefined) phrase_key = window.wpforo_phrases[phrase_key_lower]; 514 } 515 return phrase_key; 516} 517 518function wpforo_getTextSelection () { 519 $wpf('#wpf_multi_quote').remove(); 520 if (window.getSelection) { 521 var sel = window.getSelection(); 522 if (sel && sel.anchorNode && sel.anchorNode.parentNode && sel.anchorNode.parentNode.tagName !== 'A') { 523 var selectedText = sel.toString().trim(); 524 if (sel.rangeCount && selectedText.length) { 525 var getRangeAt_0 = sel.getRangeAt(0); 526 var rangeBounding = getRangeAt_0.getBoundingClientRect(); 527 var bodyBounding = document.documentElement.getBoundingClientRect(); 528 var left = rangeBounding.left + rangeBounding.width / 2 + Math.abs(bodyBounding.left) - 15; 529 var top = rangeBounding.bottom + Math.abs(bodyBounding.top) + 50; 530 531 var parent = $wpf(getRangeAt_0.commonAncestorContainer).closest('.wpforo-post-content, .wpforo-comment-content'); 532 var noNeedParent = $wpf(getRangeAt_0.commonAncestorContainer).closest('.wpforo-post-signature, .wpforo-post-content-bottom, .wpf-post-button-actions'); 533 var noNeedChild = $wpf(getRangeAt_0.endContainer).closest('.wpforo-post-signature, .wpforo-post-content-bottom, .wpf-post-button-actions'); 534 535 if (parent.length && !noNeedParent.length && !noNeedChild.length) { 536 var toolTip = $wpf('<div id="wpf_multi_quote"></div>'); 537 toolTip.css({ top: top, left: left }); 538 var link = $wpf('<span class="wpf-multi-quote" title="' + wpforo_phrase('Quote this text') + '"><i class="fas fa-quote-left"></i></span>').on('mousedown touchstart', function () { 539 var container = document.createElement('div'); 540 for (var i = 0; i < sel.rangeCount; ++i) container.appendChild(sel.getRangeAt(i).cloneContents()); 541 var post_wrap = $wpf(getRangeAt_0.startContainer).parents('[data-postid]'); 542 var userid = post_wrap.data('userid'); 543 if (!userid) userid = 0; 544 var postid = post_wrap.data('postid'); 545 if (!postid) postid = 0; 546 var editorContent = ''; 547 if (wpforo_editor.is_tinymce()) { 548 editorContent = '[quote data-userid="' + userid + '" data-postid="' + postid + '"]<p>' + container.innerHTML.replace(/\s*data-[\w-]+="[^"]*?"/gi, '') + '</p>[/quote]<p></p>'; 549 } else { 550 editorContent = '[quote data-userid="' + userid + '" data-postid="' + postid + '"]' + container.innerHTML.replace(/\s*data-[\w-]+="[^"]*?"/gi, '') + '[/quote]'; 551 } 552 wpforo_editor.insert_content(editorContent); 553 $wpf(this).remove(); 554 }); 555 toolTip.append(link); 556 $wpf('body').append(toolTip); 557 } 558 } 559 560 } 561 } 562} 563 564window.wpforo_fix_form_data_attributes = function () { 565 $wpf('form textarea[data-isbody=1]:first').each(function () { 566 var form = $wpf(this).closest('form'); 567 var id = $wpf(this).attr('id'); 568 form.attr('data-textareaid', id); 569 form.prop('data-textareaid', id); 570 form.data('textareaid', id); 571 }); 572}; 573 574$wpf(document).ready(function ($) {
575 var iwpfp = $('body.is_wpforo_page-1'); 576 if (iwpfp.length) $('html').addClass('is_wpforo_page-1'); 577 578 var wpforo_wrap = $('#wpforo-wrap'); 579 580 wpforo_setCookie('wpforo_browser_timezone', wpforo_get_browser_timezone(), { secure: true, 'max-age': 86400 }); 581 582 var scroll_to; 583 var exreg = new RegExp('\/' + wpforo.settings_slugs['postid'] + '\/(\\d+)\/?$', 'i'); 584 var match = location.pathname.match(exreg); 585 if (match) { 586 scroll_to = $('#post-' + match[1]); 587 } else { 588 //scroll_to = $("#m_, .wpforo-members-content, .wpforo-search-content", wpforo_wrap); 589 } 590 if (scroll_to !== undefined && scroll_to.length) { 591 $('html, body').scrollTop(scroll_to.offset().top - 25); 592 } 593 594 wpforo_init_dialog(); 595 596 if ($('form.wpforo-main-form').length) { 597 document.onselectionchange = function () { 598 wpforo_getTextSelection(); 599 }; 600 } 601 602 window.onbeforeunload = function (e) { 603 var forms = $('form[data-textareaid]').not(':hidden'); 604 if (forms.length) { 605 var i, textareaid; 606 for (i = 0; i < forms.length; i++) { 607 textareaid = $(forms[i]).data('textareaid'); 608 if (wpforo_editor.get_stats(textareaid).has_content) { 609 e = e || window.event; 610 e.returnValue = wpforo_phrase('Write something clever here..'); 611 return wpforo_phrase('Write something clever here..'); 612 } 613 } 614 } 615 }; 616 617 window.wpforo_fix_form_data_attributes(); 618 wpforo_tinymce_initializeIt('[data-wpforoeditor="tinymce"]', true); 619 620 setTimeout(function () { 621 wpforo_editor.fix_textareaid(); 622 wpforo_editor.set_main('', true); 623 624 var forum_sels = $('.wpf-topic-form-extra-wrap .wpf-topic-form-forumid', wpforo_wrap); 625 if (forum_sels.length) { 626 forum_sels.each(function (i, f) { 627 f = $(f); 628 var forum_opts = $('option:not([value="0"]):not([disabled])', f); 629 if (forum_opts.length === 1) { 630 631 var wpf_topic_form_extra_wrap = f.closest('.wpf-topic-form-extra-wrap'); 632 wpf_topic_form_extra_wrap.attr('data-is_just_one_forum', true); 633 wpf_topic_form_extra_wrap.prop('data-is_just_one_forum', true); 634 wpf_topic_form_extra_wrap.data('is_just_one_forum', true); 635 636 f.val(forum_opts[0].getAttribute('value')).trigger('change'); 637 } 638 }); 639 } 640 641 }, 1000); 642 643 wpforo_wrap.on('click drop', 'form[data-textareaid]', function () { 644 var textareaid = $(this).data('textareaid'); 645 wpforo_editor.set_active(textareaid); 646 }); 647 648 wpforo_wrap.on('focus', 'form[data-textareaid] textarea', function () { 649 var textareaid = $(this).parents('form[data-textareaid]').data('textareaid'); 650 if (textareaid === this.id) wpforo_editor.set_active(this.id); 651 }); 652 653 wpforo_wrap.on('keydown', 'form[data-textareaid]', function (e) { 654 if ((e.ctrlKey || e.metaKey) && (e.code === 'Enter' || e.code === 'NumpadEnter')) { 655 $('[type=submit]', $(this)).trigger('click'); 656 } else if (((e.ctrlKey || e.metaKey) && e.code === 'KeyS') || e.code === 'Pause' || e.code === 'MediaPlayPause') { 657 wpforo_trigger_custom_event(document, 'wpforo_textarea_ctrl_s', e); 658 e.preventDefault(); 659 return false; 660 } 661 }); 662 663 if ($('.wpforo-recent-content .wpf-p-error', wpforo_wrap).length) { $('.wpf-navi', wpforo_wrap).remove(); } 664 665 /** 666 * prevent multi submitting 667 * disable form elements for 10 seconds 668 */ 669 window.wpforo_prev_submit_time = 0; 670 wpforo_wrap.on('submit', 'form', function (e) { 671 if (window.wpforo_prev_submit_time) { 672 if (Date.now() - window.wpforo_prev_submit_time < 10000) return false; 673 } else { 674 var textareaid = $(this).data('textareaid'); 675 if (textareaid) { 676 var bodyminlength = $(this).data('bodyminlength'); 677 var bodymaxlength = $(this).data('bodymaxlength'); 678 if (bodyminlength || bodymaxlength) { 679 var body_stat = wpforo_editor.get_stats(textareaid); 680 if (bodyminlength) { 681 if (body_stat.chars < bodyminlength && !body_stat.embeds && !body_stat.links && !body_stat.imgs) { 682 wpforo_notice_show('Content characters length must be greater than %d', 'error', bodyminlength); 683 return false; 684 } 685 } 686 if (bodymaxlength) { 687 if (body_stat.chars > bodymaxlength) { 688 wpforo_notice_show('Content characters length must be smaller than %d', 'error', bodymaxlength); 689 return false;
690 } 691 } 692 } 693 694 wpforo_trigger_custom_event(document, 'wpforo_post_submit', { e, textareaid }); 695 if (window['wpforo_break_after_custom_event_wpforo_post_submit']) { 696 window['wpforo_break_after_custom_event_wpforo_post_submit'] = false; 697 return false; 698 } 699 } 700 701 wpforo_load_show(); 702 window.wpforo_prev_submit_time = Date.now(); 703 window.onbeforeunload = null; 704 setTimeout(function () { 705 window.wpforo_prev_submit_time = 0; 706 wpforo_load_hide(); 707 }, 10000); 708 } 709 }); 710 711 wpforo_wrap.on('click', '.wpf-spoiler-head', function () { 712 var spoiler_wrap = $(this).parents('.wpf-spoiler-wrap'); 713 if (spoiler_wrap.length) { 714 spoiler_wrap = $(spoiler_wrap[0]); 715 if (!spoiler_wrap.hasClass('wpf-spoiler-processing')) { 716 spoiler_wrap.toggleClass('wpf-spoiler-open').addClass('wpf-spoiler-processing'); 717 var spoiler_body = $('.wpf-spoiler-body', spoiler_wrap); 718 if (spoiler_body.length) { 719 var spoiler_chevron = $('.wpf-spoiler-chevron', spoiler_wrap); 720 $(spoiler_chevron[0]).toggleClass('fa-chevron-down fa-chevron-up'); 721 $(spoiler_body[0]).slideToggle(500, function () { 722 spoiler_wrap.removeClass('wpf-spoiler-processing'); 723 if (!spoiler_wrap.hasClass('wpf-spoiler-open')) { 724 $('.wpf-spoiler-wrap.wpf-spoiler-open .wpf-spoiler-head', spoiler_wrap).trigger('click'); 725 } 726 }); 727 } 728 } 729 } 730 }); 731 732 wpforo_wrap.on('click', '#add_wpftopic:not(.not_reg_user)', function () { 733 var form = $('.wpf-topic-create'); 734 $(this).attr('disabled', 'disabled'); 735 form.slideToggle('slow', () => { 736 var stat = form.is(':hidden'); 737 if (!window.add_wpftopic_phrase) window.add_wpftopic_phrase = $(this).html(); 738 var add_wpftopic = (stat ? window.add_wpftopic_phrase : '<i class="fas fa-times" aria-hidden="true"></i>'); 739 $(this).html(add_wpftopic); 740 wpforo_editor.set_content(''); 741 $('[name="thread[title]"]').trigger('focus'); 742 $('html').scrollTop(($(this).offset().top - 35)); 743 $(this).removeAttr('disabled'); 744 }); 745 }); 746 747 wpforo_wrap.on('click', '.wpf-answer-button .wpf-button:not(.not_reg_user)', function () { 748 $(this).closest('.wpf-bottom-bar').hide(); 749 }); 750 751 wpforo_wrap.on('click', '.wpforo-section .add_wpftopic:not(.not_reg_user)', function () { 752 var wrap = $(this).parents('div.wpforo-section'); 753 var form_wrap = $('.wpf-topic-form-extra-wrap', wrap); 754 755 var is_just_one_forum = form_wrap.data('is_just_one_forum'); 756 if (!is_just_one_forum) $('.wpf-topic-form-ajax-wrap').empty(); 757 var stat = form_wrap.is(':hidden'); 758 $('.wpf-topic-form-extra-wrap').slideUp('slow'); 759 var add_wpftopic; 760 if (stat) { 761 add_wpftopic = '<i class="fas fa-times" aria-hidden="true"></i>'; 762 form_wrap.slideDown('slow'); 763 } else { 764 add_wpftopic = '<i class="fas fa-feather-alt" aria-hidden="true"></i>' + $(this).data('phrase'); 765 form_wrap.slideUp('slow'); 766 } 767 if (!is_just_one_forum) { 768 var option_no_selected = $('option.wpf-topic-form-no-selected-forum'); 769 option_no_selected.show(); 770 option_no_selected.prop('selected', true); 771 } 772 $(this).html(add_wpftopic); 773 $('html').scrollTop((wrap.offset().top - 30)); 774 }); 775 776 wpforo_wrap.on('click', '.not_reg_user', function () { 777 wpforo_load_show(); 778 wpforo_notice_show(wpforo.notice.login_or_register); 779 wpforo_load_hide(); 780 }); 781 782 $(document).on('click', '#wpf-msg-box p', function () { 783 $(this).remove(); 784 }); 785 786 /* Home page loyouts toipcs toglle */ 787 wpforo_wrap.on('click', '.topictoggle', function () { 788 wpforo_load_show(); 789 790 var id = $(this).attr('id'); 791 792 id = id.replace('img-arrow-', ''); 793 $('.wpforo-last-topics-' + id).slideToggle('slow'); 794 if ($(this).hasClass('topictoggle') && $(this).hasClass('fa-chevron-down')) { 795 $('#img-arrow-' + id).removeClass('fa-chevron-down').addClass('fa-chevron-up'); 796 } else { 797 $('#img-arrow-' + id).removeClass('fa-chevron-up').addClass('fa-chevron-down'); 798 } 799 800 id = id.replace('button-arrow-', ''); 801 $('.wpforo-last-posts-' + id).slideToggle('slow'); 802 if ($(this).hasClass('topictoggle') && $(this).hasClass('wpfcl-a') && $(this).hasClass('fa-chevron-down')) { 803 $('#button-arrow-' + id).removeClass('fa-chevron-down').addClass('fa-chevron-up'); 804 } else { 805 $('#button-arrow-' + id).removeClass('fa-chevron-up').addClass('fa-chevron-down'); 806 } 807 808 wpforo_load_hide(); 809 }); 810 811 /* Home page loyouts toipcs toglle */ 812 wpforo_wrap.on('click', '.wpforo-membertoggle', function () { 813 var id = $(this).attr('id'); 814 id = id.replace('wpforo-memberinfo-toggle-', ''); 815 $('#wpforo-memberinfo-' + id).slideToggle('slow'); 816 if ($(this).find('i').hasClass('fa-caret-down')) { 817 $(this).find('i').removeClass('fa-caret-down').addClass('fa-caret-up'); 818 } else { 819 $(this).find('i').removeClass('fa-caret-up').addClass('fa-caret-down'); 820 } 821 }); 822 823 /* Threaded Layout Hide Replies */ 824 wpforo_wrap.on('click', '.wpf-post-replies-bar', function () { 825 var id = $(this).attr('id'); 826 id = id.replace('wpf-ttgg-', ''); 827 $('#wpf-post-replies-' + id).slideToggle('slow'); 828 if ($(this).find('i').hasClass('fa-angle-down')) { 829 $(this).find('i').removeClass('fa-angle-down').addClass('fa-angle-up'); 830 $(this).find('.wpforo-ttgg').attr('wpf-tooltip', wpforo_phrase('Hide Replies')); 831 } else { 832 $(this).find('i').removeClass('fa-angle-up').addClass('fa-angle-down'); 833 $(this).find('.wpforo-ttgg').attr('wpf-tooltip', wpforo_phrase('Show Replies')); 834 } 835 }); 836 837 //Reply 838 wpforo_wrap.on('click', '.wpforo-reply:not(.wpforo_layout_4)', function () { 839 wpforo_load_show(); 840 841 var main_form = $('form.wpforo-main-form[data-textareaid]'); 842 var wrap = main_form.closest('.wpf-form-wrapper'); 843 wrap.show(); 844 845 $('.wpf-reply-form-title').html(wpforo_phrase('Leave a reply')); 846 847 var post_wrap = $(this).closest('[id^=post-][data-postid]'); 848 var parentpostid = post_wrap.data('postid'); 849 if (!parentpostid) parentpostid = 0; 850 $('.wpf-form-post-parentid', main_form).val(parentpostid); 851 852 var userid = parseInt(post_wrap.data('userid')); 853 var mention = post_wrap.data('mention'); 854 var isowner = parseInt(post_wrap.data('isowner')); 855 var content = (!isowner && userid && mention ? '@' + mention + (wpforo_editor.is_tinymce(wpforo_editor.get_main()) ? ' ' : ' ') : ''); 856 857 wpforo_editor.set_content(content, wpforo_editor.get_main()); 858 859 $('html').scrollTop(wrap.offset().top); 860 861 wpforo_load_hide(); 862 863 }); 864 865 //Answer 866 wpforo_wrap.on('click', '.wpforo-answer', function () { 867 wpforo_load_show(); 868 869 var main_form = $('form.wpforo-main-form[data-textareaid]'); 870 var wrap = main_form.closest('.wpf-form-wrapper'); 871 wrap.show(); 872 873 $('.wpf-reply-form-title').html(wpforo_phrase('Your answer')); 874 875 $('.wpf-form-postid', main_form).val(0); 876 $('.wpf-form-post-parentid', main_form).val(0); 877 878 var post_wrap = $(this).closest('[id^=post-][data-postid]'); 879 880 var userid = parseInt(post_wrap.data('userid')); 881 var mention = post_wrap.data('mention'); 882 var isowner = parseInt(post_wrap.data('isowner')); 883 var content = (!isowner && userid && mention ? '@' + mention + (wpforo_editor.is_tinymce(wpforo_editor.get_main()) ? ' ' : ' ') : ''); 884 885 wpforo_editor.set_content(content, wpforo_editor.get_main()); 886 887 $('html').scrollTop(wrap.offset().top); 888 889 wpforo_load_hide(); 890 891 }); 892 893 wpforo_wrap.on('click', '.wpforo-qa-comment, .wpforo-reply.wpf-action.wpforo_layout_4', function () { 894 var wrap = $(this).parents('.reply-wrap,.wpforo-qa-item-wrap'); 895 var post_wrap = $('.post-wrap', wrap); 896 if (!post_wrap.length) post_wrap = wrap; 897 var parentid = post_wrap.data('postid'); 898 if (!parentid) parentid = post_wrap.attr('id').replace('post-', ''); 899 if (!parentid) parentid = 0; 900 var form = $('.wpforo-post-form'); 901 var textareaid = form.data('textareaid'); 902 var textarea_wrap = $('.wpf_post_form_textarea_wrap', form); 903 var textarea = $('#' + textareaid, textarea_wrap); 904 var textareatype = textarea_wrap.data('textareatype'); 905 $('.wpf_post_parentid').val(parentid); 906 $('.wpforo-qa-comment,.wpforo-reply.wpf-action.wpforo_layout_4').show(); 907 $(this).hide(); 908 $('.wpforo-portable-form-wrap', wrap).show(); 909 if (!$('.wpforo-post-form', wrap).length) form.appendTo($('.wpforo-portable-form-wrap', wrap)); 910 911 form.show(); 912 if (textareatype && textareatype === 'rich_editor') { 913 textarea_wrap.html('<textarea id="' + textareaid + '" class="wpf_post_body" name="post[body]"></textarea>'); 914 wpforo_tinymce_initializeIt('#' + textareaid); 915 } else { 916 textarea.val(''); 917 textarea.trigger('focus'); 918 } 919 920 var userid = parseInt(post_wrap.data('userid')); 921 var mention = post_wrap.data('mention'); 922 var isowner = parseInt(post_wrap.data('isowner')); 923 var content = (!isowner && userid && mention ? '@' + mention + (wpforo_editor.is_tinymce(textareaid) ? ' ' : ' ') : ''); 924 925 wpforo_editor.set_content(content, textareaid); 926 }); 927 928 wpforo_wrap.on('click', '.wpf-button-close-form', function () { 929 $(this).parents('.wpforo-portable-form-wrap').hide(); 930 $('.wpforo-post-form').hide(); 931 $('.wpforo-qa-comment,.wpforo-reply.wpf-action.wpforo_layout_4').show(); 932 wpforo_editor.set_content(''); 933 }); 934 935 //mobile menu responsive toggle 936 wpforo_wrap.on('click', '#wpforo-menu .wpf-res-menu', function () { 937 $('#wpforo-menu .wpf-menu').toggle(); 938 }); 939 var wpfwin = $(window).width(); 940 var wpfwrap = wpforo_wrap.width(); 941 if (wpfwin >= 602 && wpfwrap < 800) { 942 wpforo_wrap.on('focus', '#wpforo-menu .wpf-search-field', function () { 943 $('#wpforo-menu .wpf-menu li').hide(); 944 wpforo_wrap.find('#wpforo-menu .wpf-res-menu').show(); 945 $('#wpforo-menu .wpf-search-field').css('transition-duration', '0s'); 946 }); 947 wpforo_wrap.on('blur', '#wpforo-menu .wpf-search-field', function () { 948 wpforo_wrap.find('#wpforo-menu .wpf-res-menu').hide(); 949 $('#wpforo-menu .wpf-menu li').show(); 950 $('#wpforo-menu .wpf-search-field').css('transition-duration', '0.4s'); 951 }); 952 } 953 954 // password show/hide switcher */ 955 wpforo_wrap.on('click', '.wpf-show-password', function () { 956 var btn = $(this); 957 var parent = btn.parents('.wpf-field-wrap'); 958 var input = $(':input', parent); 959 if (input.attr('type') === 'password') { 960 input.attr('type', 'text');
961 btn.removeClass('fa-eye-slash'); 962 btn.addClass('fa-eye'); 963 } else { 964 input.attr('type', 'password'); 965 btn.removeClass('fa-eye'); 966 btn.addClass('fa-eye-slash'); 967 } 968 }); 969 970 //Turn off on dev mode 971 //$(window).on('resize', function(){ if (window.RT) { clearTimeout(window.RT); } window.RT = setTimeout(function(){ this.location.reload(false);}, 100); }); 972 973 wpforo_wrap.on('change', '#wpforo_split_form #wpf_split_create_new', function () { 974 var checked = $('#wpf_split_create_new').is(':checked'), 975 target_url = $('#wpf_split_target_url'), 976 append = $('#wpf_split_append'), 977 new_title = $('#wpf_split_new_title'), 978 forumid = $('#wpf_split_forumid'); 979 if (checked) { 980 target_url.children('input').prop('disabled', true); 981 target_url.hide(); 982 append.children('input').prop('disabled', true); 983 append.hide(); 984 new_title.children('input').prop('disabled', false); 985 new_title.show(); 986 forumid.children('select').prop('disabled', false); 987 forumid.show(); 988 } else { 989 target_url.children('input').prop('disabled', false); 990 target_url.show(); 991 append.children('input').prop('disabled', false); 992 append.show(); 993 new_title.children('input').prop('disabled', true); 994 new_title.hide(); 995 forumid.children('select').prop('disabled', true); 996 forumid.hide(); 997 } 998 }); 999 1000 //Facebook Share Buttons 1001 wpforo_wrap.on('click', '.wpf-fb', function () { 1002 var item_url = $(this).data('wpfurl'); 1003 var item_quote = $(this).parents('.post-wrap').find('.wpforo-post-content').text(); 1004 FB.ui({ 1005 method: 'share', 1006 href: item_url, 1007 quote: item_quote, 1008 hashtag: null, 1009 }, function (response) {}); 1010 }); 1011 //Share Buttons Toggle 1012 wpforo_wrap.on('mouseover', '.wpf-sb', function () { 1013 $(this).find('.wpf-sb-toggle').find('i').addClass('wpfsa'); 1014 $(this).find('.wpf-sb-buttons').show(); 1015 }); 1016 wpforo_wrap.on('mouseout', '.wpf-sb', function () { 1017 $(this).find('.wpf-sb-toggle').find('i').removeClass('wpfsa'); 1018 $(this).find('.wpf-sb-buttons').hide(); 1019 }); 1020 wpforo_wrap.on('mouseover', '.wpf-sb-toggle', function () { 1021 $(this).next().filter('.wpf-sb-buttons').parent().find('i').addClass('wpfsa'); 1022 }); 1023 wpforo_wrap.on('mouseout', '.wpf-sb-toggle', function () { 1024 $(this).next().filter('.wpf-sb-buttons').parent().find('i').removeClass('wpfsa'); 1025 }); 1026 1027 //Forum Rules 1028 wpforo_wrap.on('click', '#wpf-open-rules', function () { 1029 $('.wpforo-legal-rules').toggle(); 1030 return false; 1031 }); 1032 wpforo_wrap.on('click', '#wpflegal-rules-yes', function () { 1033 $('#wpflegal_rules').prop('checked', true); 1034 $('#wpflegal-rules-not').removeClass('wpflb-active-not'); 1035 $(this).addClass('wpflb-active-yes'); 1036 setTimeout(function () { $('.wpforo-legal-rules').slideToggle('slow'); }, 500); 1037 }); 1038 wpforo_wrap.on('click', '#wpflegal-rules-not', function () { 1039 $('#wpflegal_rules').prop('checked', false); 1040 $('#wpflegal-rules-yes').removeClass('wpflb-active-yes'); 1041 $(this).addClass('wpflb-active-not'); 1042 }); 1043 1044 //Forum Privacy Buttons 1045 wpforo_wrap.on('click', '#wpf-open-privacy', function () { 1046 $('.wpforo-legal-privacy').toggle(); 1047 return false; 1048 }); 1049 wpforo_wrap.on('click', '#wpflegal-privacy-yes', function () { 1050 $('#wpflegal_privacy').prop('checked', true); 1051 $('#wpflegal-privacy-not').removeClass('wpflb-active-not'); 1052 $(this).addClass('wpflb-active-yes'); 1053 setTimeout(function () { $('.wpforo-legal-privacy').slideToggle('slow'); }, 500); 1054 }); 1055 wpforo_wrap.on('click', '#wpflegal-privacy-not', function () { 1056 $('#wpflegal_privacy').prop('checked', false); 1057 $('#wpflegal-privacy-yes').removeClass('wpflb-active-yes'); 1058 $(this).addClass('wpflb-active-not'); 1059 }); 1060 1061 //Facebook Login Button 1062 wpforo_wrap.on('click', '#wpflegal_fblogin', function () { 1063 if ($(this).is(':checked')) { 1064 $('.wpforo_fb-button').attr('style', 'pointer-events:auto; opacity:1;'); 1065 } else { 1066 $('.wpforo_fb-button').attr('style', 'pointer-events: none; opacity:0.6;'); 1067 } 1068 }); 1069 1070 wpforo_wrap.on('click', '.wpf-load-threads .wpf-forums', function () { 1071 $('.wpf-cat-forums', $(this).parents('div.wpfl-4')).slideToggle('slow'); 1072 $('i', $(this)).toggleClass('fa-chevron-down fa-chevron-up'); 1073 }); 1074 1075 wpforo_wrap.on('click', '[data-copy-wpf-furl], [data-copy-wpf-shurl]', function () { 1076 var urls = []; 1077 var full_url = $(this).data('copy-wpf-furl'); 1078 if (full_url) urls.push(decodeURIComponent(full_url)); 1079 var short_url = $(this).data('copy-wpf-shurl'); 1080 if (short_url) urls.push(decodeURIComponent(short_url)); 1081 if (urls.length) { 1082 var label = ''; 1083 var html = '';
1084 urls.forEach(function (url, i) { 1085 label = (urls.length === 2 && i === 1) ? wpforo_phrase('Short') : wpforo_phrase('Full'); 1086 html += '<div class="wpforo-copy-url-wrap">' + 1087 '<div class="wpforo-copy-input">' + 1088 '<div class="wpforo-copy-input-header">' + 1089 '<label class="wpforo-copy-url-label">' + 1090 '<i class="fas fa-link wpfsx"></i>' + 1091 '<span class="wpforo-copy-url-label-txt">' + label + '</span>' + 1092 '</label>' + 1093 '</div>' + 1094 '<div class="wpforo-copy-input-body">' + 1095 '<input dir="ltr" readonly class="wpforo-copy-url" type="text" value="' + url + '">' + 1096 '</div>' + 1097 '</div>' + 1098 '<div class="wpforo-copied-txt"><span>' + wpforo_phrase('Copied') + '</span></div>' + 1099 '</div>'; 1100 }); 1101 var title = wpforo_phrase('Share Urls'); 1102 wpforo_dialog_show(title, html, '40%', '260px'); 1103 } 1104 }); 1105 1106 $(document).on('click', '.wpforo-copy-url-wrap', function () { 1107 var wrap = $(this); 1108 var input = $('input.wpforo-copy-url', wrap); 1109 if (input.length) { 1110 input[0].select(); 1111 if (document.execCommand('copy')) { 1112 wrap.addClass('wpforo-copy-animate'); 1113 setTimeout(function () { 1114 wrap.removeClass('wpforo-copy-animate'); 1115 }, 1000); 1116 } 1117 } 1118 }); 1119 1120 wpforo_wrap.on('click', '.wpf-toggle .wpf-toggle-advanced', function () { 1121 var wrap = $(this).closest('.wpf-toggle-wrap'); 1122 $('.wpf-ico', $(this)).toggleClass('fa-chevron-down fa-chevron-up'); 1123 $('.wpf-search-advanced-fields', wrap).slideToggle(350); 1124 }); 1125 1126 wpforo_wrap.on('click', '.wpf-toggle .wpf-toggle-custom', function () { 1127 var wrap = $(this).closest('.wpf-toggle-wrap'); 1128 $('.wpf-ico', $(this)).toggleClass('fa-chevron-down fa-chevron-up'); 1129 $('.wpf-search-custom-fields', wrap).slideToggle(350); 1130 }); 1131 1132 wpforo_wrap.on('click', 'form[data-textareaid] .wpforo-delete-custom-file', function () { 1133 if (confirm(wpforo_phrase('Are you sure you want to delete this file?'))) { 1134 var wrap = $(this).closest('.wpf-field-file-wrap'); 1135 var fieldKey = $(this).data('fieldkey'); 1136 if (fieldKey) wrap.html('<input type="hidden" name="wpftcf_delete[]" value="' + fieldKey + '">'); 1137 } 1138 }); 1139 1140 wpforo_wrap.on('change', '#wpf-profile-action', function () { 1141 var val = $(this).val(); 1142 var exreg = new RegExp('^https?://', 'i'); 1143 if (val.match(exreg)) location.href = val; 1144 }); 1145}); 1146 1147/** 1148 * 1149 * @param { jQuery|Element|Document } element 1150 * @param { string } name 1151 * @param { string|number|integer } value 1152 */ 1153function wpforo_setAttr (element, name, value) { 1154 if (element instanceof $wpf || element instanceof Element) { 1155 element = $wpf(element); 1156 element.prop(name, value); 1157 element.attr(name, value); 1158 1159 var match = name.match(new RegExp('^data-(.+)', 'i')); 1160 if (match) element.data(match[1], value); 1161 } 1162} 1163 1164function wpforo_confirm (msg, e) { 1165 if (!confirm(msg)) { 1166 e = e || window.event; 1167 e.stopPropagation(); 1168 e.preventDefault(); 1169 return false; 1170 } 1171 1172 return true; 1173} 1174 1175class wpfMultiInput extends HTMLElement { 1176 constructor () { 1177 super(); 1178 this.innerHTML += `<style> 1179 wpf-multi-input input::-webkit-calendar-picker-indicator, 1180 wpf-multi-input datalist{ 1181 display: none !important; 1182 } 1183 #wpforo #wpforo-wrap wpf-multi-input .wpf-invalid-msg{ 1184 display: none; 1185 color: red; 1186 font-weight: bold; 1187 } 1188 #wpforo #wpforo-wrap .wpf-multi-item > .wpf-multi-item-action{ 1189 margin: 0 3px; 1190 cursor: pointer; 1191 } 1192 #wpforo #wpforo-wrap .wpf-multi-item > .wpf-multi-item-action:hover > i{ 1193 color: black; 1194 transform: scale(1.2); 1195 } 1196 #wpforo #wpforo-wrap .wpf-multi-item.wpf-multi-item-known-1 > .wpf-save-variant{ 1197 display: none !important; 1198 } 1199 </style>`; 1200 this._shadowRoot = this.attachShadow({ mode: 'open' }); 1201 this._shadowRoot.innerHTML = `<style> 1202 /* NB use of pointer-events to only allow events from the à icon */ 1203 ::slotted(div.wpf-multi-item) { 1204 background-color: var(--wpf-multi-input-item-bg-color, #dedede) !important; 1205 border: var(--wpf-multi-input-item-border, 1px solid #ccc) !important; 1206 border-radius: 2px !important; 1207 color: #222 !important; 1208 display: inline-block !important; 1209 font-size: var(--wpf-multi-input-item-font-size, 14px) !important; 1210 margin: 5px !important; 1211 padding: 2px 5px 2px 5px !important; 1212 position: relative !important; 1213 top: -1px !important; 1214 } 1215 ::slotted(div.wpf-multi-item:hover) { 1216 background-color: #eee !important; 1217 color: black !important; 1218 } 1219 ::slotted(div.wpf-multi-item > .wpf-multi-item-action){ 1220 margin: 0 3px; 1221 cursor: pointer; 1222 } 1223 ::slotted(.wpf-multi-item.wpf-multi-item-known-1 > .wpf-save-variant){ 1224 display: none !important; 1225 } 1226 ::slotted(div.wpf-multi-item > .wpf-multi-item-action:hover > i){ 1227 color: black; 1228 transform: scale(1.2); 1229 } 1230 ::slotted(input){ 1231 border: 2px solid transparent !important; 1232 } 1233 ::slotted(input.wpf-invalid){ 1234 border-color: red !important; 1235 } 1236 </style><slot></slot>`; 1237 1238 this._datalist = this.querySelector('datalist');
1239 this._dbVariants = []; 1240 const dbvariants = this.dataset['dbvariants']; 1241 if (dbvariants) this._dbVariants = JSON.parse(dbvariants); 1242 1243 this._input = this.querySelector('input'); 1244 this._input.onblur = this._handleBlur.bind(this); 1245 this._input.oninput = this._handleInput.bind(this); 1246 this._input.onkeyup = (event) => { 1247 this._handleKeyup(event); 1248 }; 1249 this._input.form.onsubmit = (event) => { 1250 this._handleSubmit(event); 1251 }; 1252 this._items = [...this.querySelectorAll('.wpf-multi-item')]; 1253 this._items.forEach((item) => { 1254 item.querySelector('.wpf-del').onclick = () => { 1255 this._deleteItem(item); 1256 }; 1257 const save_button = item.querySelector('.wpf-save-variant'); 1258 if (save_button) { 1259 save_button.onclick = () => { 1260 this._saveItem(item); 1261 }; 1262 } 1263 }); 1264 1265 this._allowDuplicates = this.hasAttribute('allow-duplicates') || this._input.hasAttribute('allow-duplicates'); 1266 this._allowCustomValues = this.hasAttribute('allow-custom-values') || this._input.hasAttribute('allow-custom-values'); 1267 this._isRequired = this.hasAttribute('required') || this._input.hasAttribute('required'); 1268 this._input.removeAttribute('required'); 1269 1270 this._invalidMsg = this.querySelector('.wpf-invalid-msg'); 1271 } 1272 1273 // Called by _handleKeydown() when the value of the input is an allowed value. 1274 _addItem (value) { 1275 if (!value.trim()) return; 1276 if (!this._allowCustomValues && !this._dbVariants.includes(value)) return; 1277 if (!this._allowDuplicates && this.getValues().includes(value)) return; 1278 this._input.value = ''; 1279 const item = document.querySelector('template#wpf-multi-item-template').cloneNode(true).content.firstChild; 1280 item.classList.remove('wpf-multi-item-known-0', 'wpf-multi-item-known-1'); 1281 item.classList.add((this._dbVariants.includes(value) ? 'wpf-multi-item-known-1' : 'wpf-multi-item-known-0')); 1282 item.querySelector('.wpf-multi-item-value').textContent = value; 1283 item.querySelector('.wpf-del').onclick = () => { 1284 this._deleteItem(item); 1285 }; 1286 const save_button = item.querySelector('.wpf-save-variant'); 1287 if (save_button) { 1288 save_button.onclick = () => { 1289 this._saveItem(item); 1290 }; 1291 } 1292 this.insertBefore(item, this._input); 1293 1294 // Remove value from datalist options and from _allowedValues array. 1295 // Value is added back if an item is deleted (see _deleteItem()). 1296 if (!this._allowDuplicates && this._dbVariants.includes(value)) { 1297 for (const option of this._datalist.options) { 1298 if (option.value === value) { 1299 option.remove(); 1300 } 1301 } 1302 } 1303 } 1304 1305 // Called when the à icon is tapped/clicked or 1306 // by _handleKeydown() when Backspace is entered. 1307 _deleteItem (item) { 1308 if (!item.classList.contains('wpf-multi-item')) return; 1309 const value = item.querySelector('.wpf-multi-item-value').textContent; 1310 item.remove(); 1311 // If duplicates aren't allowed, value is removed (in _addItem()) 1312 // as a datalist option and from the _allowedValues array. 1313 // So â need to add it back here. 1314 if (!this._allowDuplicates && this._dbVariants.includes(value)) { 1315 const option = document.createElement('option'); 1316 option.value = value; 1317 // Insert as first option seems reasonable... 1318 this._datalist.insertBefore(option, this._datalist.firstChild); 1319 } 1320 } 1321 1322 _saveItem (item) { 1323 if (!item.classList.contains('wpf-multi-item')) return; 1324 const value = item.querySelector('.wpf-multi-item-value').textContent; 1325 1326 if (typeof value === 'string') { 1327 if (item.parentNode instanceof Node && item.parentNode.dataset && typeof item.parentNode.dataset['fieldkey'] === 'string') { 1328 this._ajaxSaveItem(+item.parentNode.dataset['formid'], item.parentNode.dataset['fieldkey'].trim(), value, () => { 1329 item.classList.remove('wpf-multi-item-known-0'); 1330 item.classList.add('wpf-multi-item-known-1'); 1331 }); 1332 } 1333 } 1334 } 1335 1336 _ajaxSaveItem (formid, fieldKey, value, resolve, reject) { 1337 if (!value.trim()) return; 1338 wpforo_load_show('Saving...'); 1339 1340 $wpf.ajax({ 1341 type: 'POST', 1342 data: { 1343 formid, 1344 fieldKey, 1345 value, 1346 action: 'wpforotcf_append_field_values', 1347 _wpfnonce: wpforo['nonces']['wpforotcf_append_field_values'], 1348 }, 1349 }).done(function (r) { 1350 if (r.success) { 1351 if (resolve) resolve(); 1352 } else { 1353 if (reject) reject(); 1354 } 1355 }).always(function () { 1356 wpforo_load_hide(); 1357 }); 1358 } 1359 1360 // Avoid stray text remaining in the input element that's not in a div.item. 1361 _handleBlur () { 1362 this._input.value = ''; 1363 } 1364 1365 // Called when input text changes, 1366 // either by entering text or selecting a datalist option. 1367 _handleInput () { 1368 // Add a div.item, but only if the current value 1369 // of the input is an allowed value
1370 const match = this._dbVariants.find(v => v.toLowerCase() === this._input.value.toLowerCase()); 1371 if (match !== undefined) this._addItem(match); 1372 } 1373 1374 // Called when text is entered or keys pressed in the input element. 1375 _handleKeyup (event) { 1376 const itemToDelete = event.target.previousElementSibling; 1377 const value = this._input.value; 1378 1379 // On Backspace, delete the div.item to the left of the input 1380 if (value === '' && event.key === 'Backspace' && itemToDelete) { 1381 this._deleteItem(itemToDelete); 1382 // Add a div.item, but only if the current value 1383 // of the input is an allowed value 1384 } else if (event.key === 'Enter' && value.trim()) { 1385 if (this._allowCustomValues) { 1386 event.preventDefault(); 1387 this._addItem(value); 1388 } else if (this._dbVariants.includes(value) && !this.getValues().includes(value)) { 1389 event.preventDefault(); 1390 this._addItem(value); 1391 } 1392 } 1393 } 1394 1395 _handleSubmit (event) { 1396 if (this._input.value) { 1397 event.preventDefault(); 1398 setTimeout(wpforo_load_hide, 100); 1399 if (this._allowCustomValues) this._addItem(this._input.value); 1400 this._input.value = ''; 1401 return; 1402 } 1403 1404 const values = this.getValues(); 1405 if (this._isRequired && !values.length) { 1406 event.preventDefault(); 1407 this._input.classList.add('wpf-invalid'); 1408 this._invalidMsg.style = 'display: inline-block'; 1409 let i = 0; 1410 const intervalHendler = setInterval(() => { 1411 this._input.classList.toggle('wpf-invalid'); 1412 if (i++ >= 4) { 1413 this._input.classList.add('wpf-invalid'); 1414 wpforo_load_hide(); 1415 clearInterval(intervalHendler); 1416 setTimeout(() => { 1417 this._input.classList.remove('wpf-invalid'); 1418 this._invalidMsg.style = 'display: none;'; 1419 }, 3000); 1420 } 1421 }, 50); 1422 } else if (values.length) { 1423 const name = this._input.name; 1424 this._input.removeAttribute('name'); 1425 values.forEach((val) => { 1426 const hidden = document.createElement('input'); 1427 hidden.type = 'hidden'; 1428 hidden.name = name; 1429 hidden.value = val; 1430 this._input.form.append(hidden); 1431 }); 1432 } 1433 } 1434 1435 // Public method for getting item values as an array. 1436 getValues () { 1437 const values = []; 1438 const items = this.querySelectorAll('.wpf-multi-item'); 1439 for (const item of items) { 1440 values.push(item.querySelector('.wpf-multi-item-value').textContent); 1441 } 1442 return values; 1443 } 1444} 1445 1446window.customElements.define('wpf-multi-input', wpfMultiInput); 1447 1448class wpfAutocompleteInput extends HTMLElement { 1449 constructor () { 1450 super(); 1451 this.innerHTML += `<style> 1452 wpf-autocomplete-input input::-webkit-calendar-picker-indicator, 1453 wpf-autocomplete-input datalist{ 1454 display: none !important; 1455 } 1456 #wpforo #wpforo-wrap wpf-autocomplete-input .wpf-invalid-msg{ 1457 display: none; 1458 color: red; 1459 font-weight: bold; 1460 } 1461 </style>`; 1462 this._shadowRoot = this.attachShadow({ mode: 'open' }); 1463 this._shadowRoot.innerHTML = `<style> 1464 ::slotted(input){ 1465 border: 2px solid transparent !important; 1466 } 1467 ::slotted(input.wpf-invalid){ 1468 border-color: red !important; 1469 } 1470 </style><slot></slot>`; 1471 this._datalist = this.querySelector('datalist'); 1472 this._dbVariants = []; 1473 const dbvariants = this.dataset['dbvariants']; 1474 if (dbvariants) this._dbVariants = JSON.parse(dbvariants); 1475 1476 this._input = this.querySelector('input'); 1477 this._input.form.onsubmit = (event) => { 1478 this._handleSubmit(event); 1479 }; 1480 1481 this._allowCustomValues = this.hasAttribute('allow-custom-values') || this._input.hasAttribute('allow-custom-values'); 1482 this._isRequired = this.hasAttribute('required') || this._input.hasAttribute('required'); 1483 this._invalidMsgRequired = this.querySelector('.wpf-invalid-msg.wpf-required'); 1484 this._invalidMsgNotAllowedValue = this.querySelector('.wpf-invalid-msg.wpf-not-allowed-value'); 1485 } 1486 1487 _handleSubmit (event) { 1488 const value = this._input.value; 1489 if (!this._isRequired && !value) return; 1490 let prevent = false; 1491 if (this._isRequired && !value) { 1492 this._invalidMsg = this._invalidMsgRequired; 1493 prevent = true; 1494 }
1494 else if (!this._allowCustomValues && !this._dbVariants.includes(value)) { 1495 this._invalidMsg = this._invalidMsgNotAllowedValue; 1496 prevent = true; 1497 } 1498 if (prevent) { 1499 event.preventDefault(); 1500 wpforo_load_hide(); 1501 this._input.classList.add('wpf-invalid'); 1502 this._invalidMsg.style = 'display: inline-block'; 1503 let i = 0; 1504 const intervalHendler = setInterval(() => { 1505 this._input.classList.toggle('wpf-invalid'); 1506 if (i++ >= 4) { 1507 this._input.classList.add('wpf-invalid'); 1508 wpforo_load_hide(); 1509 clearInterval(intervalHendler); 1510 setTimeout(() => { 1511 this._input.classList.remove('wpf-invalid'); 1512 this._invalidMsg.style = 'display: none;'; 1513 }, 3000); 1514 } 1515 }, 50); 1516 } 1517 } 1518} 1519 1520window.customElements.define('wpf-autocomplete-input', wpfAutocompleteInput); 1521 1522
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.