f05f2c77cc
BUG= Review URL: https://codereview.chromium.org//12219002 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@18111 260f80e4-7a28-3924-810f-c04153c831b5
83 lines
2.4 KiB
Plaintext
83 lines
2.4 KiB
Plaintext
// 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](http://www.dartlang.org/docs/library-tour/#maps-aka-dictionaries-or-hashes)
|
|
* section of the library tour.
|
|
*/
|
|
$(ANNOTATIONS)class $CLASSNAME$EXTENDS implements Map<String, String>
|
|
$NATIVESPEC {
|
|
|
|
// TODO(nweiz): update this when maps support lazy iteration
|
|
bool containsValue(String value) => values.any((e) => e == value);
|
|
|
|
bool containsKey(String key) => $dom_getItem(key) != null;
|
|
|
|
String operator [](String key) => $dom_getItem(key);
|
|
|
|
void operator []=(String key, String value) { $dom_setItem(key, value); }
|
|
|
|
String putIfAbsent(String key, String ifAbsent()) {
|
|
if (!containsKey(key)) this[key] = ifAbsent();
|
|
return this[key];
|
|
}
|
|
|
|
String remove(String key) {
|
|
final value = this[key];
|
|
$dom_removeItem(key);
|
|
return value;
|
|
}
|
|
|
|
void clear() => $dom_clear();
|
|
|
|
void forEach(void f(String key, String value)) {
|
|
for (var i = 0; true; i++) {
|
|
final key = $dom_key(i);
|
|
if (key == null) return;
|
|
|
|
f(key, this[key]);
|
|
}
|
|
}
|
|
|
|
Collection<String> get keys {
|
|
final keys = [];
|
|
forEach((k, v) => keys.add(k));
|
|
return keys;
|
|
}
|
|
|
|
Collection<String> get values {
|
|
final values = [];
|
|
forEach((k, v) => values.add(v));
|
|
return values;
|
|
}
|
|
|
|
int get length => $dom_length;
|
|
|
|
bool get isEmpty => $dom_key(0) == null;
|
|
$!MEMBERS
|
|
}
|