App-Dochazka-WWW

 view release on metacpan or  search on metacpan

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

    'jquery',
    'ajax',
    'app/caches',
    'current-user',
    'datetime',
    'lib',
    'stack',
], function (
    $,
    ajax,
    appCaches,
    currentUser,
    dt,
    coreLib,
    stack,
) {

    var
        createIntervalCheckMandatoryProps = function (obj) {
            var actObj;
            // check that all mandatory properties are present
            if (! obj.iNdate) {
                stack.restart(undefined, {
                    "resultLine": "Interval date missing"
                });
                return false;
            }
            if (! obj.code) {
                stack.restart(undefined, {
                    "resultLine": "Interval activity code missing"
                });
                return false;
            }
            if (! obj.aid) {
                console.log("Looking up activity " + obj.code + " in cache");
                actObj = appCaches.getActivityByCode(obj.code);
                if (! actObj) {
                    stack.restart(undefined, {
                        "resultLine": 'Activity ' + obj.code + ' not found'
                    });
                    return false;
                }
                obj.aid = actObj.aid;
            }
            if (! obj.iNtimerange) {
                stack.restart(undefined, {
                    "resultLine": "Interval time range missing"
                });
                return false;
            }
            return true;
        },

        createMultipleIntSave = function (obj) {
            var cu = currentUser('obj'),
                daylist = $('input[id="iNdaylist"]').val(),
                month = $('input[id="iNmonth"]').val(),
                year = $('input[id="iNyear"]').val(),
                dl = daylist.split(','),
                i, rest, sc, fc;
            // validate activity
            if (obj.code) {
                console.log("Looking up activity " + obj.code + " in cache");
                i = appCaches.getActivityByCode(obj.code);
                if (! i) {
                    coreLib.displayError('Activity ' + obj.code + ' not found');
                    return null;
                }
                obj.aid = i.aid;
            } else {
                coreLib.displayError("Interval activity code missing");
                return null;
            }
            // validate day list
            if (! coreLib.isArray(dl) || dl.length === 0) {
                coreLib.displayError("Invalid day list");
                return null;
            }
            for (i = 0; i < dl.length; i += 1) {
                dl[i] = year + '-' + dt.monthToInt(month) + '-' + dl[i];
            }
            console.log("Date list", dl);
            rest = {
                "method": "POST",
                "path": "interval/fillup",
                "body": {
                    'date_list': dl,
                    'dry_run': '0',
                    'eid': String(cu.eid),
                    'aid': obj.acTaid,
                },
            };
            sc = function (st) {
                if (st.code === "DISPATCH_FILLUP_INTERVALS_CREATED") {
                    coreLib.displayResult(
                        st.payload.success.intervals.length + " intervals created; " +
                        st.payload.failure.intervals.length + " intervals failed"
                    );
                } else if (st.code === "DISPATCH_FILLUP_NO_INTERVALS_CREATED") {
                    coreLib.displayResult("The dates in question already have 100% schedule fulfillment");
                } else {
                    coreLib.displayError(st.text);
                }
            };
            ajax(rest, sc);
        },

        createSingleIntMenuItem = function (obj) {
            stack.push('createSingleInt');
        },

        createSingleIntSave = function (obj) {
            var caller = stack.getTarget().name,
                cu = currentUser('obj'),
                rest,
                sc = function (st) {
                    if (caller === 'createSingleIntFixedDay') {
                        stack.unwindToTarget('viewIntervalsAction');
                    } else {
                        stack.unwindToTarget(
                            'createSingleInt',
                            emptyObj,
                            {
                                "resultLine": "Interval " + st.payload.iid + " created",
                                "inputId": "iNdate",
                            }
                        );
                    }
                };
            console.log("Entering createSingleIntSave() from caller " + caller + " with obj", obj);
            if (caller === 'createSingleInt' || caller === 'createSingleIntFixedDay') {
                // obj already populated
            } else if (caller === 'createLastPlusOffset' || caller === 'createNextScheduled') {
                // Scrape time range from form



( run in 1.288 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )