MediaWiki:Notiplus.js

///* */ /* notiplus alpha 1.1 * Adds support for localized notifications on Wikia wikis * Copyright (c) 2015 M. Faiz Syahmi User:mfaizsyahmi * This program is free software; you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *   the Free Software Foundation; either version 2 of the License, or *   (at your option) any later version */

// namespace window.notiplus = window.notiplus || {};

// default values notiplus.url = notiplus.url || '/wiki/Project:Notiplus?action=render'; // TEST notiplus.cookiePrefix = notiplus.cookiePrefix || 'notiplus-'; notiplus.consentRequired = notiplus.consentRequired || true; notiplus.lang = notiplus.lang || 'en'; //DEBUG notiplus.DEBUG = true;

// UI strings notiplus.i18n = {}; notiplus.i18n.en = { consent_text: 'To enhance your experience, this wiki would like to display wiki-specific notifications.', consent_allow: 'Allow', consent_deny: 'Deny', or: 'or' }; notiplus.str = notiplus.i18n[notiplus.lang] || notiplus.i18n.en;

// internal data variables notiplus.$N = $("#WikiaNotifications"); // holds a reference to Wikia Notification jquery object notiplus._notidata = null; // holds the html of returned notification page notiplus.log = function { if (notiplus.DEBUG) console.log(arguments) };

// HELPER FUNCTIONS /* format - formats strings (from stackoverflow/610406) * returns formatted string */ notiplus.format = function(format) { var args = Array.prototype.slice.call(arguments, 1); return format.replace(/{(\d+)}/g, function(match, number) { 		return (typeof args[number] != 'undefined') ? args[number] : match;	}); };

/* _addnoti - adds the individual notiplus * Args: id *       exp - cookie expiration *      linkDismiss - whether all links should dismiss the notification *      html - content * directly adds the notification, returns nothing */ notiplus._addnoti = function(id, exp, linkDismiss, html) { notiplus.log('Adding item ' + id) //var $N = notiplus.$N; var $content = $(' ').html(html); var $dismiss = $(''); $content.prepend($dismiss); if (linkDismiss) $dismiss.add(':link',$content); $dismiss.on('click.notiplus', function(e) {		$.cookie(notiplus.cookiePrefix + id + "-dismissed", "1", { path:"/", expires:exp });		$content.parent.remove;	}); notiplus.$N.append( $('').append($content) ) }

// MAIN SUBROUTINE // parses the returned ajax and displays notiplus notiplus.parse = function { // abort if no data if (notiplus._notidata == null) return false; var now = new Date, $data = $(notiplus._notidata).not('script').filter('div[id]'); notiplus.log('Parsing '+$data.length + 'items') // go through each div and decide whether to display it	$data.each( function(e) {		//notiplus.log($(this));		var starts = new Date($(this).attr('data-start')),			expires = Number($(this).attr('data-expires')) || 300;		var ends = new Date(starts.getTime + expires*86400000);

var id = $(this).attr('id'), linkDismiss = ($(this).attr('data-linkdismiss') || false); if (typeof id == 'undefined') return false; // DON'T PROCEED WITHOUT IDs! // check if already dismissed var dismissed = $.cookie(notiplus.cookiePrefix + id + "-dismissed") || false notiplus.log(id,linkDismiss,dismissed) // display if not dismissed and within time frame if (dismissed == false && now >= starts && now < ends) notiplus._addnoti( id, expires, linkDismiss, $(this).html ) }) };

// MAIN INITIALIZATION CODE // check consent and ajax load the page containing the notifications notiplus.init = function { notiplus.log('Initializing notiplus...') // creates the notification container if ($("#WikiaNotifications").length==0) { $("body").append(''); }	notiplus.$N = $("#WikiaNotifications"); // consent var consent = $.cookie(notiplus.cookiePrefix + "consented"); notiplus.log('consent value is ',consent); if (notiplus.consentRequired && consent == null) { // Need consent, display consent notification var allowLink = $(notiplus.format('{0}', notiplus.str.consent_allow)) .on('click.notiplus', function(e) {					$.cookie(notiplus.cookiePrefix + "consented", true, {expires: 365, path: '/'})					$('#notification-consent').parent.remove;					notiplus.init; // run through initialization code again, this time consent check wold have been cleared				}), denyLink = $(notiplus.format('{0}', notiplus.str.consent_deny)) .on('click.notiplus', function(e) {					$.cookie(notiplus.cookiePrefix + "consented", false, {expires: 365, path: '/'})					$('#notification-consent').parent.remove;				}), dismiss = $('').on('click.notiplus', function(e) {					//$.cookie(notiplus.cookiePrefix + "consented", false, {expires: 365, path: '/'})					$('#notification-consent').parent.remove;				}), $consent = $(' ') .prepend(dismiss) .prepend(notiplus.format(' {0} ',notiplus.str.consent_text)) .append( $(' ')					.append(allowLink)					.append(notiplus.format(' {0} ',notiplus.str.or))					.append(denyLink)				); notiplus.$N.append( $('').append($consent) ); return; // exit the function } else if (notiplus.consentRequired && consent == 'false') { // Consent required but denied, abort return false; } // Consent granted or not required past this point // loads the page containing notiplus notiplus.log('Loading notifications...') $.ajax({		url: notiplus.url,		success: function(data) {			notiplus._notidata = data;			notiplus.parse;		}, error: function(obj,str) {			notiplus.log('error loading notiplus\'s notifications.', str);		}	}) }; // FOR DEBUG notiplus.reset = function { $.cookie(notiplus.cookiePrefix + "consented", -1, {expires:-1}); $.cookie(notiplus.cookiePrefix + "consented", -1, {expires:-1, path:'/'}); if (notiplus._notidata !== null) { $(notiplus._notidata).filter('div[id]').each( function {			$.cookie(notiplus.cookiePrefix + $(this).attr('id') + "-dismissed", -1, {expires:-1});			$.cookie(notiplus.cookiePrefix + $(this).attr('id') + "-dismissed", -1, {expires:-1, path:'/'});		}) } } //notiplus.reset;

// Starts the script main routine notiplus.init;

///* */