Skip to content

Commit

Permalink
Allow populating all data (subtotal, tax, shipping) from storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jsor committed Aug 18, 2016
1 parent 5e83d78 commit 4abbbbb
Show file tree
Hide file tree
Showing 15 changed files with 354 additions and 227 deletions.
186 changes: 108 additions & 78 deletions dist/carty.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Carty - v0.6.0 - 2016-01-08
* Carty - v0.6.0 - 2016-08-18
* http://sorgalla.com/carty/
* Copyright (c) 2015-2016 Jan Sorgalla; Licensed MIT
*/
Expand All @@ -12,7 +12,7 @@
exports["carty"] = factory(require("jquery"));
else
root["carty"] = factory(root["jQuery"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_14__) {
})(this, function(__WEBPACK_EXTERNAL_MODULE_13__) {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
Expand Down Expand Up @@ -64,16 +64,16 @@ return /******/ (function(modules) { // webpackBootstrap
var carty = __webpack_require__(1);

carty.format = {
currency: __webpack_require__(8),
number: __webpack_require__(10)
currency: __webpack_require__(7),
number: __webpack_require__(9)
};

carty.storage = {
localStorage: __webpack_require__(12)
localStorage: __webpack_require__(11)
};

carty.ui = {
jquery: __webpack_require__(13)
jquery: __webpack_require__(12)
};

module.exports = carty;
Expand All @@ -91,7 +91,6 @@ return /******/ (function(modules) { // webpackBootstrap
var emitter = __webpack_require__(3);
var toNumber = __webpack_require__(5);
var options = __webpack_require__(6);
var value = __webpack_require__(7);
var type = __webpack_require__(4);

var resolve = Promise.resolve.bind(Promise);
Expand All @@ -104,8 +103,12 @@ return /******/ (function(modules) { // webpackBootstrap
return previous + (item.price * item.quantity);
}, 0);
},
shipping: null,
tax: null
shipping: function() {
return 0;
},
tax: function() {
return 0;
}
};

var _defaultItemAttributes = {
Expand All @@ -116,21 +119,22 @@ return /******/ (function(modules) { // webpackBootstrap
function cart() {
return {
size: cart.size(),
quantity: cart.quantity(),
subtotal: cart.subtotal(),
shipping: cart.shipping(),
tax: cart.tax(),
quantity: cart.quantity(),
total: cart.total(),
items: _items.map(function(item) {
return item();
})
items: items()
};
}

var emit = emitter(cart);

var _options = extend({}, _defaultOptions, opts);
var _items = [];
var _options = extend({}, _defaultOptions, opts);
var _items = [];
var _subtotal = undefined;
var _shipping = undefined;
var _tax = undefined;
var _ready;

cart.on(['load', 'add', 'update', 'remove', 'clear', 'checkout'], emit.bind(undefined, 'change'));
Expand Down Expand Up @@ -223,34 +227,34 @@ return /******/ (function(modules) { // webpackBootstrap
return cart;
};

cart.quantity = function() {
return _items.reduce(function(previous, item) {
return previous + item().quantity;
}, 0);
};

cart.subtotal = function() {
var items = _items.map(function(item) {
return item();
});
if (type(_subtotal) !== 'undefined') {
return _subtotal;
}

return toNumber(value(_options.subtotal, undefined, [items, cart]), _options);
return toNumber(_options.subtotal.call(undefined, items(), cart));
};

cart.shipping = function() {
if (!cart.size()) {
return 0;
if (type(_shipping) !== 'undefined') {
return _shipping;
}

return toNumber(value(_options.shipping, undefined, [cart]), _options);
return toNumber(_options.shipping.call(undefined, cart));
};

cart.tax = function() {
if (!cart.size()) {
return 0;
if (type(_tax) !== 'undefined') {
return _tax;
}

return toNumber(value(_options.tax, undefined, [cart]), _options);
return toNumber(_options.tax.call(undefined, cart));
};

cart.quantity = function() {
return _items.reduce(function(previous, item) {
return previous + item().quantity;
}, 0);
};

cart.total = function() {
Expand Down Expand Up @@ -285,25 +289,58 @@ return /******/ (function(modules) { // webpackBootstrap
});
}

function populate(data) {
if (type(data) !== 'object') {
return;
}

if (type(data.items) === 'array') {
_items = data.items.map(function(attr) {
return createItem(attr);
});
}

if (type(data.subtotal) !== 'undefined') {
_subtotal = toNumber(data.subtotal);
}

if (type(data.shipping) !== 'undefined') {
_shipping = toNumber(data.shipping);
}

if (type(data.tax) !== 'undefined') {
_tax = toNumber(data.tax);
}
}

function reset() {
_items.length = 0;
_subtotal = undefined;
_shipping = undefined;
_tax = undefined;
}

function load() {
return emit('load').then(function() {
return resolve(
_options.storage && _options.storage.load()
).then(function(items) {
if (type(items) === 'array') {
_items = items.map(function(attr) {
return createItem(attr);
});
}
).then(function(data) {
populate(data);
}).then(emit.bind(cart, 'loaded'), function(e) {
emit('loadfailed', e);
return reject(e);
emit('loadfailed', e);
return reject(e);
});
}, function() {
// Catch load event listener rejections
});
}

function items() {
return _items.map(function(item) {
return item();
})
}

function has(attr) {
var checkItem;
var found = false;
Expand Down Expand Up @@ -344,7 +381,9 @@ return /******/ (function(modules) { // webpackBootstrap

return resolve(
_options.storage && _options.storage.put(state, cart)
).then(emit.bind(cart, 'updated', state), function(e) {
).then(function(data) {
populate(data);
}).then(emit.bind(cart, 'updated', state), function(e) {
emit('updatefailed', e, state);
return reject(e);
})['catch'](function() {
Expand Down Expand Up @@ -382,7 +421,9 @@ return /******/ (function(modules) { // webpackBootstrap

return resolve(
_options.storage && _options.storage.put(state, cart)
).then(emit.bind(cart, 'added', state), function(e) {
).then(function(data) {
populate(data);
}).then(emit.bind(cart, 'added', state), function(e) {
emit('addfailed', e, state);
return reject(e);
})['catch'](function() {
Expand All @@ -407,7 +448,9 @@ return /******/ (function(modules) { // webpackBootstrap

return resolve(
_options.storage && _options.storage.remove(state, cart)
).then(emit.bind(cart, 'removed', state), function(e) {
).then(function(data) {
populate(data);
}).then(emit.bind(cart, 'removed', state), function(e) {
emit('removefailed', e, state);
return reject(e);
})['catch'](function() {
Expand All @@ -420,11 +463,15 @@ return /******/ (function(modules) { // webpackBootstrap

function clear() {
return emit('clear').then(function() {
_items.length = 0;

return resolve(
_options.storage && _options.storage.clear()
).then(emit.bind(cart, 'cleared'), function(e) {
).then(function(data) {
if (type(data) !== 'undefined') {
populate(data);
} else {
reset();
}
}).then(emit.bind(cart, 'cleared'), function(e) {
emit('clearfailed', e);
return reject(e);
})['catch'](function() {
Expand All @@ -441,7 +488,7 @@ return /******/ (function(modules) { // webpackBootstrap
_options.storage && _options.storage.checkout(data, cart)
)
.then(function(value) {
_items.length = 0;
reset();

return value;
})
Expand Down Expand Up @@ -889,30 +936,11 @@ return /******/ (function(modules) { // webpackBootstrap

'use strict';

module.exports = value;

var type = __webpack_require__(4);

function value(value, context, args) {
if (type(value) === 'function') {
return value.apply(context, args || []);
}

return value;
}


/***/ },
/* 8 */
/***/ function(module, exports, __webpack_require__) {

'use strict';

module.exports = formatCurrency;

var extend = __webpack_require__(2);
var defaultCurrencies = __webpack_require__(9);
var formatNumber = __webpack_require__(10);
var defaultCurrencies = __webpack_require__(8);
var formatNumber = __webpack_require__(9);

function formatCurrency(value, options) {
return _formatCurrency(options, value);
Expand Down Expand Up @@ -940,7 +968,7 @@ return /******/ (function(modules) { // webpackBootstrap


/***/ },
/* 9 */
/* 8 */
/***/ function(module, exports) {

'use strict';
Expand Down Expand Up @@ -1012,15 +1040,15 @@ return /******/ (function(modules) { // webpackBootstrap


/***/ },
/* 10 */
/* 9 */
/***/ function(module, exports, __webpack_require__) {

'use strict';

module.exports = formatNumber;

var toNumber = __webpack_require__(5);
var toFixed = __webpack_require__(11);
var toFixed = __webpack_require__(10);
var type = __webpack_require__(4);

function formatNumber(value, options) {
Expand Down Expand Up @@ -1079,7 +1107,7 @@ return /******/ (function(modules) { // webpackBootstrap


/***/ },
/* 11 */
/* 10 */
/***/ function(module, exports) {

'use strict';
Expand All @@ -1103,7 +1131,7 @@ return /******/ (function(modules) { // webpackBootstrap


/***/ },
/* 12 */
/* 11 */
/***/ function(module, exports) {

'use strict';
Expand All @@ -1125,9 +1153,11 @@ return /******/ (function(modules) { // webpackBootstrap
return {
load: function() {
try {
return JSON.parse(localStorage.getItem(namespace));
return {
items: JSON.parse(localStorage.getItem(namespace))
};
} catch (e) {
return []
return {}
}
},
put: save,
Expand All @@ -1139,14 +1169,14 @@ return /******/ (function(modules) { // webpackBootstrap


/***/ },
/* 13 */
/* 12 */
/***/ function(module, exports, __webpack_require__) {

'use strict';

module.exports = uiJquery;

var $ = __webpack_require__(14);
var $ = __webpack_require__(13);

var _defaultOptions = {
namespace: 'carty',
Expand Down Expand Up @@ -1362,10 +1392,10 @@ return /******/ (function(modules) { // webpackBootstrap


/***/ },
/* 14 */
/* 13 */
/***/ function(module, exports) {

module.exports = __WEBPACK_EXTERNAL_MODULE_14__;
module.exports = __WEBPACK_EXTERNAL_MODULE_13__;

/***/ }
/******/ ])
Expand Down
Loading

0 comments on commit 4abbbbb

Please sign in to comment.