"use strict"; var ksLib = (function() { $.ajaxSetup({ cache: false }); var _addHeaderFooter = function(cmp, hdr, ftr, displayRecord) { //cmp: compare function. //hdr: array of headers. Header can be a function (function argument is current record), or header can be a string. //ftr: array of footers. Footer can be a function (function argument is previous record), or footer can be a string. return function(acc, cur, ind, arr) { var output = []; var splitCode; if (ind !== 0) { //only display footer if not first record //footer begins at level splitCode. Of course in reverse order splitCode = cmp(arr[ind - 1], cur); if (splitCode !== -1) { //not equal -1 so new group output.push(ftr.slice(splitCode).map(function(fun) {return (typeof fun === "function")?fun(arr[ind - 1]):fun}).reverse().join('')); } } else { splitCode = 0; } //header begins at level splitCode if (splitCode !== -1) { //not equal -1 so new group output.push(hdr.slice(splitCode).map(function(fun) {return (typeof fun === "function")?fun(cur):fun}).join('')); } output.push(displayRecord(cur)); if (ind === arr.length - 1) { //last element, so display all footers. Of course in reverse order output.push(ftr.map(function(fun) {return (typeof fun === "function")?fun(cur):fun}).reverse().join('')); } //Join them to make it 1 string and add to array output return acc.concat(output.join('')); } }; var createOutput = function(thisReport) { return function(thisData) { return thisReport.source(thisData) .reduce(_addHeaderFooter(thisReport.compare, thisReport.headers, thisReport.footers, thisReport.display), []) .join(''); } }; var updDecimalFlds = function(data) { //for example arr.map(ksLib.updDecimalFlds(['MinBeurtenAlsUit', 'Hserie'])) //translates decimal . in , return function(elem) { data.forEach(function(e) {elem[e] = elem[e].replace(/\./g, ',')}); return elem; } }; var updNullFlds = function(data) { //for example arr.map(ksLib.updNullFlds(['MinBeurtenAlsUit', 'SpelerNaam'])) //translates null in '' return function(elem) { data.forEach(function(e) {elem[e] = (elem[e] !== null)?elem[e]:''}); return elem; } }; var htmlSanitize = function(elem) { return elem.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;'); }; var updStringFlds = function(data) { //for example arr.map(ksLib.updStringFlds(['PloegNaam', 'SpelerNaam'])) //translates special characters < > & return function(elem) { data.forEach(function(e) {elem[e] = htmlSanitize(elem[e])}); return elem; } }; var weekStart = function(strDate, firstWeekday) { //strDate is date string in format 2016-11-29... //firstWeekday: 0=Sunday, 1=Monday ... var result = new Date(strDate); var daysBack = result.getDay() - firstWeekday; result.setDate(result.getDate() - daysBack - (daysBack < 0 ? 7 : 0)); return result; }; var dutchDateFormat = function(strDate) { //for example converts 2016-11-29 13:59:30 to 29-11-2016 13:59:30 return strDate.slice(8, 10) + '-' + strDate.slice(5, 7) + '-' + strDate.slice(0, 4) + strDate.slice(10); }; var td = function(text, align) { return '<td' + (align?' style="text-align:' + align + '"':'') + '>' + text + '</td>'; }; var _lightBoxClose = function() { //remove contents of ks_lightbox in case a video or other content is actively playing //hide ks_lightbox and shadow DIV's $('#ks_lightbox').empty().hide(); $('#ks_lightbox-shadow').hide(); }; var lightBox = function(insertContent){ //add ks_lightbox/shadow <div/>'s if not previously added if ($('#ks_lightbox').length === 0) { $('body').append('<div id="ks_lightbox-shadow"/><div id="ks_lightbox"/>'); $(document).on('click', '#ks_lightbox-shadow, #ks_lightBoxClose', _lightBoxClose); } //insert content $('#ks_lightbox') .html('<img id="ks_lightBoxClose" style="float:right;cursor:pointer;" src="closebutton.png" alt=""/>' + insertContent) //move the ks_lightbox to the current window top + 100px and show it .css('top', $(window).scrollTop() + 100 + 'px') .show(); $('#ks_lightbox-shadow').show(); }; var detectFldChange = function(data) { //helper function for addPlace (the 2 callbacks) //detectFldChange(['field1', 'field2']) return function(prv, cur) { return data.some(function(e) {return (prv[e] !== cur[e])}); } }; var addPlace = function(funcNotSame, funcNewGroup) { return function(acc, cur, ind, inpArr) { //helper function for example arrTussenstand.reduce(addPlace(cb1, cb2), {arr: []}) //for example cb1 = function(prv, cur) {return (cur.MinBeurtenAlsUit !== prv.MinBeurtenAlsUit)} //cb2 is optional var runningCounter; if (acc.arr.length === 0) { //Set field Place to 1 on first item cur.Place = runningCounter = 1; } else { var prv = acc.arr[acc.arr.length - 1]; if ((typeof funcNewGroup === "function") && funcNewGroup(prv, cur)) { //Set field Place to 1 if Afdeling is different cur.Place = runningCounter = 1; } else { runningCounter = acc.runningCounter + 1 if (funcNotSame(prv, cur)) { //increase field Place in case one or more of the relevant fields are different cur.Place = runningCounter; } else { //Nothing changes. Same Place cur.Place = prv.Place; } } } if (ind === inpArr.length - 1) { //last element return acc.arr.concat(cur); } else { return {arr: acc.arr.concat(cur), runningCounter: runningCounter}; } } }; var sameWidth = function(jQryElements) { jQryElements.width(Math.max.apply(null, jQryElements.map(function(){return $(this).width()}).get())); } var listExpanded = function(adsList) { var adsListExpanded = []; for (var i=0;i<adsList.length;i++) { for (var i2=1;i2<=adsList[i][2];i2++) { adsListExpanded.push([adsList[i][0], adsList[i][1]]); } } return adsListExpanded; }; var showRandomAd = function(srcAdsList, Counter) { var adsList = srcAdsList.slice(0); var totStr = ''; if (typeof Counter === "undefined") { Counter = 1; } for (var i=1;i<=Counter;i++) { if (adsList.length !== 0) { var randomImage = adsList.splice(Math.random() * adsList.length, 1); var htmlText = '<img src="' + randomImage[0][0] + '" alt="reclame" />'; if (randomImage[0][1] !== '') { htmlText = '<a href="' + randomImage[0][1] + '">' + htmlText + '</a>'; } totStr += htmlText + ' '; adsList = adsList.filter(function(value) {return (value[0] !== randomImage[0][0])}); } } return totStr; }; var errorFetchingData = function() { alert('Gegevens ophalen is mislukt!'); }; return {createOutput: createOutput, updNullFlds: updNullFlds, updDecimalFlds: updDecimalFlds, updStringFlds: updStringFlds, htmlSanitize: htmlSanitize, td: td, lightBox: lightBox, weekStart: weekStart, dutchDateFormat: dutchDateFormat, addPlace: addPlace, detectFldChange: detectFldChange, errorFetchingData: errorFetchingData, sameWidth: sameWidth, listExpanded: listExpanded, showRandomAd: showRandomAd} }());