UNPKG

fd-gulp-convert-encoding

Version:

convert file to assigned charset

340 lines (276 loc) 10.6 kB
/** * purchasingList view交互逻辑 */ define('detail.modules.purchasingList.View', ['jQuery', 'Class', 'detail.core.Wing', 'detail.core.Event', 'detail.lib.orderValidator.OrderValidator', 'detail.lib.amountControl.AmountControl', 'detail.modules.purchasingList.SkuSelector', 'lofty.ui.Scroller'], function($, Class, Wing, Event, OrderValidator, AmountControl, SkuSelector, Scroller) { return Class({ init: function(view, context) { this.onEvent = $.os.supportsTouch ? 'tap' : 'click'; this.div = view; this.config = context; this.purchasingList = $('div.obj-purchasing-list', this.div); this.totalAmount = 0; this.totalPrice = 0; this.skuMap = {}; this.skuSubmitData = []; if (this.config.isSKUOffer === 'true'){ this._initSkuSelector(this.div, this.config); this._actionSheet(); this._bindEvent(); this._initSkuScroller(); this._initSkuListScroller(); } else { this._initSingle(this.div, this.config); } }, _bindEvent: function(){ var that = this; Event.on('hidePurchasingFullInfo', function(){ that.div.css3Animate({ y: -120, time: 200 }); }); Event.on('showPurchasingFullInfo', function(){ that.div.css3Animate({ y: 0, time: 200 }); }); }, _initSingle: function(view, context){ var validator = new OrderValidator(context), $objOrder = $('div.obj-order', view), amountControl, that = this; amountControl = new AmountControl($objOrder, context, validator); amountControl.setCallback(function(amount, validated){ if (!validated){ return; } that.totalAmount = amount; that.totalPrice = that._getCurRangePrice(amount) * amount; that._renderTotalPrice(that.totalAmount, that.totalPrice); Event.trigger('setPurchasingData', { totalAmount: that.totalAmount, totalPrice: that.totalPrice }); }); }, _initSkuSelector: function(view, context){ var validator = new OrderValidator(context), that = this; this.skuSelector = new SkuSelector(view, context, validator); this.skuSelector.setCallback(function(targetData, totalAmount, skuMap){ // if (targetData.specId && targetData.specId !== ''){ // that.skuSubmitData.push({'specId':targetData.specId,'amount': totalAmount}); // } // 加入purchasingList that.skuMap = skuMap; that._renderPurchasingList(skuMap); that._renderSkuTotalPrice(skuMap); }); }, _renderPurchasingList: function(skuMap){ var $content = $('div.obj-content', this.purchasingList), itemList = {}, html = '', arrUl = [], that = this; itemList = this._reformRenderData(skuMap); $.each(itemList, function(k, v){ arrUl.push('<ul class="sku-list">'); $.each(v, function(index, value){ var arrLi = []; arrLi.push('<li data-max="' + value.canBookCount + '" data-spec="' + value.specId + '">'); arrLi.push('<p class="sku-name">' + value.name.replace('&gt;', '') + '</p>'); arrLi.push('<div class="unit-d-amount-control">'); arrLi.push('<a href="#" class="amount-down">-</a>'); arrLi.push('<input type="text" onfocus="this.type=\'number\'" onblur="this.type=\'text\'" maxlength="10" class="amount-input" value="' + value.amount + '">'); arrLi.push('<a href="#" class="amount-up">+</a>'); arrLi.push('</div>'); arrLi.push('<a href="#" class="link-remove">删除</a>'); arrLi.push('</li>'); arrUl.push(arrLi.join('')); }); arrUl.push('</ul>'); }); html = arrUl.join(''); $content.html(html); this._showListTrigger(); this._bindAmountControl(); this._bindRemoveItem(); }, _renderSkuTotalPrice: function(skuMap){ var price, that = this; this.totalAmount = 0; this.totalPrice = 0; $.each(skuMap, function(k, v){ that.totalAmount += v.amount; }); if (this.config.isRangePriceSku === 'true'){ price = this._getCurSkuRangePrice(this.totalAmount); this.totalPrice = this.totalAmount * price * 1; } else { $.each(skuMap, function(k, v){ price = v.discountPrice || v.price; that.totalPrice += v.amount * price * 1; }); } this._renderTotalPrice(this.totalAmount, this.totalPrice); Event.trigger('setPurchasingData', { totalAmount: this.totalAmount, totalPrice: this.totalPrice, skuMap: skuMap }); }, _getCurSkuRangePrice: function(amount){ var priceRange = this.config.skuDiscountPriceRanges, price; price = priceRange[0][1]; for (var i = priceRange.length; i > 0; i--){ if (amount >= priceRange[i - 1][0]){ price = priceRange[i - 1][1]; break; } } return price; }, _getCurRangePrice: function(amount){ var priceRange = this.config.discountPriceRanges, price; price = priceRange[0].price; for (var i = priceRange.length; i > 0; i--){ if (amount >= priceRange[i - 1].begin * 1){ price = priceRange[i - 1].convertPrice ? priceRange[i - 1].convertPrice : priceRange[i - 1].price; break; } } return price; }, _renderTotalPrice: function(totalAmount, totalPrice){ var $amount = $('p.amount .value', this.purchasingList), $price = $('p.price .value', this.purchasingList); $amount.text(totalAmount); $price.text(totalPrice.toFixed(2)); }, _reformRenderData: function(skuMap){ var itemList = {}; $.each(skuMap, function(k, v){ var itemName = ''; if (v.amount && v.amount > 0){ itemName = v.name.split('&gt;')[0]; if (!itemList[itemName]) { itemList[itemName] = []; } itemList[itemName].push(v); } }); return itemList; }, _showListTrigger: function(){ var $content = $('div.obj-content', this.purchasingList), $showup = $('p.price a', this.purchasingList); if ($('ul', this.purchasingList).length){ $showup.removeClass('visibility-hide'); } else { $showup.addClass('visibility-hide'); } }, _bindAmountControl: function(){ var $items = $('ul.sku-list li', this.purchasingList), config = this.config, that = this; $items.each(function(i){ var validator, amountControl, elm = $(this), max = elm.data('max'), specId = elm.data('spec'); validator = new OrderValidator(config); validator.setMax(max); amountControl = new AmountControl(elm, config, validator); amountControl.setCallback(function(amount, validated){ if (!validated){ return; } that.skuMap[specId].amount = amount; that._renderSkuTotalPrice(that.skuMap); }); }); }, _bindRemoveItem: function(){ var $links = $('a.link-remove', this.purchasingList), that = this; $links.on(this.onEvent, function(e){ e.preventDefault(); var elm = $(this), $li = elm.closest('li'), specId = $li.data('spec'); $li.remove(); that.skuMap[specId].amount = 0; that._renderSkuTotalPrice(that.skuMap); }); }, _actionSheet: function(){ var $list = $('div.obj-content', this.purchasingList), $header = $('div.obj-header', this.purchasingList), $linkShow = $('p.price a', this.purchasingList), that = this; $('div.obj-header', this.purchasingList).on(this.onEvent, function(e){ e.preventDefault(); if (!$('ul', that.purchasingList).length){ return; } if ($linkShow.hasClass('d-showup')){ $list.show(); $linkShow.removeClass('d-showup').addClass('d-showdown'); $header.removeClass('box-shadow'); that._addMask(); that._resetSpecOperator(); } else { $list.hide(); $header.addClass('box-shadow'); that._removeMask(); $linkShow.removeClass('d-showdown').addClass('d-showup'); } }); }, _resetSpecOperator: function(){ var $ops = $('dl.unit-d-sku-selector a', this.div), that = this; $ops.each(function(i){ var elm = $(this); if (elm.hasClass('fui-btn-checked')){ elm.trigger(that.onEvent); } }); }, _initSkuScroller: function(){ var ratio = this.config.ratio || window.devicePixelRatio, skuMaxHeight = document.documentElement.clientHeight - 330, $objSkuSelector = $('#obj-sku-selector', this.div); setTimeout(function(){ if ($objSkuSelector.height() > skuMaxHeight){ $objSkuSelector.height(skuMaxHeight); var myScroll = new Scroller({ container: '#obj-sku-selector', checkDOMChanges: true }); } }, 200); }, _initSkuListScroller: function(){ var ratio = this.config.ratio || window.devicePixelRatio, skuListMaxHeight = document.documentElement.clientHeight - 240; }, _addMask: function(){ this.div.append('<div class="mask"></div>'); }, _removeMask: function(){ $('div.mask', this.div).remove(); } }); });