Exemple #1
0
                self.link("computeCosts", function (err, costs) {

                    if (err) { return showError(err); }
                    if (costs.error) {
                        showError(costs.error, true);
                    }

                    if (config.oncompute && config.oncompute.total) {
                        var binds = config.oncompute.total;

                        for (var i in binds) {
                            Bind.call(self, binds[i], costs);
                        }
                    }

                    if (config.oncompute && config.oncompute.item) {

                        // each item from cart (mongo id)
                        for (var itemId in costs.items) {

                            // binds
                            var binds = config.oncompute.item;

                            for (var i in binds) {
                                binds[i].context = $("#" + itemId, self.dom);
                                Bind.call(self, binds[i], costs.items[itemId]);
                            }
                        }
                    }
                });
Exemple #2
0
    function init(conf) {

        // initialize the globals
        self = this;
        config = processConfig(conf);
        if (config.container) {
            container = $(config.container, module.dom);
        } else {
            container = module.dom;
        }
        template = $(config.template.value, module.dom);

        // **************************************
        // generate general binds from the config
        var binds = [];

        for (var i in config.controls) {
            switch (i) {
                case "add":
                    binds.push({
                        target: config.controls[i],
                        context: ".controls",
                        on: [{
                            name: "click",
                            emit: "requestNewItem"
                        }]
                    });
                    break;
                case "delete":
                    binds.push({
                        target: config.controls[i],
                        context: ".controls",
                        on: [{
                            name: "click",
                            handler: "removeSelected"
                        }]
                    });
                    break;
            }
        }

        // run the internal binds
        for (var i in binds) {
            Bind.call(self, binds[i]);
        }

        // run the binds
        for (var i in config.binds) {
            Bind.call(self, config.binds[i]);
        }

        Events.call(self, config);

        $(self.dom).on("click", "[disabled]", function () { return false; });
        showEmpty(true);

        self.read();
    }
Exemple #3
0
    function refresh() {

        var binds = [];

        for (var i in config.controls) {
            switch (i) {
                case "add":
                    binds.push({
                        target: config.controls[i],
                        context: ".controls",
                        on: [{
                            name: "click",
                            emit: "requestNewItem"
                        }]
                    });
                    break;
                case "delete":
                    binds.push({
                        target: config.controls[i],
                        context: ".controls",
                        on: [{
                            name: "click",
                            handler: "removeSelected"
                        }]
                    });
                    break;
            }
        }

        // run the internal binds
        for (var i in binds) {
            Bind.call(self, binds[i]);
        }

        // run the binds
        for (var i in config.binds) {
            Bind.call(self, config.binds[i]);
        }

        showEmpty(true);

        self.read();
    }
Exemple #4
0
    function render(item) {
        var newItem = template.clone();

        // run the binds
        for (var i in config.binds) {
            var bindObj = config.binds[i];
            bindObj.context = newItem;
            Bind.call(self, bindObj, item);
        }

        container.append(newItem);
    }
Exemple #5
0
    function renderSelector(item) {
        var newItem = $(template).clone();
        newItem
            .removeClass("template")
            .addClass(config.options.classes.item)
            .appendTo(container)
            .show();

        for (var i in config.template.binds) {
            var bindObj = config.template.binds[i];
            bindObj.context = newItem;
            Bind.call(self, bindObj, item);
        }

        newItem.attr('id', item[config.options.id]);
    }
Exemple #6
0
    function init(conf) {
        // initialize the globals
        self = this;
        config = processConfig(conf);
        if (config.container) {
            container = $(config.container, module.dom);
        } else {
            container = module.dom;
        }
        template = $(config.template.value, module.dom);

        // **************************************
        // generate general binds from the config
        var binds = [];

        for (var i in config.controls) {
            switch (i) {
                case "add":
                    binds.push({
                        target: config.controls[i],
                        context: ".controls",
                        on: [{
                            name: "click",
                            handler: "createItem"
                        }]
                    });
                    break;
                case "delete":
                    binds.push({
                        target: config.controls[i],
                        context: ".controls",
                        on: [{
                            name: "click",
                            handler: "removeSelected"
                        }]
                    });
                    break;
            }
        }

        if (pagination) {

            // Build DOM references
            pagination.dom = {};
            pagination.dom.container = $(pagination.container);
            pagination.dom.next = $(pagination.controls.next);
            pagination.dom.previous = $(pagination.controls.previous);
            pagination.dom.pages = [];

            disabledClass = pagination.controls.disable;

            for (var i in pagination.controls) {

                switch (i) {

                    case "next":
                        $(pagination.controls[i]).on("click", function () {
                            var clickedElem = $(this);

                            if (clickedElem.hasClass(disabledClass) || clickedElem.prop(disabledClass)) {
                                return false;
                            }

                            goToNextPage();
                            return false;
                        });
                        break;

                    case "previous":
                        $(pagination.controls[i]).on("click", function () {
                            var clickedElem = $(this);

                            if (clickedElem.hasClass(disabledClass) || clickedElem.prop(disabledClass)) {
                                return false;
                            }

                            goToPrevPage();
                            return false;
                        });
                        break;
                }
            }

            if (paginationNumbers) {

                $(self.dom).on("click", "." + pagination.numbers.classes.item, function() {

                    if ($(this).hasClass("active")) {
                        return false;
                    }

                    var pageNumber = parseInt($(this).attr("data-page"));

                    if (!pageNumber) {
                        return;
                    }

                    page = pageNumber;

                    showPage(pageNumber, dbData.filter, dbData.options);

                    if (!pagination.hash) {
                        return false;
                    }
                });
            }
        }

        // run the internal binds
        for (var i in binds) {
            Bind.call(self, binds[i]);
        }

        // run the binds
        for (var i in config.binds) {
            Bind.call(self, config.binds[i]);
        }

        Events.call(self, config);


        if (config.options.autofetch) {
            self.read(config.options.filters || {}, { sort: config.options.sort });
        }
    }
Exemple #7
0
module.exports = function init (config) {

    // create the socket
    var socket = io.connect(config.origin || ("//" + location.host));

    // get self (the module)
    var self = this;

    // store config in self.config
    self.config = config;

    // process config
    processConfig.call(self);

    // run the binds
    for (var i = 0; i < config.binds.length; ++i) {
        Bind.call(self, config.binds[i]);
    }

    /**
     *  self.socketInit (object, function);
     *  Inits the socket in the page. This is called automatically!
     *
     *  options: an object containing the following fields:
     *      - force: if the websocket is already inited and `force` is `true`,
     *        the websocket will be reinited
     *      - log: log level passed to socket.io
     *
     *  callback: a function that is called when the socket it inited
     *
     * */
    self.socketInit = function (options, callback) {

        // call the server operation
        self.link("init", {data: options}, function (err, data) {

            // first time when initing the websocket
            if (!err && data === "REFRESH") {
                location.reload();
            }

            callback (err, data);
        });
    };

    /**
     *  self.clientEmit (object, function);
     *  Emits an event and data to the server
     *
     *  options: an object containing
     *      - event: the event name
     *      - data:  the data that goes to server side
     *
     *  callback: a function that is passed too to the emit function
     *            from socket emit function
     * */
    self.clientEmit = function (options, callback) {

        // default value for callback
        callback = callback || function () {};

        // validate options
        if (!options || options.constructor !== Object && options.constructor !== String) {
            throw new Error ("Options must be a string or an object");
        }

        // validate callback
        if (!callback || callback.constructor !== Function) {
            throw new Error ("Callback must be a function");
        }

        // emit event passing options and callback
        self.socket.emit(options._event || options.event || options, options, callback);
    }

    /**
     *  self.clientListen (object, function);
     *  Listen an event that comes from the server
     *
     *  options: an object containing
     *      - event: the event name
     *
     *  callback: a function that is passed too to the on function
     *
     * */
    self.clientListen = function (options, callback) {

        // listen for events from server
        self.socket.on(options.event, callback);
    };

    /**
     *  self.serverSend (options);
     *  Call the sendMessage from the server
     *
     *  message: an object containing
     *      - type (string): client, session, group, or all
     *      - session (session id): which clinet should recive this message; all if undefined
     *      - data (object)
     * */
    self.serverSend = function (options) {

        // override the event field
        options._event = "sockets.server.send";

        // emit a server event
        self.clientEmit(options);
    };

    /**
     *  self.serverEmitGlobal (options);
     *  Emits a global event using M.emit()
     *
     *  options:
     *   - event: the event name that is emited
     *   - data: data to be emited
     * */
    self.serverEmitGlobal = function (options) {

        // override the event field
        options._event = "sockets.server.emitGlobal";

        // global server event
        self.clientEmit(options);
    };

    /**
     *  This automatically inits the socket in the page
     *
     * */
    self.socketInit({}, function (err) {

        // handle error
        if (err) { return self.emit("error", err); }

        // set socket object
        self.socket = socket;

        // emit ready
        self.emit("ready", self.config, socket);
    });

    // call events
    Events.call(self, config);
};
Exemple #8
0
        selector: function(item) {
            // TODO where do we know the key is the _id
            var existingItem = container.find("#" + item._id);

            // new item type in the cart
            if (!existingItem.length) {

                var newItem = $(template).clone();
                newItem
                    .removeClass("template")
                    .addClass(config.options.classes.item)
                    .appendTo(container)
                    .show();

                for (var i in config.template.binds) {
                    var bindObj = config.template.binds[i];
                    bindObj.context = newItem;
                    Bind.call(self, bindObj, item);
                }

                if (item.amountError) {
                    showError(item.amountError);
                }

                // now there should be an element with this id
                existingItem = container.find("#" + item._id);
            }

            var qKey = config.options.quantityKey;
            existingItem.find(".quantity").each(function() {

                var elem = $(this);

                switch (this.tagName) {
                    case "INPUT":
                        var initialAdd = elem.val() == 0;
                        elem.attr("value", item[qKey]);
                        elem.val(item[qKey]);
                        if (initialAdd) {
                            elem.on("change", function() {
                                blockCart();
                                var newVal = parseInt($(this).val());
                                if (isNaN(newVal) || newVal < 0) {
                                    $(this).val(item.quantity);
                                    unblockCart();
                                } else if (newVal === 0) {
                                    if (config.removeIfZero) {
                                        existingItem.fadeOut(function() {
                                            item.quantity = newVal;
                                            removeItem(item);
                                            unblockCart();
                                        });
                                    } else {
                                        $(this).val(1);
                                        unblockCart();
                                    }
                                } else {
                                    item.quantity = newVal;
                                    updateItem(item);
                                }
                            });
                            $("input[type='number']:visible", self.dom).twbsNumberInput();
                        }
                        break;
                    default:
                        elem.text(item[qKey]);
                }
            });

            return;
        }