<!--
Copyright 2013 The Polymer Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<!--
/**
 * @module Polymer Elements
 */
/**
 * Element access to localStorage.  The "name" property
 * is the key to the data ("value" property) stored in localStorage.
 *
 * polymer-localstorage automatically saves the value to localStorage when
 * value is changed.  Note that if value is an object auto-save will be
 * triggered only when value is a different instance.
 *
 * Example:
 *
 *     <polymer-localstorage name="my-app-storage" value="{{value}}"></polymer-localstorage>
 *
 * @class polymer-localstorage
 * @blurb Element access to localStorage.
 * @snap http://polymer.github.io/polymer-localstorage/snap.png
 * @author The Polymer Authors
 * @categories Data
 *
 */
/**
 * Fired after it is loaded from localStorage.
 * 
 * @event polymer-localstorage-load
 */
-->
<link rel="import" href="../polymer/polymer.html">

<!--
Copyright 2013 The Polymer Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<!--
/**
 * @module Polymer Elements
 */
/**
 * polymer-localstorage provides access to localStorage.
 *
 * Example:
 *
 *     <polymer-localstorage name="my-app-storage" value="{{value}}"></polymer-localstorage>
 *
 * @class polymer-localstorage
 */
-->
<polymer-element name="polymer-localstorage" attributes="name value useRaw">
  <template>
    <style>
      @host {
        * {
          display: none;
        }
      }
    </style>
  </template>
  <script>
    Polymer('polymer-localstorage', {
      useRaw: false,
      ready: function() {
        this.load();
      },
      valueChanged: function() {
        this.save();
      },
      load: function() {
        var s = window.localStorage.getItem(this.name);
        if (s && !this.useRaw) {
          this.value = JSON.parse(s);
        } else {
          this.value = s;
        }
      },
      save: function() {
        window.localStorage.setItem(this.name,
          this.useRaw ? this.value : JSON.stringify(this.value));
      }
    });
  </script>
</polymer-element>