| // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| part of $LIBRARYNAME; |
| |
| /** |
| * The type used by the |
| * [Window.localStorage] and [Window.sessionStorage] properties. |
| * Storage is implemented as a Map<String, String>. |
| * |
| * To store and get values, use Dart's built-in map syntax: |
| * |
| * window.localStorage['key1'] = 'val1'; |
| * window.localStorage['key2'] = 'val2'; |
| * window.localStorage['key3'] = 'val3'; |
| * assert(window.localStorage['key3'] == 'val3'); |
| * |
| * You can use [Map](http://api.dartlang.org/dart_core/Map.html) APIs |
| * such as containsValue(), clear(), and length: |
| * |
| * assert(window.localStorage.containsValue('does not exist') == false); |
| * window.localStorage.clear(); |
| * assert(window.localStorage.length == 0); |
| * |
| * For more examples of using this API, see |
| * [localstorage_test.dart](http://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/tests/html/localstorage_test.dart). |
| * For details on using the Map API, see the |
| * [Maps](https://www.dartlang.org/guides/libraries/library-tour#maps) |
| * section of the library tour. |
| */ |
| $(ANNOTATIONS)$(NATIVESPEC)$(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS |
| with MapMixin<String, String> { |
| |
| void addAll(Map<String, String> other) { |
| other.forEach((k, v) { this[k] = v; }); |
| } |
| |
| // TODO(nweiz): update this when maps support lazy iteration |
| bool containsValue(Object value) => values.any((e) => e == value); |
| |
| bool containsKey(Object key) => _getItem(key) != null; |
| |
| String operator [](Object key) => _getItem(key); |
| |
| void operator []=(String key, String value) { _setItem(key, value); } |
| |
| String putIfAbsent(String key, String ifAbsent()) { |
| if (!containsKey(key)) this[key] = ifAbsent(); |
| return this[key]; |
| } |
| |
| String remove(Object key) { |
| final value = this[key]; |
| _removeItem(key); |
| return value; |
| } |
| |
| void clear() => _clear(); |
| |
| void forEach(void f(String key, String value)) { |
| for (var i = 0; true; i++) { |
| final key = _key(i); |
| if (key == null) return; |
| |
| f(key, this[key]); |
| } |
| } |
| |
| Iterable<String> get keys { |
| final keys = <String>[]; |
| forEach((k, v) => keys.add(k)); |
| return keys; |
| } |
| |
| Iterable<String> get values { |
| final values = <String>[]; |
| forEach((k, v) => values.add(v)); |
| return values; |
| } |
| |
| int get length => _length; |
| |
| bool get isEmpty => _key(0) == null; |
| |
| bool get isNotEmpty => !isEmpty; |
| $!MEMBERS |
| } |