Skip to content
Browse files

Added YUI 2 version

  • Loading branch information...
1 parent 52badaa commit 183d7084d8f755e5f05bcc59fafa02337abfd3ea Nicholas C. Zakas committed
View
40 idle-timer/yui2/idle-timer.htm
@@ -0,0 +1,40 @@
+<html>
+<head>
+ <title>Idle Timer Example</title>
+ <script type="text/javascript" src="http://yui.yahooapis.com/combo?2.7.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
+ <script type="text/javascript" src="idle-timer.js"></script>
+<body>
+ <h1>Idle Timer Example</h1>
+ <p>The idle timer is built on <a href="http://developer.yahoo.com/yui/">YUI 2</a> and provides two events: <code>idle</code> and <code>active</code>, which fire when the user's idle state has changed.</p>
+ <p>This example has an idle timeout of 10 seconds. When you move your mouse over the page or start typing, you're considered "active". The status at the top of the screen changes to indicate your current state.</p>
+ <div id="status" style="padding: 5px;">&nbsp;</div>
+ <form>
+ <label for="comment">Comment:</label><br />
+ <textarea rows="10" cols="30" id="comment" name="comment"></textarea><br />
+ <input type="submit" value="Submit" />
+ </form>
+
+
+ <script type="text/javascript">
+ YAHOO.util.Event.onDOMReady(function(){
+
+ var IdleTimer = YAHOO.util.IdleTimer;
+ IdleTimer.subscribe("idle", function(){
+ var status = YAHOO.util.Dom.get("status");
+ status.innerHTML = "User is idle :(";
+ status.style.backgroundColor = "silver";
+ });
+
+ IdleTimer.subscribe("active", function(){
+ var status = YAHOO.util.Dom.get("status");
+ status.innerHTML = "User is active :D";
+ status.style.backgroundColor = "yellow";
+ });
+
+ IdleTimer.start(10000);
+
+ });
+
+ </script>
+</body>
+</html>
View
174 idle-timer/yui2/idle-timer.js
@@ -0,0 +1,174 @@
+/*
+ * Copyright (c) 2009 Nicholas C. Zakas
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+/**
+ * Idle timer
+ * @module idle-timer
+ */
+(function(){
+
+ //-------------------------------------------------------------------------
+ // Private variables
+ //-------------------------------------------------------------------------
+
+ var idle = false, //indicates if the user is idle
+ tId = -1, //timeout ID
+ enabled = false, //indicates if the idle timer is enabled
+ timeout = 30000, //the amount of time (ms) before the user is considered idle
+
+ //shortcuts
+ YUE = YAHOO.util.Event;
+
+ //-------------------------------------------------------------------------
+ // Private functions
+ //-------------------------------------------------------------------------
+
+ /* (intentionally not documented)
+ * Handles a user event indicating that the user isn't idle.
+ * @param {Event} event A DOM2-normalized event object.
+ * @return {void}
+ */
+ function handleUserEvent(){
+
+ //clear any existing timeout
+ clearTimeout(tId);
+
+ //if the idle timer is enabled
+ if (enabled){
+
+ //if it's idle, that means the user is no longer idle
+ if (idle){
+ toggleIdleState();
+ }
+
+ //set a new timeout
+ tId = setTimeout(toggleIdleState, timeout);
+ }
+ }
+
+ /* (intentionally not documented)
+ * Toggles the idle state and fires an appropriate event.
+ * @return {void}
+ */
+ function toggleIdleState(){
+
+ //toggle the state
+ idle = !idle;
+
+ //fire appropriate event
+ YAHOO.util.IdleTimer.fireEvent(idle ? "idle" : "active");
+ }
+
+ //-------------------------------------------------------------------------
+ // Public interface
+ //-------------------------------------------------------------------------
+
+ /**
+ * Centralized control for determining when a user has become idle
+ * on the current page.
+ * @class IdleTimer
+ * @static
+ */
+ YAHOO.namespace("util");
+ var IdleTimer = {
+
+ /**
+ * Indicates if the idle timer is running or not.
+ * @return {Boolean} True if the idle timer is running, false if not.
+ * @method isRunning
+ * @static
+ */
+ isRunning: function(){
+ return enabled;
+ },
+
+ /**
+ * Indicates if the user is idle or not.
+ * @return {Boolean} True if the user is idle, false if not.
+ * @method isIdle
+ * @static
+ */
+ isIdle: function(){
+ return idle;
+ },
+
+ /**
+ * Starts the idle timer. This adds appropriate event handlers
+ * and starts the first timeout.
+ * @param {int} newTimeout (Optional) A new value for the timeout period in ms.
+ * @return {void}
+ * @method start
+ * @static
+ */
+ start: function(newTimeout){
+
+ //set to enabled
+ enabled = true;
+
+ //set idle to false to begin with
+ idle = false;
+
+ //assign a new timeout if necessary
+ if (typeof newTimeout == "number"){
+ timeout = newTimeout;
+ }
+
+ //assign appropriate event handlers
+ YUE.on(document, "mousemove", handleUserEvent);
+ YUE.on(document, "keydown", handleUserEvent);
+
+ //set a timeout to toggle state
+ tId = setTimeout(toggleIdleState, timeout);
+ },
+
+ /**
+ * Stops the idle timer. This removes appropriate event handlers
+ * and cancels any pending timeouts.
+ * @return {void}
+ * @method stop
+ * @static
+ */
+ stop: function(){
+
+ //set to disabled
+ enabled = false;
+
+ //clear any pending timeouts
+ clearTimeout(tId);
+
+ //detach the event handlers
+ YUE.removeListener(document, "mousemove", handleUserEvent);
+ YUE.removeListener(document, "keydown", handleUserEvent);
+ }
+
+ };
+
+ //inherit event functionality
+ YAHOO.lang.augmentObject(IdleTimer, YAHOO.util.EventProvider.prototype);
+
+ //create the custom event objects
+ IdleTimer.createEvent("active");
+ IdleTimer.createEvent("idle");
+
+ //assign into place
+ YAHOO.util.IdleTimer = IdleTimer;
+})();
View
0 idle-timer/idle-timer.htm → idle-timer/yui3/idle-timer.htm
File renamed without changes.
View
0 idle-timer/idle-timer.js → idle-timer/yui3/idle-timer.js
File renamed without changes.

0 comments on commit 183d708

Please sign in to comment.
Something went wrong with that request. Please try again.