/*
原则:
*1表示分组筛选,如果只有一个分组,则不需要显示
*0表示分组+范围,如果分组超过10,则出现范围筛选,否则分组就可以了
*/
(function() {
var __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
__hasProp = {}.hasOwnProperty;
define(['restful', './ProductModel', 't!/views/product/product.html', 'utils', './basket', '_', 'async', 'i18n', 'shared/utils'], function(_restful, _ProductModel, _template, _utils, _basket, _, _async, _i18n, _sharedUtils) {
var Product;
return Product = (function(_super) {
__extends(Product, _super);
function Product() {
document.oncontextmenu = function() {
return false;
};
document.onselectstart = function() {
return false;
};
if (/product\/(\d+)\.html/.test(location.href)) {
this.product_id = RegExp.$1;
}
Product.__super__.constructor.call(this, this.product_id, 'w');
this.model_id = _utils.getUrlParam('model_id');
this.showStandFilter = !this.model_id;
this.filterDropdownMenu = !this.model_id;
this.initTemplates(_template);
this.initElement();
this.loadData();
}
Product.prototype.initTemplates = function(text) {
var $tepl;
$tepl = $(text);
return this.templates = {
model: Handlebars.compile($tepl.find('.models').val()),
basket: Handlebars.compile($tepl.find('.basket').val()),
filter: Handlebars.compile($tepl.find('.filter-item').val())
};
};
Product.prototype.renderHeaderAndFilter = function($header) {
var qtyHtml;
Product.__super__.renderHeaderAndFilter.apply(this, arguments);
qtyHtml = "
" + _i18n.product.price + " | " + _i18n.product.qty + " | ";
return this.header.$el.eq(0).append(qtyHtml);
};
Product.prototype.renderModel = function() {
var $els;
$els = this.elements.productModel;
Product.__super__.renderModel.call(this, this.templates.model, $els.tbody, $els.pagination);
return this.bindEvent();
};
Product.prototype.loadData = function() {
var $els, self;
$els = this.elements.productModel;
self = this;
return _async.series({
product: function(done) {
return self.loadProduct(null, done);
},
model: function(done) {
return self.loadModel(1, done);
}
}, function() {
self.loadBasket(function() {
return self.renderSidebar();
});
self.renderHeaderAndFilter($els.thead);
self.setDimension();
return self.renderModel();
});
};
Product.prototype.updateLowestPrice = function(model_id, qty) {
var $o, params, url;
$o = this.elements.productModel.tbody;
url = "quotation/lowest";
params = {
model_id: model_id,
qty: qty
};
return _restful.get(url, params, function(result) {
var $row, price;
price = result && result.length > 0 ? result[0].price : 0;
price = (price && _sharedUtils.toCurrency(price)) || '';
$row = $o.find("tr[data-model-id='" + model_id + "']");
return $row.find('td.price').text(price || '');
});
};
Product.prototype.fillQuantity = function() {
var $o, $row, item, _i, _len, _ref, _results;
$o = this.elements.productModel.tbody;
_ref = this.data.basket;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
item = _ref[_i];
this.updateLowestPrice(item.model_id, item.qty);
$row = $o.find("tr[data-model-id='" + item.model_id + "']");
if ($row.length === 0) {
continue;
}
_results.push($row.addClass('full').attr('data-qty', item.qty || '').find('input').val(item.qty || ''));
}
return _results;
};
Product.prototype.initElement = function() {
var $els, $productModel, self;
self = this;
this.initFilter($('#filter'), function() {
return self.loadModelAndRender();
});
$productModel = $('#product-model');
$els = this.elements = {
sidebar: $('#sidebar'),
body: $('body'),
filter: $('#filter'),
sidebarFilter: $('#sidebarFilter'),
basketItems: $('#basketItems'),
basketItemsCount: $('#basketItemsCount'),
allColumnButton: $('#btnAllColumn'),
productModel: {
pagination: $productModel.find('.pagination'),
container: $productModel,
thead: $productModel.find('table thead'),
tbody: $productModel.find('table tbody'),
table: $productModel.find('table')
}
};
this.filter.elements.container.bind('product:dimension:changed', function() {
return self.loadModelAndRender();
});
$els.productModel.pagination.bind('page:change', function(event, data) {
return self.loadModelAndRender(data.pageIndex);
});
$els.productModel.tbody.bind('onFriendlyKey', function(event, $target, type) {
var $input, $next, $row;
if (type === 'left' || type === 'right') {
return;
}
$row = $target.closest('tr');
$next = (function() {
switch (type) {
case 'enter':
case 'down':
return $row.next();
case 'up':
return $row.prev();
}
})();
if (!$next) {
return;
}
$input = $next.find('td.qty input');
return $input.focus();
});
return this.initAllColumnEvent();
};
Product.prototype.initAllColumnEvent = function() {
var $el, flags, self;
$el = this.elements.allColumnButton;
flags = ['less', 'more'];
self = this;
return $el.bind('click', function() {
var isLess;
isLess = $el.data('flag') === 'less';
$el.data('flag', flags[Number(isLess)]);
$el.find("span." + flags[Number(isLess)]).show();
$el.find("span." + flags[Number(!isLess)]).hide();
return self.displayColumn(!isLess);
});
};
Product.prototype.displayColumn = function(showAll) {
var $content, $sidebar, contentWidth;
$sidebar = $('#sidebar');
$content = $('#product-model');
contentWidth = 1170;
if (showAll) {
this.header.env = this.env = 'all';
$sidebar.hide();
} else {
this.header.env = this.env = 'w';
contentWidth = 870;
$sidebar.show();
}
$content.width(contentWidth);
return this.loadData();
};
Product.prototype.syncCheckboxStatus = function($o, fieldIndex, selectedList) {
var $field;
$field = $o.find("div[data-index='" + fieldIndex + "']");
return $field.find('input:checkbox').each(function() {
var item, value, _i, _len;
value = $(this).val();
for (_i = 0, _len = selectedList.length; _i < _len; _i++) {
item = selectedList[_i];
if (item.value === value) {
return this.checked = true;
}
}
this.checked = false;
return true;
});
};
Product.prototype.getTitle = function(index) {
var title, _ref, _ref1;
if (index === 0) {
return _i18n.product.modelNoFilter;
}
title = (_ref = this.data.header.subtitle[_utils.language]) != null ? _ref[index] : void 0;
title = title || ((_ref1 = this.data.header.title[_utils.language]) != null ? _ref1[index] : void 0);
if (title) {
return _utils.subHandler(title);
}
};
Product.prototype.renderSidebar = function() {
var $els, basket, html;
$els = this.elements;
$els.basketItemsCount.text(this.data.basket.length);
basket = this.data.basket.slice(0, 5);
html = this.templates.basket(basket);
return $els.basketItems.html(html);
};
Product.prototype.bindEvent = function() {
var $inputs, $o, self;
self = this;
$o = this.elements.productModel.tbody;
$inputs = $o.find('input');
$inputs.bind('focus', function(e) {
var $row;
this.select();
$o.find('>tr.active').removeClass('active');
$row = $(this).closest('tr');
return $row.addClass('active');
});
_utils.initFriendlyKey($inputs);
$inputs.bind('value:change', function() {
var $row, unit, value;
$row = $(this).parents('tr');
value = parseFloat(this.value);
if (isNaN(value)) {
value = 0;
}
unit = $row.attr('data-unit');
if (unit !== '1000') {
value = parseInt(value);
}
this.value = value || '';
$row.toggleClass('full', value > 0);
return self.quantityChanged($row, value);
});
$inputs.bind('blur', function() {
return $(this).trigger('value:change');
});
return $o.find('.qty .reset').bind('click', function(e) {
e.preventDefault();
return $(this).parent().find('input').val('').trigger('value:change');
});
};
Product.prototype.quantityChanged = function($row, qty) {
var data, fn, model_id, model_no, old, self;
self = this;
old = parseFloat($row.attr('data-qty'));
if (isNaN(old)) {
old = 0;
}
if (old === qty) {
return;
}
model_no = $row.attr('data-model-no');
model_id = $row.attr('data-model-id');
data = {
model_id: model_id,
model_no: model_no,
product_id: this.product_id,
qty: qty
};
fn = qty === 0 ? 'removeAtBasket' : 'addToBasket';
return _basket[fn](data, function() {
$row.attr('data-qty', qty);
self.updateLowestPrice(data.model_id, qty);
return self.loadBasket(function() {
return self.renderSidebar();
});
});
};
Product.prototype.loadModelAndRender = function(pageIndex) {
var self;
if (pageIndex == null) {
pageIndex = 1;
}
self = this;
return this.loadModel(pageIndex, function() {
self.setDimension();
self.renderModel();
self.renderSidebar();
self.fillQuantity();
return self.resetWidth(self.elements.productModel.table);
});
};
Product.prototype.loadBasket = function(cb) {
var self;
self = this;
return _basket.simpleBasket(9999, this.product_id, function(result) {
self.data.basket = result;
self.fillQuantity();
return typeof cb === "function" ? cb() : void 0;
});
};
Product.prototype.loadBasketWithProduct = function(cb) {
var self;
self = this;
return _basket.simpleBasket(5, this.product_id, function(result) {
self.data.basketWithProduct = result;
return typeof cb === "function" ? cb() : void 0;
});
};
return Product;
})(_ProductModel);
});
}).call(this);