Difference between revisions of "MediaWiki:Common.js"

Jump to: navigation, search

Difference between revisions of "MediaWiki:Common.js"

 
(97 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
+
// Terms Modal
 
 
 
$(document).ready(function() {
 
$(document).ready(function() {
 
 
(function ($, mw) {
 
"use strict";
 
 
var $toolbar, queue = [];
 
 
function insertButton(btnObj) {
 
$toolbar.wikiEditor('addToToolbar', btnObj);
 
}
 
 
function handleQueue() {
 
for (var i = 0; i < queue.length; i++) {
 
insertButton(queue[i]);
 
}
 
queue = [];
 
}
 
 
function check() {
 
if ($.wikiEditor && $.fn.wikiEditor && $('#wpTextbox1').length) {
 
$toolbar = $('#wpTextbox1');
 
handleQueue();
 
}
 
}
 
 
// Only on editpage
 
if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit', 'formedit']) !== -1) {
 
/**
 
* krInsertWikiEditorButton
 
*
 
* @param options {Object} An object with options:
 
* - section {String} (optional) The name of the section in the WikiEditor. Defaults to 'main'
 
* - group {String} (optional) The name of the group in the WikiEditor. Defaults to 'insert'
 
* - id {String} (required) Unique id (ie. 'my-button')
 
* - icon {String} (recommended) URL to the icon, should be square about 21 to 22px
 
* - label {String} (required) Tooltip displayed when hovering button
 
* - insertBefore {String} (optional) Wikitext to be inserted before the cursor on-click
 
* - sampleText {String} (optional) Text inserted in place of the cursor if no text was selected
 
* - insertAfter {String} (optional) Wikitext to be inserted after the cursor on-click
 
* - callback {Function} (optional) Called when the button is clicked
 
* - autoSummary {mixed} (optional) Null or an Object with the following properties:
 
*  - summary {String} (required) Edit summary that should be used
 
*  - position {String} (optional) 'append', 'prepend' or 'replace'
 
*  - delimiter {String} (optional) delimiter between the (possibly) current summary and the to-be-inserted summary
 
*/
 
window.krInsertWikiEditorButton = function (options) {
 
// Defaults
 
options = $.extend({
 
'section': 'main',
 
'group': 'insert',
 
'id': null,
 
'icon': '//upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Toolbaricon_bold_!.png/21px-Toolbaricon_bold_!.png',
 
'label': '',
 
'insertBefore': '',
 
'sampleText': '',
 
'insertAfter': '',
 
'callback': null,
 
'autoSummary': {
 
'summary': null,
 
'position': 'append',
 
'delimiter': '; '
 
}
 
}, options);
 
// Required
 
if (!options.id || !options.label) {
 
return false;
 
}
 
var btnObj = {
 
'section': options.section,
 
'group': options.group,
 
'tools': {}
 
};
 
btnObj.tools[options.id] = {
 
label: options.label,
 
type: 'button',
 
icon: options.icon,
 
action: {
 
type: 'callback',
 
execute: function () {
 
// encapsulateSelection
 
$toolbar.textSelection('encapsulateSelection', {
 
pre: options.insertBefore,
 
peri: options.sampleText,
 
post: options.insertAfter
 
});
 
// Auto summary
 
if (options.autoSummary && options.autoSummary.summary) {
 
var $summary = $('#wpSummary'), currentSum = $summary.val();
 
if (!$.trim(currentSum)) {
 
$summary.val(options.autoSummary.summary);
 
} else {
 
switch (options.autoSummary.position) {
 
case 'prepend':
 
$summary.val(
 
options.autoSummary.summary +
 
options.autoSummary.delimiter +
 
currentSum
 
);
 
break;
 
case 'replace':
 
$summary.val(options.autoSummary.summary);
 
break;
 
default: // 'append'
 
$summary.val(
 
currentSum +
 
options.autoSummary.delimiter +
 
options.autoSummary.summary
 
);
 
}
 
}
 
}
 
// Callback
 
if ($.isFunction(options.callback)) {
 
options.callback();
 
}
 
}
 
}
 
};
 
if ($toolbar) {
 
insertButton(btnObj);
 
} else {
 
queue.push(btnObj);
 
}
 
};
 
$.when(
 
mw.loader.using( 'ext.wikiEditor.toolbar' ),
 
$.ready
 
).then(check);
 
} else {
 
// No-op function to avoid errors on other pages
 
window.krInsertWikiEditorButton = function () {};
 
}
 
}(jQuery, mediaWiki));
 
  
 +
  setTimeout(function() {
 +
    const termsModal = $('#termsModal');
  
// Happy face
+
    if (termsModal.length) {
krInsertWikiEditorButton({
+
      // Check if the "rpmTermsAgreement" cookie is present
id: "mw-customeditbutton-myspecialbutton",
+
      if (!document.cookie.includes('rpmTermsAgreement')) {
icon: "//upload.wikimedia.org/wikipedia/commons/thumb/a/a4/Gnome-face-smile.svg/22px-Gnome-face-smile.svg.png",
+
        // Show the Bootstrap modal
label: 'Insert happy face',
+
        termsModal.modal({
insertBefore: '[[File:Gnome-face-smile.svg|21px|',
+
          backdrop: "static"
insertAfter: ']]',
+
        });
sampleText: 'Happy face'
+
      }
});
 
  
// Monkey
+
      // Agree button click event to set the "rpm" cookie
krInsertWikiEditorButton({
+
      $('#agreeTermsButton').click(function() {
id: 'mw-customeditbutton-anotherspecialbutton',
+
        termsModal.modal('hide');
icon: '//upload.wikimedia.org/wikipedia/commons/thumb/1/1b/Gnome-face-monkey.svg/22px-Gnome-face-monkey.svg.png',
+
       
label: 'Insert monkey',
+
        // Set the "rpmTermsAgreement" cookie with expiration date five years from now
insertBefore: '[[File:Gnome-face-monkey.svg|21px|',
+
        var expirationDate = new Date();
insertAfter: ']]',
+
        expirationDate.setFullYear(expirationDate.getFullYear() + 5);
sampleText: 'Happy face'
+
        document.cookie = "rpmTermsAgreement=true; expires=" + expirationDate.toUTCString() + "; path=/";
});
+
      });
 +
    }
 +
  }, 200);
  
});
+
  /* MailChimp Form */
  
 +
  var mailchimpFormHTML =
 +
    '<div id="mc_embed_signup">' +
 +
    '    <form action="https://rcabc.us21.list-manage.com/subscribe/post?u=aa570610c198792349997a983&amp;id=91bc758592&amp;f_id=000857e1f0" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_self">' +
 +
    '        <div id="mc_embed_signup_scroll">' +
 +
    '            <div class="mc-field-group form-group">' +
 +
    '                <label for="mce-EMAIL">Email Address</label>' +
 +
    '                <input type="email" value="" name="EMAIL" class="required email form-control" id="mce-EMAIL" required>' +
 +
    '                <span id="mce-EMAIL-HELPERTEXT" class="helper_text"></span>' +
 +
    '            </div>' +
 +
    '            <div id="mce-responses" class="clear foot">' +
 +
    '                <div class="response" id="mce-error-response" style="display:none"></div>' +
 +
    '                <div class="response" id="mce-success-response" style="display:none"></div>' +
 +
    '            </div>' +
 +
    '            <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_aa570610c198792349997a983_91bc758592" tabindex="-1" value=""></div>' +
 +
    '            <input type="submit" value="Subscribe To RPM Updates" name="subscribe" id="mc-embedded-subscribe" class="btn btn-primary">' +
 +
    '        </div>' +
 +
    '    </form>' +
 +
    '</div>';
  
    var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
+
  $('#mailchimpForm').html(mailchimpFormHTML);
    x = new Date;
 
    var y = ' || ' + x.getFullYear() + '-' + monthNames[(x.getMonth() + 3)] + '-' + x.getDate() + ' }}';
 
 
});
 
});

Latest revision as of 22:13, 19 June 2023

// Terms Modal
$(document).ready(function() {

  setTimeout(function() {
    const termsModal = $('#termsModal');

    if (termsModal.length) {
      // Check if the "rpmTermsAgreement" cookie is present
      if (!document.cookie.includes('rpmTermsAgreement')) {
        // Show the Bootstrap modal
        termsModal.modal({
          backdrop: "static"
        });
      }

      // Agree button click event to set the "rpm" cookie
      $('#agreeTermsButton').click(function() {
        termsModal.modal('hide');
        
        // Set the "rpmTermsAgreement" cookie with expiration date five years from now
        var expirationDate = new Date();
        expirationDate.setFullYear(expirationDate.getFullYear() + 5);
        document.cookie = "rpmTermsAgreement=true; expires=" + expirationDate.toUTCString() + "; path=/";
      });
    }
  }, 200);

  /* MailChimp Form */

  var mailchimpFormHTML =
    '<div id="mc_embed_signup">' +
    '    <form action="https://rcabc.us21.list-manage.com/subscribe/post?u=aa570610c198792349997a983&amp;id=91bc758592&amp;f_id=000857e1f0" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_self">' +
    '        <div id="mc_embed_signup_scroll">' +
    '            <div class="mc-field-group form-group">' +
    '                <label for="mce-EMAIL">Email Address</label>' +
    '                <input type="email" value="" name="EMAIL" class="required email form-control" id="mce-EMAIL" required>' +
    '                <span id="mce-EMAIL-HELPERTEXT" class="helper_text"></span>' +
    '            </div>' +
    '            <div id="mce-responses" class="clear foot">' +
    '                <div class="response" id="mce-error-response" style="display:none"></div>' +
    '                <div class="response" id="mce-success-response" style="display:none"></div>' +
    '            </div>' +
    '            <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_aa570610c198792349997a983_91bc758592" tabindex="-1" value=""></div>' +
    '            <input type="submit" value="Subscribe To RPM Updates" name="subscribe" id="mc-embedded-subscribe" class="btn btn-primary">' +
    '        </div>' +
    '    </form>' +
    '</div>';

  $('#mailchimpForm').html(mailchimpFormHTML);
});