<commit_before>var namespace = new Namespace("ryepdx.openerp.quickship.kiosk");

/**
 * Coordinates the UI and the server.
 * The model and view should both be primarily inert classes that the controller does things to.
 * The controller sets up UI events and tells the model to do things to the server based on those events.
 * The controller also tells the UI to update itself based on the model's responses.
 *
 * @param model
 * @param view
 * @constructor
 */
namespace.Controller = function (model, view, options) {
    this.view = view;
    this.model = model;
    this.options = this._parseOptions(options);

    this._scale_polling = false;

    this.setupEvents();
    this.initUspsBalance();
    this.activate();
};

/**
 * Kicks off background polling.
 */
namespace.Controller.prototype.activate = function () {
    this._scale_polling = true;
    this._pollScale();
};

/**
 * Stops background polling.
 */
namespace.Controller.prototype.deactivate = function () {
    this._scale_polling = false;
};

/**
 * Wires up View events to Model actions.
 */
namespace.Controller.prototype.setupEvents = function () {
    var that = this;
    that._setupHashChangeEvent();
    that._setupSelectedQuoteChangeEvent();
    that._setupSelectedQuoteOkayButton();
    that._setupSelectedQuoteCancelButton();
    that._setupInputCompleteEvent();
    that._setupBoxCodeSelectedEvent();
    that._setupSaleOrderChangeEvent();
};

/**
 * Set the initial USPS account balance in the View.
 */
namespace.Controller.prototype.initUspsBalance = function () {
    var that = this;

    that.model
        .getUspsAccount()
        .done(function (response) {
            if (response.postage_balance) {
                that.view.updateUspsBalance(response.postage_balance);
            }
            if (response.error) {
                that.options.logger.error("Error retrieving USPS balance: \"" + response.error + "\"");
                that.options.message.error("Could not retrieve USPS balance!");
            }
        });
};

/**
 * Set the shipper ID default value.
 */
namespace.Controller.prototype.initShipperID = function () {
    var that = this;

    that.model
        .getQuickshipID()
        .done(function (quickship_id) {
            that.view.setShipper(quickship_id);
        });
}

/**
 * Takes a number corresponding to the position of a label in the list of
 * quotes and prints the label. 1-indexed, to match the UI numbering.
 *
 * @param list_selection
 */
namespace.Controller.prototype.printLabel = function (list_selection) {
    var that = this;
    if (isNaN(parseInt(list_selection)) || !isFinite(list_selection)) {
        that.options.message.error("'" + list_selection + "' is not a valid number.");
        return;
    }

    // Attempt to get the selected quote.
    try {
        var quote = that.model.getQuote(parseInt(list_selection) - 1);
    } catch (err) {
        that.options.message.error(err);
        return;
    }

    // If we got here, we have a valid quote. Print its label!
    that.model
        .getLabel(that.model.getPackageID(), quote)
        .done(function (result) {
            if (result.errors) {
                that.options.logger.error(result);
                that.options.message.error("Could not get label! Check logger for details.")
            } else {
                if (quote.company == "USPS") {
                    that.view.updateUspsBalance(result.postage_balance);
                }
                that.model.print("EPL2", result.label);
            }
        });

    // Clear the widget's state.
    that.model.reset();
    that.view.reset();
};

/**
 * Takes an options object, fills in its omitted fields with defaults, and returns it.
 *
 * @param userOptions
 * @returns {*|{}}
 * @private<commit_msg>Preload package dimensions.<commit_after>var namespace = new Namespace("ryepdx.openerp.quickship.kiosk");

/**
 * Coordinates the UI and the server.
 * The model and view should both be primarily inert classes that the controller does things to.
 * The controller sets up UI events and tells the model to do things to the server based on those events.
 * The controller also tells the UI to update itself based on the model's responses.
 *
 * @param model
 * @param view
 * @constructor
 */
namespace.Controller = function (model, view, options) {
    this.view = view;
    this.model = model;
    this.options = this._parseOptions(options);

    this._scale_polling = false;

    this.setupEvents();
    this.initUspsBalance();
    this.activate();
};

/**
 * Kicks off background polling.
 */
namespace.Controller.prototype.activate = function () {
    this._scale_polling = true;
    this._pollScale();
};

/**
 * Stops background polling.
 */
namespace.Controller.prototype.deactivate = function () {
    this._scale_polling = false;
};

/**
 * Wires up View events to Model actions.
 */
namespace.Controller.prototype.setupEvents = function () {
    var that = this;
    that._setupHashChangeEvent();
    that._setupSelectedQuoteChangeEvent();
    that._setupSelectedQuoteOkayButton();
    that._setupSelectedQuoteCancelButton();
    that._setupInputCompleteEvent();
    that._setupBoxCodeSelectedEvent();
    that._setupSaleOrderChangeEvent();
};

/**
 * Set the initial USPS account balance in the View.
 */
namespace.Controller.prototype.initUspsBalance = function () {
    var that = this;

    that.model
        .getUspsAccount()
        .done(function (response) {
            if (response.postage_balance) {
                that.view.updateUspsBalance(response.postage_balance);
            }
            if (response.error) {
                that.options.logger.error("Error retrieving USPS balance: \"" + response.error + "\"");
                that.options.message.error("Could not retrieve USPS balance!");
            }
        });
};

/**
 * Set the shipper ID default value.
 */
namespace.Controller.prototype.initShipperID = function () {
    var that = this;

    that.model
        .getQuickshipID()
        .done(function (quickship_id) {
            that.view.setShipper(quickship_id);
        });
}

/**
 * Takes a number corresponding to the position of a label in the list of
 * quotes and prints the label. 1-indexed, to match the UI numbering.
 *
 * @param list_selection
 */
namespace.Controller.prototype.printLabel = function (list_selection) {
    var that = this;
    if (isNaN(parseInt(list_selection)) || !isFinite(list_selection)) {
        that.options.message.error("'" + list_selection + "' is not a valid number.");
        return;
    }

    // Attempt to get the selected quote.
    try {
        var quote = that.model.getQuote(parseInt(list_selection) - 1);
    } catch (err) {
        that.options.message.error(err);
        return;
    }

    // If we got here, we have a valid quote. Print its label!
    that.model
        .getLabel(that.model.getPackageID(), quote)
        .done(function (result) {
            if (result.errors) {
                that.options.logger.error(result);
                that.options.message.error("Could not get label! Check logger for details.")
            } else {
                if (quote.company == "USPS") {
                    that.view.updateUspsBalance(result.postage_balance);
                }
                that.model.print("EPL2", result.label);
            }
        });

    // Clear the widget's state.
    that.model.reset();
    that.view.reset();
};

/**
 * Tells the Controller's Model to load the package dimensions object into its cache.
 */
namespace.Controller.prototype.preloadPackageDimensions = function () {
    this.model.getPackageDimensions();
}

/**
 * Takes an options object, fills in its omitted fields with defaults, and returns it.
 *
 * @param userOptions
 * @returns {*|{}}
 * @private
{'fraction_non_alphanumeric': 0.11698306824012314, 'fraction_numerical': 0.0007696254489481786, 'mean_word_length': 2.5997229916897506, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '218610', 'n_tokens_mistral': 2201, 'n_tokens_neox': 2157, 'n_words': 583}