Authored by biao

update for select all feature for cart. code review by LZF

@@ -13,7 +13,10 @@ var dialog = require('../me/dialog'), @@ -13,7 +13,10 @@ var dialog = require('../me/dialog'),
13 tip = require('../plugin/tip'); 13 tip = require('../plugin/tip');
14 14
15 15
16 -var $names; 16 +var $names,
  17 + $selectAllBtn = $('.balance .iconfont');
  18 +
  19 +var requesting = false;
17 20
18 //var $curDelPanel; 21 //var $curDelPanel;
19 22
@@ -163,6 +166,129 @@ $('.cart-goods').on('touchstart', '.checkbox', function() { @@ -163,6 +166,129 @@ $('.cart-goods').on('touchstart', '.checkbox', function() {
163 // }); 166 // });
164 //}) 167 //})
165 168
  169 +function requestUpdateAllGoodsCheckStatus(theGoods, successHandeler) {
  170 + if (requesting) {
  171 + return;
  172 + }
  173 + requesting = true;
  174 + $.ajax({
  175 + url: 'select',
  176 + type: 'post',
  177 + data: {'skuList': JSON.stringify(theGoods)},
  178 + success: function(res) {
  179 + if (res.code === 200) {
  180 + successHandeler();
  181 + } else {
  182 + tip.show(res.message);
  183 + }
  184 + },
  185 + error: function(err) {
  186 + tip.show('网络异常');
  187 + },
  188 + complete: function() {
  189 + requesting = false;
  190 + }
  191 + });
  192 +}
  193 +
  194 +function didUpdateAllGoodsCheckStatus() {
  195 + var $checkedBoxs = $('.shopping-cart-good .icon-cb-checked'),
  196 + $uncheckedBoxs = $('.shopping-cart-good .icon-checkbox');
  197 +
  198 + var shouldSelectAll;
  199 +
  200 + if ($selectAllBtn.hasClass('icon-cb-checked')) {
  201 + $selectAllBtn.removeClass('icon-cb-checked').addClass('icon-checkbox');
  202 + shouldSelectAll = true;
  203 + } else {
  204 + $selectAllBtn.removeClass('icon-checkbox').addClass('icon-cb-checked');
  205 + shouldSelectAll = false;
  206 + }
  207 +
  208 + if (!shouldSelectAll) {
  209 + $uncheckedBoxs.each(function(idx, uncheckedBox) {
  210 + $(uncheckedBox).removeClass('icon-checkbox').addClass('icon-cb-checked');
  211 + });
  212 + } else {
  213 + $checkedBoxs.each(function(idx, checkedBox) {
  214 + $(checkedBox).removeClass('icon-cb-checked').addClass('icon-checkbox');
  215 + });
  216 + }
  217 +}
  218 +
  219 +function bottomCheckBoxHandeler(isSelected, type, handlerAfterTouch) {
  220 + var goodInfo = {};
  221 + var $goods = $('.cart-content:not(.hide) .shopping-cart-good');
  222 + var $good = null;
  223 + var goodsList = [];
  224 +
  225 + function GoodInfo(properties) {
  226 + this.goods_type = properties.goods_type;
  227 + this.buy_number = properties.buy_number;
  228 + this.product_sku = properties.product_sku;
  229 + this.selected = properties.selected;
  230 + }
  231 +
  232 + goodInfo.goods_type = type;
  233 + goodInfo.selected = isSelected ? 'Y': 'N';
  234 +
  235 + $goods.each(function(idx, good) {
  236 + $good = $(good);
  237 +
  238 + goodInfo.product_sku = $(good).data('id');
  239 + goodInfo.buy_number = $good.find('.count').eq(0).text().trim().replace('×', '');
  240 +
  241 + goodsList.push(new GoodInfo(goodInfo));
  242 + });
  243 + console.log(goodsList);
  244 +
  245 + requestUpdateAllGoodsCheckStatus(goodsList, handlerAfterTouch);
  246 +}
  247 +
  248 +function getCartType() {
  249 + var $navItem = $('.cart-nav ').find('li'),
  250 + type = 'ordinary';
  251 +
  252 + if ($navItem.eq(0).hasClass('active')) {
  253 + type = 'ordinary';
  254 + } else {
  255 + type = 'advance';
  256 + }
  257 +
  258 + return type;
  259 +}
  260 +
  261 +function willBeSelected() {
  262 + var isSelected = true;
  263 + var $this = $(this);
  264 +
  265 + if ($this.hasClass('icon-cb-checked')) {
  266 + isSelected = true;
  267 + } else {
  268 + isSelected = false;
  269 + }
  270 +
  271 + return isSelected;
  272 +}
  273 +
  274 +
  275 +
  276 +
  277 +//全选按钮
  278 +$selectAllBtn.on('touchend', function() {
  279 + // var $this = $(this);
  280 +
  281 +
  282 + // if ($this.hasClass('icon-cb-checked')) {
  283 + // $this.removeClass('icon-cb-checked').addClass('icon-checkbox');
  284 + // } else {
  285 + // $this.removeClass('icon-checkbox').addClass('icon-cb-checked');
  286 + // }
  287 +
  288 + bottomCheckBoxHandeler(willBeSelected(), getCartType(), didUpdateAllGoodsCheckStatus);
  289 +});
  290 +
  291 +
166 292
167 $('.btn-balance').on('touchend', function() { 293 $('.btn-balance').on('touchend', function() {
168 window.location.href = '/shoppingCart/orderEnsure?cartType=' + 'ordinary'; 294 window.location.href = '/shoppingCart/orderEnsure?cartType=' + 'ordinary';
@@ -78,7 +78,7 @@ class IndexController extends AbstractAction @@ -78,7 +78,7 @@ class IndexController extends AbstractAction
78 $result = array(); 78 $result = array();
79 79
80 if ($this->isAjax()) { 80 if ($this->isAjax()) {
81 - $productId = $this->post('id', 0); 81 + $productId = $this->post('skuList', 0);
82 $uid = $this->getUid(true); 82 $uid = $this->getUid(true);
83 $shoppingKey = $this->getSession('shoppingKey'); 83 $shoppingKey = $this->getSession('shoppingKey');
84 $result = CartModel::selectGoods($uid, $productId, $shoppingKey); 84 $result = CartModel::selectGoods($uid, $productId, $shoppingKey);