catalog-cart.html 2.39 KB
<link rel="import" href="../catalog-element/catalog-element.html">

<script>
(function() {
  var _instances = [];

  var _store = function() {
    if (_instances.length) {
      localStorage['catalog.cart'] = JSON.stringify(_instances[0].items);
    }
  };
  
  var _retrieve = function() {
    try {
      return JSON.parse(localStorage['catalog.cart'] || '[]');
    } catch (e) {
      console.log('error retrieving catalog data from localstorage.', e);
      return [];
    }
  };

  var _add = function(name) {
    var el = document.createElement('catalog-element');
    el.name = name;
    if (!el.data) {
      return;
    }

    var check = _instances[0];
    var insertAt;
    if (check.items.length === 0 || check.items[0].name > name) {
      insertAt = 0;
    } else {
      for (var i = 0; i < check.items.length; i++) {
        if (name > check.items[i].name && (!check.items[i + 1] || name < check.items[i+1].name)) {
          //console.log(name, ">", check.items[i].name);
          insertAt = i + 1;
          break;
        }
      }
    }

    _instances.forEach(function(instance) {
      instance.splice('items',insertAt,0,el.data);
      instance.fire('item-added', {name: el.name, element: el.data}, {bubbles: false});
    });
    _store();
    return el.data;
  };

  var _remove = function(el) {
    var check = _instances[0];
    var removeAt = -1;
    for (var i = 0; i < check.items.length; i++) {
      if (check.items[i] === el || check.items[i].name === el) {
        removeAt = i;
      }
    }

    if (removeAt >= 0) {
      _instances.forEach(function(instance) {
        instance.splice('items',removeAt,1);
        instance.fire('item-removed', {name: el.name, element: el.data}, {bubbles: false});
      });
      _store();
    } else {
      return false;
    }
  };

  Polymer({
    is: 'catalog-cart',
    properties: {
      items: {
        type: Array,
        notify: true,
        value: function() {
          return _retrieve();
        }
      }
    },
    created: function() {
      _instances.push(this);
    },
    add: function(name) {
      if (this.includes(name)) return false;
      return _add(name);
    },
    remove: function(name) {
      return _remove(name);
    },
    includes: function(el) {
      for (var i = 0; i < this.items.length; i++) {
        if (this.items[i] === el || this.items[i].name === el) return true;
      }
      return false;
    }
  });
})();
</script>