App-Dochazka-WWW

 view release on metacpan or  search on metacpan

share/js/dochazka-www/caches.js  view on Meta::CPAN

// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// *************************************************************************
//
// app/caches.js
//
// application-specific caches
//
"use strict";

define ([
    'jquery',
    'app/lib',
    'app/prototypes',
    'ajax',
    'current-user',
    'datetime',
    'lib',
    'populate',
    'stack',
], function (
    $,
    appLib,
    appPrototypes,
    ajax,
    currentUser,
    dt,
    coreLib,
    populate,
    stack,
) {

    var
        colorList = [
            "#e6194b",
            "#3cb44b",
            "#ffe119",
            "#0082c8",
            "#f58231",
            "#911eb4",
            "#46f0f0",
            "#f032e6",
            "#d2f53c",
            "#fabebe",
            "#008080",
            "#e6beff",
            "#aa6e28",
            "#fffac8",
            "#800000",
            "#aaffc3",
            "#808000",
            "#ffd8b1",
            "#000080",
        ],

        backgroundColorStashed = null,
        currentEmployeeStashed = null,
        currentEmplPrivStashed = null,

        activityCache = [],
        activityByAID = {},
        activityByCode = {},

        profileCache = [],
        profileByEID = {},
        profileByNick = {},

        scheduleCache = [],
        scheduleBySID = {},
        scheduleByScode = {},

        endTheMasquerade = function () {
            currentUser('flag1', 0); // turn off masquerade flag
            // console.log('flag1 === ', currentUser('flag1'));
            currentUser('obj', currentEmployeeStashed);
            currentEmployeeStashed = null;
            appLib.fillUserBox();
            $('body').css("background-color", backgroundColorStashed);
            coreLib.displayResult('Masquerade is finished');
            $('input[name="sel"]').val('');
        },

        getActivityByAID = function (aid) {
            if (activityCache.length > 0) {
                return activityByAID[aid];
            }
            console.log('CRITICAL ERROR: activity cache not populated');
            return null;
        },

        getActivityByCode = function (code) {
            console.log("Entering getActByCode() with code " + code);
            if (activityCache.length > 0) {
                return activityByCode[code];
            }
            console.log('CRITICAL ERROR: activity cache not populated');
            return null;
        },

        getProfileByEID = function (eid) {
            console.log("Entering getProfileByEID() with eid " + eid);
            if (profileCache.length > 0) {
                return profileByEID[parseInt(eid, 10)];
            }
            console.log('Employee profile cache not populated yet');
            return null;
        },

        getProfileByNick = function (nick) {
            console.log("Entering getProfileByNick() with nick " + nick);
            if (profileCache.length > 0) {
                return profileByNick[nick];
            }
            console.log('Employee profile cache not populated yet');
            return null;
        },

        getScheduleByScode = function (scode) {
            console.log("Entering getScheduleByScode() with scode " + scode);
            if (scheduleCache.length > 0) {
                return scheduleByScode[scode];
            }
            console.log('Schedule cache not populated yet');
            return null;
        },

        getScheduleBySID = function (sid) {
            console.log("Entering getScheduleBySID() with SID " + sid);
            if (scheduleCache.length > 0) {
                return scheduleBySID[sid];
            }
            console.log('Schedule cache not populated yet');
            return null;
        },

        fullDayTsrange = function (date) {
            return "[ \"" + date + " 00:00\", \"" + date + " 24:00\" )";
        },

        masqEmployee = function (obj) {
            var cu, priv;

            // if obj is empty, dA was selected from menu
            // if obj is full, it contains the employee to masquerade as
            console.log("Entering masqEmployee() with object", obj);
        
            if (currentEmployeeStashed) {
                endTheMasquerade();
                return null;
            }

            if (coreLib.isObjEmpty(obj)) {
                // let the admin pick which user to masquerade as
                stack.push('searchEmployee', {}, {
                    "masquerade": true,
                });
                return null;
            }

            // check the masquerade object
            cu = currentUser('obj');
            priv = currentUser('priv');
            if (obj.nick === cu.nick) {
                coreLib.displayError('Request to masquerade as self makes no sense');
                return null;
            }
            if (priv !== 'admin') {
                if (obj.supervisor !== cu.eid) {
                    coreLib.displayError(obj.nick + " is not your supervisee");
                    return null;
                }
            }

            // let the masquerade begin
            currentEmployeeStashed = $.extend({}, cu);
            backgroundColorStashed = $('body').css("background-color");
            currentUser('obj', obj);
            currentUser('flag1', 1); // turn on masquerade flag
            populate.bootstrap([
                populateFullEmployeeProfileCache,
                populateScheduleBySID,
            ]);
            appLib.fillUserBox(); // reset userbox
            $('body').css("background-color", "#669933");
            stack.unwindToType('dmenu'); // return to most recent dmenu
        },

        populateActivityCache = function (populateArray) {
            var ao, rest, sc, fc, populateContinue;
            console.log("Entering populateActivityCache()");
            populateContinue = populate.shift(populateArray);
            if (coreLib.isArray(activityCache) && activityCache.length > 0) {
                console.log("populateActivityCache(): noop, cache already populated");
                console.log("Activity Cache contents", activityCache);
                populateContinue(populateArray);
                return null;
            }
            rest = {
                "method": 'GET',
                "path": 'activity/all'
            };
            sc = function (st) {
                var i;
                for (i = 0; i < st.payload.length; i += 1) {
                    ao = st.payload[i];
                    ao.color = colorList[i];
                    activityCache.push(ao);
                    activityByAID[st.payload[i].aid] = ao;
                    activityByCode[st.payload[i].code] = ao;
                }
                coreLib.displayResult(i + 1 + " activity objects loaded into cache");
                populateContinue(populateArray);
            };
            fc = function (st) {
                coreLib.displayError(st.payload.message);
                populateContinue(populateArray);
            };
            ajax(rest, sc, fc);
        },

        populateAIDfromCode = function (populateArray) {
            var aid, code, populateContinue;
            populateContinue = populate.shift(populateArray);
            // assume there is a form with the code in it
            code = coreLib.nullify($('#iNact').text());
            if (! code) {
                code = coreLib.nullify($('input[id="iNact"]').val());
                if (! code) {
                    console.log("CRITICAL ERROR: populateAIDfromCode: no code in form");
                    populateContinue(populateArray);
                }
            }
            console.log("populateAIDfromCode: Activity code is " + code);
            aid = getActivityByCode(code).aid;
            $('#acTaid').html(String(aid));
            populateContinue(populateArray);
        },

        populateExistingIntervals = function (populateArray) {
            var cu = currentUser('obj'),
                eid = cu.eid,
                date, lia, tsr,
                rest, sc, fc, populateContinue;
            date = $("#iNdate").text();



( run in 1.128 second using v1.01-cache-2.11-cpan-140bd7fdf52 )