User:Mfaizsyahmi/notiplus/Code

///* /* notiplus alpha 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/User:Mfaizsyahmi/Noti?action=render'; // TEST notiplus.cookiePrefix = notiplus.cookiePrefix || 'HPW-'; notiplus.consentRequired = notiplus.consentRequired || true; notiplus.lang = notiplus.lang || 'en';

// 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

// 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 * takes the id, expiration period and html content * directly adds the notification, returns nothing */ notiplus._addnoti = function(id,exp,html) { //var $N = notiplus.$N; var $content = $(' ').html(html); var $dismiss = $('').on('click.notiplus', function(e) {		$.cookie(notiplus.cookiePrefix + id + "-dismissed", "1", { path:"/", expires:exp });		$content.parent.remove;	}); $content.prepend($dismiss) 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; // go through each div and decide whether to display it	$(notiplus._notidata).filter('div[id]').each( function(e) {		//console.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'); if (typeof id == 'undefined') return false; // DON'T PROCEED WITHOUT IDs! // check if already dismissed var dismissed = $.cookie(notiplus.cookiePrefix + id + "-dismissed") || false //console.log(now, starts,expires,ends,id, dismissed, dismissed == false, now >= starts , now < ends) // display if not dismissed and within time frame if (dismissed == false && now >= starts && now < ends) notiplus._addnoti( id, expires, $(this).html ) }) };

// MAIN INITIALIZATION CODE // check consent and ajax load the page containing the notifications notiplus.init = function { // creates the notification container if ($("#WikiaNotifications").length==0) { $("body").append(''); }	notiplus.$N = $("#WikiaNotifications"); // consent var consent = $.cookie(notiplus.cookiePrefix + "consented"); 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 $.ajax({		url: notiplus.url,		success: function(data) {			notiplus._notidata = data;			notiplus.parse;		}, error: function(obj,str) {			console.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:'/'}) } notiplus.reset;*/

// Starts the script main routine notiplus.init;

///* */