App-Dochazka-WWW

 view release on metacpan or  search on metacpan

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

// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// *************************************************************************
//
// app/priv-lib.js
//
"use strict";

define ([
    'jquery',
    'ajax',
    'current-user',
    'datetime',
    'lib',
    'target',
    'stack',
    'start'
], function (
    $,
    ajax,
    currentUser,
    datetime,
    coreLib,
    target,
    stack,
    start
) {

    var genPrivHistoryAction = function (tgt) {
            return function (obj) {
                console.log("Entering some privHistory-related function, target is " + tgt);
                var nick = currentUser('obj').nick,
                    rest = {
                        "method": 'GET',
                        "path": 'priv/history/nick/' + nick
                    },
                    // success callback
                    sc = function (st) {
                        if (st.code === 'DISPATCH_RECORDS_FOUND') {
                            var history = st.payload.history.map(
                                function (row) {
                                    return {
                                        "nick": nick,
                                        "phid": row.phid,
                                        "priv": row.priv,
                                        "effective": datetime.readableDate(row.effective)
                                    };
                                }
                            );
                            if (tgt === 'privHistoryDtable') {
                                stack.push(tgt, history, { "xtarget": "myProfileAction" });
                            } else if (tgt === 'privHistoryDrowselect') {
                                stack.push(tgt, {
                                    'pos': 0,
                                    'set': history
                                });
                            }
                        }
                    },
                    fc = function (st) {
                        if (st.payload.code === "404") {
                            // The employee has no history records. This is not
                            // really an error condition.
                            if (tgt === 'privHistoryDtable') {
                                stack.push(tgt, [], { "xtarget": "empProfile" });
                            } else if (tgt === 'privHistoryDrowselect') {
                                stack.push(tgt, {
                                    'pos': 0,
                                    'set': []
                                });
                            }
                        }
                        coreLib.displayError(st.payload.message);
                    };
                ajax(rest, sc, fc);
            };
        },
        privHistoryAddRecordAction = function (obj) {
            var cu = currentUser('obj');
            console.log("Entering privHistoryAddRecordAction with nick " + cu.nick);
            stack.push('privHistoryAddRecord', {
                'nick': cu.nick
            });
        },
        privHistoryDeleteAction = function (obj) {
            var phid,
                set = coreLib.drowselectState.set,
                pos = coreLib.drowselectState.pos,
                rest = {
                    "method": 'DELETE',
                    "path": 'priv/history/phid/'
                },
                // success callback
                sc = function (st) {
                    if (st.code === 'DOCHAZKA_CUD_OK') {
                        console.log("Payload is", st.payload);
                        stack.unwindToTarget("actionPrivHistory");
                        coreLib.displayError("Priv (status) history record successfully deleted");
                    }
                },
                fc = function (st) {
                    coreLib.displayError(st.payload.message);
                };

            if (set === null || set === undefined || set.length === 0) {
                coreLib.displayError("Nothing to do");
                start.drowselectListen();
            }
            phid = set[pos].phid;
            console.log("Going to delete PHID " + phid);
            rest.path += phid;
            ajax(rest, sc, fc);
            // start.drowselectListen();
        },
        privHistorySaveAction = function (obj) {
            console.log("Entering privHistorySaveAction with obj", obj);
            var rest = {
                    "method": 'POST',
                    "path": 'priv/history/nick/' + currentUser('obj').nick,
                    "body": {
                        "effective": $("#pHeffective").val(),



( run in 0.489 second using v1.01-cache-2.11-cpan-d7f47b0818f )