1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Simple Storage Example — Saving In-Progress Text Entry</title>
<style type="text/css">
/*margin and padding on body element
can introduce errors in determining
element position and are not recommended;
we turn them off as a foundation for YUI
CSS treatments. */
body {
margin:0;
padding:0;
}
</style>
<link rel="stylesheet" type="text/css" href="../../build/fonts/fonts-min.css" />
<script type="text/javascript" src="../../build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="../../build/element/element-min.js"></script>
<script type="text/javascript" src="../../build/cookie/cookie-min.js"></script>
<script type="text/javascript" src="../../build/swf/swf-min.js"></script>
<script type="text/javascript" src="../../build/swfstore/swfstore-min.js"></script>
<script type="text/javascript" src="../../build/storage/storage-min.js"></script>
<!--begin custom header content for this example-->
<style type="text/css">
#textentry {
height:100px;
width:400px;
background-color:#dedede;
color:#2D119F;
font-weight:bold;
}
#autosave {
margin-top: 5px;
font-size: 10px;
color: #666666;
}
</style>
<!--end custom header content for this example-->
</head>
<body class="yui-skin-sam">
<h1>Simple Storage Example — Saving In-Progress Text Entry</h1>
<div class="exampleIntro">
<p>This example demonstrates a rudimentary use case for the <a href="http://developer.yahoo.com/yui/storage/">Storage Utility</a>. When you enter text in the text field below, your content will be locally saved on every fifth <code>keypress</code>. If your browser supports one of the Storage Utility's storage mechanisms (Gears, SWF, and HTML5 are tried, in that order), your last-saved content will persist when you reload the page.</p>
<p>Watch the logger on the right side of the page as you type for feedback about what the example is doing.</p>
</div>
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== -->
<form action="." method="post" id="textentryform">
<textarea id="textentry">Text entered here will be autosaved as you type.</textarea>
<p id="autosave">#</p>
</form>
<!--Always include gears_init.js when you want to support Google Gears:-->
<script type="text/javascript" src="../storage/assets/gears_init.js"></script>
<script language="javascript">
YAHOO.util.Event.onDOMReady(function() {
var ctr = 0,
storageEngine;
YAHOO.util.StorageEngineSWF.SWFURL = 'swfstore.swf';
//wrap instantiation in a try/catch to handle situations where
//the client browser does not support any of your desired
//storage engines; use the catch statement to handle those
//unsupported browsers. In this case, we're using Storage as
//an enhancement, and we can fail gracefully if instantiation
//fails.
try {
storageEngine = YAHOO.util.StorageManager.get(
YAHOO.util.StorageEngineGears.ENGINE_NAME,
YAHOO.util.StorageManager.LOCATION_LOCAL,
{
order: [
YAHOO.util.StorageEngineGears,
YAHOO.util.StorageEngineSWF,
YAHOO.util.StorageEngineHTML5
],
force: false
}
);
} catch(e) {
YAHOO.log("No supported storage mechanism present.");
storageEngine = false;
}
//storageEngine will be truthy if instantiation succeeded and
//false if no engine was available or if instantiation failed
//for any reason.
if(storageEngine) {
//If we got a valid storage engine, we can make use of it.
//Use the custom event CE_READY to identify the moment
storageEngine.subscribe(storageEngine.CE_READY, function(e) {
YAHOO.log("A Storage instance is ready using " +
storageEngine.getName() +
"; start typing in the text box and content will be saved every five keystrokes.", "info", "example");
//Prepopulate the text field with saved contents, if present.
if(storageEngine.hasKey("simple-storage-textentry")) {
YAHOO.log(storageEngine.getItem("simple-storage-textentry"));
YAHOO.util.Dom.get("textentry").value = storageEngine.getItem("simple-storage-textentry");
}
//Set up an event listener to trigger the Storage set method
//every five keystrokes.
YAHOO.util.Event.on("textentry", "keypress", function(e) {
ctr++;
if(!(ctr%5)) { //on every fifth keystroke, autosave the contents
storageEngine.setItem("simple-storage-textentry", YAHOO.util.Dom.get("textentry").value);
document.getElementById("autosave").innerHTML = "last saved at " + (new Date).toString();
YAHOO.log("Autosaved textarea content to " +
storageEngine.getName() + " via Storage Utility.", "info", "example");
}
});
});
}
});
</script>
<!--END SOURCE CODE FOR EXAMPLE =============================== -->
</body>
</html>
|