(function() { (function(root, factory) { var _cheerio; if (typeof define === 'function' && define.amd) { return define(['_'], factory($)); } else if (typeof module !== "undefined" && module !== null ? module.exports : void 0) { _cheerio = require('cheerio'); return module.exports = factory(_cheerio); } })(this, function($) { var ModelHeader; ModelHeader = (function() { /* @env: 当前显示环境 */ function ModelHeader(_at_data, _at_env, _at_language) { this.data = _at_data; this.env = _at_env != null ? _at_env : 'w'; this.language = _at_language != null ? _at_language : 'en'; this.dimension = { indexes: [], unit: '', customUnit: '' }; } ModelHeader.prototype.build = function() { this.$el = this.buildTitle(); this._cleanTitle(this.$el); return this.$el; }; ModelHeader.prototype.setTitleDimension = function() { var $this, current, html, unit, _i, _len, _ref, _results; if (this.dimension.indexes.length === 0) { return; } unit = this.dimension.customUnit || this.dimension.unit; _ref = this.dimension.indexes; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { current = _ref[_i]; $this = this.$el.parent().find("tr.title th[data-drop='" + current + "']"); if (!$this.length) { continue; } html = $this.html(); _results.push($this.html(html.replace(/\((mm|inch)\)/, "(" + unit + ")"))); } return _results; }; ModelHeader.prototype.getTitle = function(text) { text = text != null ? text.replace(/\[(.+?)\]/g, '$1').replace('\n', '
') : void 0; return text; }; ModelHeader.prototype.isWeightColumn = function(index) { return /weight/i.test(this.data.command[index]); }; ModelHeader.prototype.buildTitle = function() { var html, index, isWeight, mergeRow, subtext, subtitle, text, title, _i, _ref, _ref1, _ref2; subtitle = ''; title = ''; for (index = _i = 0, _ref = this.data.columnCount; 0 <= _ref ? _i < _ref : _i > _ref; index = 0 <= _ref ? ++_i : --_i) { isWeight = this.isWeightColumn(index); subtext = ((_ref1 = this.data.subtitle[this.language]) != null ? _ref1[index] : void 0) || ''; text = ((_ref2 = this.data.title[this.language]) != null ? _ref2[index] : void 0) || ''; text = this.getTitle(text); subtext = this.getTitle(subtext); if (this.data.twoLevelTitle) { mergeRow = ''; } else { mergeRow = ' rowspan="2"'; text = subtext; } if (isWeight) { mergeRow = ' rowspan="2"'; text = ['Weight', '重量'][~~(/cn/i.test(this.language))]; } title += "" + text + ""; if (this.data.twoLevelTitle && !isWeight) { subtitle += "" + subtext + ""; } else { subtitle += ''; } } html = "" + title + "" + subtitle + ""; return $(html); }; ModelHeader.prototype.willShowColumns = function() { var index, result, _i, _ref; result = {}; for (index = _i = 0, _ref = this.data.columnCount; 0 <= _ref ? _i < _ref : _i > _ref; index = 0 <= _ref ? ++_i : --_i) { if (this.ignoreColumn(index)) { continue; } result["f" + index] = ''; } return result; }; ModelHeader.prototype.titleIsEmpty = function(index) { var subtitle, title, _ref, _ref1; title = (_ref = this.data.title[this.language]) != null ? _ref[index] : void 0; subtitle = (_ref1 = this.data.subtitle[this.language]) != null ? _ref1[index] : void 0; return !(title || subtitle); }; ModelHeader.prototype.ignoreColumn = function(index) { var isWeight, regEnv, target, titleIsEmpty; target = this.data.target[index]; titleIsEmpty = this.titleIsEmpty(index); isWeight = this.isWeightColumn(index); if (/all/i.test(this.env)) { if ((target && !titleIsEmpty) || isWeight) { return false; } } regEnv = new RegExp(this.env, "i"); return !regEnv.test(target) || titleIsEmpty; }; ModelHeader.prototype._estimateMergeRow = function($title) { var $subtitleCell, item, merge, _i, _len, _results; merge = this.data.merge[this.language]; _results = []; for (_i = 0, _len = merge.length; _i < _len; _i++) { item = merge[_i]; if (item.y1 !== item.y2) { continue; } $subtitleCell = $title.eq(1).find("th").eq(item.y1); $title.eq(0).find("th").eq(item.y1).attr('rowspan', 2).addClass('merge').attr('data-drop', $subtitleCell.attr('data-drop')); _results.push($subtitleCell.attr('data-remove', 1)); } return _results; }; ModelHeader.prototype._estimateMergeCol = function($title) { var $cell, col, index, item, merge, mergeColAndColspan, self, titles, _i, _j, _len, _ref, _ref1, _ref2, _results; self = this; mergeColAndColspan = function(y1, y2) { var col, mergeCount, mergeIndex, _i; mergeIndex = y1; mergeCount = y2 - y1 + 1; for (col = _i = y1; y1 <= y2 ? _i <= y2 : _i >= y2; col = y1 <= y2 ? ++_i : --_i) { if (self.ignoreColumn(col)) { mergeCount--; if (mergeIndex >= col) { mergeIndex++; } } } return { index: mergeIndex, count: mergeCount }; }; _ref = this.data.merge[this.language]; _results = []; for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { item = _ref[index]; if (item.y1 === item.y2) { continue; } merge = mergeColAndColspan(item.y1, item.y2); if (merge.count === 0) { continue; } for (col = _j = _ref1 = item.y1, _ref2 = item.y2; _ref1 <= _ref2 ? _j <= _ref2 : _j >= _ref2; col = _ref1 <= _ref2 ? ++_j : --_j) { if (col > merge.index) { $title.eq(0).find("th").eq(col).attr('data-remove', 1); } } $cell = $title.eq(0).find("th").eq(merge.index); $cell.attr('colspan', merge.count).addClass('merge'); titles = this.data.title[this.language] || this.data.subtitle[this.language]; _results.push($cell.html(self.getTitle(titles[item.y1]))); } return _results; }; ModelHeader.prototype._estimateRemoveCol = function($title) { var index, _i, _ref, _results; _results = []; for (index = _i = 0, _ref = this.data.columnCount; 0 <= _ref ? _i < _ref : _i > _ref; index = 0 <= _ref ? ++_i : --_i) { if (!this.ignoreColumn(index)) { continue; } $title.eq(0).find("th[data-drop='" + index + "']").attr('data-remove', 1); _results.push($title.eq(1).find("th[data-drop='" + index + "']").attr('data-remove', 1)); } return _results; }; ModelHeader.prototype._cleanTitle = function($title) { this._estimateRemoveCol($title); this._estimateMergeRow($title); this._estimateMergeCol($title); return $title.find('th[data-remove="1"]').remove(); }; return ModelHeader; })(); return { ModelHeader: ModelHeader }; }); }).call(this);