/** * @requires $ jQuery * @requires _ UnderscoreJS */ (function (/* Object */ ns) { 'use strict'; /** * @alias app.ecommerce.order.admin.CartInformation * @constructor */ ns.CartInformation = function () { this.construct.apply(this); }; ns.CartInformation.prototype = { rules: { 'editClick': { 'show': ['.save-item-price', '.cancel-item-price', '.input-item-price'], 'hide': ['.static-item-price', '.error-item-price'] }, 'cancelClick': { 'show': ['.edit-item-price', '.static-item-price'], 'hide': ['.save-item-price', '.input-item-price', '.error-item-price'] }, 'saveClick': { 'success': { 'show':['.edit-item-price', '.static-item-price'], 'hide': ['.error-item-price', '.input-item-price', '.save-item-price', '.cancel-item-price'] }, 'failure': { 'show':['.error-item-price'], 'hide': [] } } }, construct: function(){ this.initEditLinks(); this.initCancelLinks(); this.initSaveLink(); }, initEditLinks: function() { $('.edit-item-price a').click(_.bind(this.processEditClick, this)); }, initCancelLinks: function() { $('.cancel-item-price a').click(_.bind(this.processCancelClick, this)); }, initSaveLink: function() { $('.save-item-price a').click(_.bind(this.processSaveClick, this)); }, processLinkClick: function(e, type) { var link = $(e.currentTarget); link.parent().hide(); var rules = this.rules[type + 'Click']; for (var i in rules.hide) { if (!rules.hide.hasOwnProperty(i)) { continue; } link.parent().siblings(rules.hide[i]).hide() } for (i in rules.show) { if (!rules.show.hasOwnProperty(i)) { continue; } link.parent().siblings(rules.show[i]).show() } }, processEditClick: function(e) { this.processLinkClick(e, 'edit'); }, processCancelClick: function(e) { var cancelLink = $(e.currentTarget); this.processLinkClick(e, 'cancel'); var price = cancelLink.parent().siblings('.static-item-price').text().trim(); price = price.replace('$', ''); $('#itemPrice-' + cancelLink.data('itemId')).val(price); }, processSaveClick: function(e) { var saveLink = $(e.currentTarget); var params = { dataType: 'json', action: 'updateItemPrice', 'itemId': saveLink.data('itemId'), 'price': $('#itemPrice-' + saveLink.data('itemId')).val(), 'orderId': saveLink.data('orderId') }; qs.ajax(qs.constant('BASE_URL') + '/' + qs.constant('CURRENT_PAGE'), params) .done(_.bind(this.onSuccess, this, saveLink)) .fail(_.bind(this.onFail, this, saveLink)); }, onSuccess: function(saveLink, data){ if ('ok' == data.success) { this.updatePrices(saveLink, data); } else { this.showErrors(saveLink, data); } }, onFail: function(saveLink){ this.showErrors(saveLink, {'errors': 'Connection lost. Please try again later.' }); }, processResponse: function(rules, saveLink){ for (var i in rules.hide) { if (!rules.hide.hasOwnProperty(i)) { continue; } saveLink.parent().siblings(rules.hide[i]).hide() } for (i in rules.show) { if (!rules.show.hasOwnProperty(i)) { continue; } saveLink.parent().siblings(rules.show[i]).show() } }, updatePrices: function(saveLink, data) { saveLink.parent().siblings('.static-item-price').html('$' + data.itemPrice); saveLink.parent().siblings('.input-item-price').val(data.itemPrice); $('#item-total-' + saveLink.data('itemId')).html('$' + data.itemTotalPrice); saveLink.parent().hide(); this.processResponse(this.rules.saveClick.success, saveLink); $('.cart_total_sum').html(data.renderedSummary); }, showErrors: function(saveLink, data) { this.processResponse(this.rules.saveClick.failure, saveLink); saveLink.parent().siblings('.error-item-price').html(data.errors); } } })(qs.defineNS('app.ecommerce.order.admin'));