Advertisement
Guest User

Untitled

a guest
Mar 16th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Loop from '/DateTime/Timer/Loop';
  2. import Serialize from '/Support/Serialize';
  3. import Collection from '/Support/Collection';
  4. import Repository from '/Storage/Repository';
  5. import CartItemObject from '/App/Cart/CartItemObject';
  6. import PeriodicTimer from '/DateTime/Timer/PeriodicTimer';
  7. import {default as AsyncRepository} from '/ActiveRecord/Repository';
  8. import Serializable, {toObject} from '/Support/Interfaces/Serializable';
  9. import LocalStorageAdapter from '/Storage/Adapters/LocalStorageAdapter';
  10.  
  11.  
  12. /**
  13.  * Cart instance
  14.  * @TODO Refactor me
  15.  */
  16. export default class Cart extends Serializable {
  17.     /**
  18.      * @type {Repository}
  19.      * @private
  20.      */
  21.     _repository = new Repository(new LocalStorageAdapter('cart:'));
  22.  
  23.     /**
  24.      * @type {KnockoutObservableArray<T>}
  25.      * @private
  26.      */
  27.     _items = ko.observableArray([]);
  28.  
  29.     /**
  30.      * @constructor
  31.      */
  32.     constructor() {
  33.         super();
  34.  
  35.         this.refresh();
  36.  
  37.         var loop = new Loop;
  38.         loop
  39.             .subscribe((timer:PeriodicTimer) => {
  40.                 this.refresh();
  41.             })
  42.             .everySecond()
  43.             .withoutOverlapping();
  44.  
  45.         loop.start();
  46.     }
  47.  
  48.     /**
  49.      * @returns {KnockoutObservableArray.<T>}
  50.      */
  51.     get items() {
  52.         return this._items;
  53.     }
  54.  
  55.     /**
  56.      * TODO optimize items diff
  57.      * @returns {Cart}
  58.      */
  59.     refresh() {
  60.         var all      = new Collection(this.all());
  61.         var rendered = new Collection(this._items());
  62.  
  63.         // Add from rendered
  64.         all.each(item => {
  65.             var exists = rendered.find(i => i.buyId === item.buyId).first();
  66.             if (!exists) {
  67.                 this._items.push(item);
  68.             }
  69.         });
  70.  
  71.         // Remove from rendered
  72.         rendered.each(item => {
  73.             var exists = all.find(i => i.buyId === item.buyId).first();
  74.             if (!exists) {
  75.                 this._items.remove(i => i.buyId === item.buyId);
  76.             }
  77.         });
  78.  
  79.         return this;
  80.     }
  81.  
  82.     /**
  83.      * @param {string} uuid
  84.      * @returns {boolean}
  85.      */
  86.     contains(uuid:string):boolean {
  87.         return !!this.get(uuid);
  88.     }
  89.  
  90.     /**
  91.      * @param {string} uuid
  92.      * @returns {boolean}
  93.      */
  94.     remove(uuid:string):boolean {
  95.         var item = this.get(uuid);
  96.         if (item) {
  97.             this._repository.remove(item.buyId);
  98.             this.refresh();
  99.             return true;
  100.         }
  101.         return false;
  102.     }
  103.  
  104.     /**
  105.      * @param {string} uuid
  106.      * @returns {CartItemObject}
  107.      */
  108.     get(uuid:string):CartItemObject {
  109.         return (new Collection(this.all()))
  110.             .where('uuid', uuid)
  111.             .first();
  112.     }
  113.  
  114.     /**
  115.      * @param {object} cartObject
  116.      */
  117.     add(cartObject = {}):Cart {
  118.         if (!cartObject[toObject]) {
  119.             throw new Error('Can not add object to cart');
  120.         }
  121.  
  122.         var data = cartObject[toObject]();
  123.         var item = new CartItemObject(data);
  124.  
  125.         this._repository.set(item.buyId, item[toObject]());
  126.  
  127.         this.refresh();
  128.  
  129.         return this;
  130.     }
  131.  
  132.     /**
  133.      * @returns {Array}
  134.      */
  135.     all():Array {
  136.         var items = [];
  137.         var data  = this._repository.all();
  138.  
  139.         Object.keys(data).forEach(key => {
  140.             items.push(CartItemObject.createFromStorage(data[key], key));
  141.         });
  142.  
  143.         return items;
  144.     }
  145.  
  146.     /**
  147.      * @param phone
  148.      * @param name
  149.      * @param address
  150.      * @returns {RepositoryResponse}
  151.      */
  152.     async confirm(phone, name, address) {
  153.         return await CartRepository.create().saveData({
  154.             phone:   phone,
  155.             name:    name,
  156.             address: address,
  157.             cart:    Object.values(Serialize.toObject(this))
  158.         });
  159.     }
  160.  
  161.     /**
  162.      * @returns {{}}
  163.      */
  164.     [toObject]() {
  165.         return this._repository.all();
  166.     }
  167. }
  168.  
  169.  
  170. export class CartRepository extends AsyncRepository {
  171.     static create() {
  172.         return this.cached(() => {
  173.             return new this(Cart, {
  174.                 update: {url: '/cart/confirm.json', method: 'put'}
  175.             });
  176.         });
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement