(function() { define(['restful', 'shared/model_header', './model_filter', 'utils', '_', 'shared/utils', 'shared/product', 'i18n'], function(_restful, _ModelHeader, _ModeFilter, _utils, _, _sharedUtils, _sharedProduct, _i18n) { var ProductModel; return ProductModel = (function() { function ProductModel(_at_product_id, _at_env) { this.product_id = _at_product_id; this.env = _at_env != null ? _at_env : 'w'; this.language = _utils.language; this.filterDropdownMenu = true; this.showStandFilter = true; this.condition = { priceType: 'lowest', lang: this.language, get_price: true, get_weight: true, order_id: _utils.getUrlParam('order_id') }; this.data = { condition: [], header: [], group: [], product: {}, models: {}, dimension: {} }; this.header = new _ModelHeader.ModelHeader(); this.header.language = _utils.language; this.header.env = this.env; } ProductModel.prototype.initFilter = function($filter, cb) { var self; self = this; this.filter = new _ModeFilter(this.product_id, $filter); if (!this.showStandFilter) { this.filter.hideStandFilter(); } $('body').bind('filter:group:change', function(event, data) { self.data.condition[data.rowIndex] = { type: 'normal', selected: data.selected }; self.filter.condition = self.combineCondition(); self.filter.build(); return typeof cb === "function" ? cb() : void 0; }); $filter.bind('filter:range:change', function(event, data) { self.data.condition[data.rowIndex] = { type: 'range', min: data.min, max: data.max, fieldIndex: data.fieldIndex }; self.filter.condition = self.combineCondition(); self.filter.build(); return typeof cb === "function" ? cb() : void 0; }); return $filter.bind('filter:group:loaded', function(event, data) { return self.setDropdownMenu(data.rowIndex); }); }; ProductModel.prototype.setDropdownMenu = function(index) { var $cell, self; if (!this.filterDropdownMenu) { return; } self = this; $cell = this.header.$el.find("th[data-drop='" + index + "']"); if ($cell.length === 0 || $cell.attr('data-drop-added') === 'true') { return; } $cell.attr('data-drop-added', 'true'); $cell.append(''); $cell.addClass('drop'); return $cell.bind('click', function() { return self.filter.popFilter($cell, index); }); }; ProductModel.prototype.renderHeaderAndFilter = function($header) { var index, _i, _ref; $header.html(this.header.build()); this.filter.data = this.data.header; this.filter.product = this.data.product; this.filter.willShowColumns = {}; for (index = _i = 0, _ref = this.data.header.columnCount; 0 <= _ref ? _i < _ref : _i > _ref; index = 0 <= _ref ? ++_i : --_i) { if (this.header.titleIsEmpty(index)) { continue; } this.filter.willShowColumns["f" + index] = ''; } return this.filter.build(); }; ProductModel.prototype.renderModel = function(template, $model, $pagination) { $model.html(template(this.data.models)); return $pagination.html(_utils.renderPagination(this.data.models.pagination)); }; ProductModel.prototype.resetWidth = function($o) { var total, width; total = $o.find('tbody>tr:eq(1)>td').length; width = 100; if (total > 15 && total < 20) { width = 120; } else if (total >= 20) { width = 150; } return $o.css('width', width + "%"); }; ProductModel.prototype.getNormalFieldCondition = function(data) { var filterList, filters, key, result, value; result = {}; filters = {}; _.map(data.selected, function(current) { var key, values; if (current.type === 'group') { key = "f" + current.fieldIndex; values = result[key] || []; values.push(current.value); result[key] = values; return; } if (current.type === 'filter') { values = filters[current.group_id] || []; values.push(current.value); filters[current.group_id] = values; } }); for (key in result) { value = result[key]; result[key] = value.join('|'); } filterList = []; for (key in filters) { value = filters[key]; filterList.push(value.join(',')); } if (filterList.length > 0) { result['f0'] = filterList.join('|'); } return result; }; ProductModel.prototype.getRangeFieldCondition = function(data) { var result; result = {}; result["f" + data.fieldIndex] = data.min + "~" + data.max; return result; }; ProductModel.prototype.getRowCondition = function(condition, row) { var field, result, value, _results; result = (function() { switch (row.type) { case 'normal': return this.getNormalFieldCondition(row); case 'range': return this.getRangeFieldCondition(row); default: return void 0; } }).call(this); if (!result) { return; } _results = []; for (field in result) { value = result[field]; _results.push(condition[field] = value); } return _results; }; ProductModel.prototype.combineCondition = function() { var header, index, key, result, row, _i, _ref, _ref1; result = { source: this.env, fields: [] }; header = this.data.header; for (index = _i = 0, _ref = header.columnCount; 0 <= _ref ? _i < _ref : _i > _ref; index = 0 <= _ref ? ++_i : --_i) { if (!this.header.ignoreColumn(index)) { result.fields.push(index); } } _ref1 = this.data.condition; for (key in _ref1) { row = _ref1[key]; this.getRowCondition(result, row); } result.fields = result.fields.join(','); return result; }; ProductModel.prototype.loadProduct = function(cond, cb) { var params, self; self = this; cond = cond || {}; params = { id: this.product_id }; cond.model_filter = true; return _restful.get('product/:id', cond, params, function(result) { self.data.header = JSON.parse(result.model); self.data.product = result; self.header.data = self.data.header; return typeof cb === "function" ? cb() : void 0; }); }; ProductModel.prototype.findDimensionIndexes = function(fieldName) { var index, result, title, titles, _i, _len, _ref; result = { indexes: [], unit: null }; titles = (_ref = this.data.header[fieldName]) != null ? _ref[this.language] : void 0; if (!titles) { return result; } for (index = _i = 0, _len = titles.length; _i < _len; index = ++_i) { title = titles[index]; if (/\((mm|inch)\)/i.test(title)) { result.indexes.push(index); result.unit = RegExp.$1; } } return result; }; ProductModel.prototype.analysisDimension = function() { var dimension, index, item, mergedCells, result, _i, _len; result = this.data.dimension = { customUnit: _utils.getCookie('dimension'), indexes: [], unit: null }; dimension = this.findDimensionIndexes('title'); if (dimension.indexes.length === 0) { dimension = this.findDimensionIndexes('subtitle'); this.data.dimension.indexes = dimension.indexes; this.data.dimension.unit = dimension.unit; return this.data.dimension; } result.unit = dimension.unit; mergedCells = this.data.header.merge[this.language]; _.map(dimension.indexes, function(current) { var isMerge; isMerge = _.find(mergedCells, function(item) { return item.y1 !== item.y2 && current >= item.y1 && current <= item.y2; }); if (!isMerge) { return result.indexes.push(current); } }); for (index = _i = 0, _len = mergedCells.length; _i < _len; index = ++_i) { item = mergedCells[index]; if (item.y1 === item.y2) { continue; } _.map(dimension.indexes, function(current) { var _j, _ref, _ref1, _results; if (current >= item.y1 && current <= item.y2) { return result.indexes = result.indexes.concat((function() { _results = []; for (var _j = _ref = item.y1, _ref1 = item.y2; _ref <= _ref1 ? _j <= _ref1 : _j >= _ref1; _ref <= _ref1 ? _j++ : _j--){ _results.push(_j); } return _results; }).apply(this)); } }); } result.indexes = _.uniq(result.indexes); return this.data.dimension = result; }; ProductModel.prototype.convertDimension = function(items) { var customUnit, dividend, indexes; indexes = this.data.dimension.indexes; if (indexes.length === 0) { return; } customUnit = _utils.getCookie('dimension'); if (!customUnit || customUnit === this.data.dimension.unit) { return; } dividend = 25.4; return _.map(this.data.models.items, function(item) { var index, value, _i, _len, _results; _results = []; for (_i = 0, _len = indexes.length; _i < _len; _i++) { index = indexes[_i]; value = item.field["f" + index]; if (!value || isNaN(value = parseFloat(value))) { continue; } value = customUnit === 'mm' ? value * dividend : value / dividend; _results.push(item.field["f" + index] = value.toFixed(value > 1 ? 2 : 3)); } return _results; }); }; ProductModel.prototype.setDimension = function() { this.analysisDimension(); this.data.dimension.customUnit = _utils.getCookie('dimension'); this.convertDimension(); this.header.dimension = this.data.dimension; this.header.setTitleDimension(); this.filter.dimension = this.data.dimension; return this.filter.setDimensionText(); }; ProductModel.prototype.loadModel = function(pageIndex, cb) { var api, cond, params, self; if (pageIndex == null) { pageIndex = 1; } self = this; cond = this.combineCondition(); this.filter.condition = cond; cond.pageIndex = pageIndex; cond.model_id = this.model_id; _.extend(cond, this.condition); params = { product_id: this.product_id }; api = 'product/:product_id/model'; return _restful.get(api, cond, params, function(result) { _sharedProduct.analyseUnitIndex(result.items); _.map(result.items, function(item) { var uoq, uow; item.price = _sharedUtils.toCurrency(item.price); if (!item.specialUnit) { return; } uoq = self.data.product.unit_of_quantity; if (!/\//.test(uoq)) { uoq = _i18n.unit[uoq] || uoq; } uow = self.data.product.unit_of_weight; item.unitPriceText = _sharedUtils.unitOfQuantity(uoq, item.unit, true, self.language); item.unitQuantityText = _sharedUtils.unitOfQuantity(uoq, item.unit, false, self.language); return item.unitWeightText = uow + "/" + item.unitQuantityText; }); result.header = self.data.header; self.data.models = result; return typeof cb === "function" ? cb() : void 0; }); }; return ProductModel; })(); }); }).call(this);