Return-Path: Delivered-To: apmail-incubator-click-commits-archive@minotaur.apache.org Received: (qmail 44622 invoked from network); 19 Jan 2010 13:50:56 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 19 Jan 2010 13:50:56 -0000 Received: (qmail 72633 invoked by uid 500); 19 Jan 2010 13:50:56 -0000 Delivered-To: apmail-incubator-click-commits-archive@incubator.apache.org Received: (qmail 72617 invoked by uid 500); 19 Jan 2010 13:50:56 -0000 Mailing-List: contact click-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: click-dev@incubator.apache.org Delivered-To: mailing list click-commits@incubator.apache.org Received: (qmail 72606 invoked by uid 99); 19 Jan 2010 13:50:56 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 19 Jan 2010 13:50:56 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 19 Jan 2010 13:50:47 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 080A923889E3; Tue, 19 Jan 2010 13:50:27 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r900780 - /incubator/click/trunk/click/examples/webapp/domloaded.js Date: Tue, 19 Jan 2010 13:50:26 -0000 To: click-commits@incubator.apache.org From: sabob@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100119135027.080A923889E3@eris.apache.org> Author: sabob Date: Tue Jan 19 13:50:26 2010 New Revision: 900780 URL: http://svn.apache.org/viewvc?rev=900780&view=rev Log: domloaded Added: incubator/click/trunk/click/examples/webapp/domloaded.js Added: incubator/click/trunk/click/examples/webapp/domloaded.js URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/webapp/domloaded.js?rev=900780&view=auto ============================================================================== --- incubator/click/trunk/click/examples/webapp/domloaded.js (added) +++ incubator/click/trunk/click/examples/webapp/domloaded.js Tue Jan 19 13:50:26 2010 @@ -0,0 +1,182 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +/* + * This script was adapted from the following online articles: + * + * http://www.cherny.com/webdev/26/domloaded-object-literal-updated + * http://simon.incutio.com/archive/2004/05/26/addLoadEvent + */ + + /* + * Usage: call addLoadEvent one or more times, passing in a function as + * argument each time, for example: + * + * function doSomething() { + * // method1 + * } + * addLoadEvent(doSomething); + * + * addLoadEvent(function() { + * // method2 + * }); + */ +addLoadEvent = (function(func){ + DomLoaded.load(func); +}) + +var DomLoaded = +{ + isReady: false, + onload: [], + loaded: function() + { + if (arguments.callee.done) return; + //comment the line below and use isReady instead? TEST + arguments.callee.done = true; + DomLoaded.isReady = true; + for (i = 0;i < DomLoaded.onload.length;i++) DomLoaded.onload[i](); + + // Remove event listener to avoid memory leak + if (document.addEventListener) + document.removeEventListener( "DOMContentLoaded", DomLoaded.loaded, false ); + + // Remove script element used by IE hack + //if( !window.frames.length ) {// don't remove if frames are present (#1187) + // var elem = document.getElementById("__ie_onload"); + //} + }, + load: function(fireThis) + { + //If the dom is ready, execute the function and return. + if(DomLoaded.isReady) return fireThis(); + this.onload.push(fireThis); + if (document.addEventListener) + document.addEventListener("DOMContentLoaded", DomLoaded.loaded, false); + if (/KHTML|WebKit/i.test(navigator.userAgent)) + { + var _timer = setInterval(function() + { + if (/loaded|complete/.test(document.readyState)) + { + clearInterval(_timer); + delete _timer; + DomLoaded.loaded(); + } + }, 10); + } + /*@cc_on @*/ + /*@if (@_win32) + var proto = "src='javascript:void(0)'"; + if (location.protocol == "https:") proto = "src=//0"; + document.write("<\/scr"+"ipt>"); + var script = document.getElementById("__ie_onload"); + script.onreadystatechange = function() { + if (this.readyState == "complete") { + DomLoaded.loaded(); + } + }; + /*@end @*/ + //window.onload = DomLoaded.loaded; + //window.addEventListener("onload", DomLoaded.loaded, false); + addEvent( window, "load", DomLoaded.loaded); + + } +}; + +// written by Dean Edwards, 2005 +// with input from Tino Zijdel - crisp@xs4all.nl +// http://dean.edwards.name/weblog/2005/10/add-event/ +// and +// http://therealcrisp.xs4all.nl/upload/addEvent_dean.html +function addEvent(element, type, handler) +{ + if (element.addEventListener) + element.addEventListener(type, handler, false); + else + { + if (!handler.$$guid) handler.$$guid = addEvent.guid++; + if (!element.events) element.events = {}; + var handlers = element.events[type]; + if (!handlers) + { + handlers = element.events[type] = {}; + if (element['on' + type]) handlers[0] = element['on' + type]; + element['on' + type] = handleEvent; + } + + handlers[handler.$$guid] = handler; + } +} +addEvent.guid = 1; + +function removeEvent(element, type, handler) +{ + if (element.removeEventListener) + element.removeEventListener(type, handler, false); + else if (element.events && element.events[type] && handler.$$guid) + delete element.events[type][handler.$$guid]; +} + +function handleEvent(event) +{ + event = event || fixEvent(window.event); + var returnValue = true; + var handlers = this.events[event.type]; + + for (var i in handlers) + { + if (!Object.prototype[i]) + { + this.$$handler = handlers[i]; + if (this.$$handler(event) === false) returnValue = false; + } + } + + if (this.$$handler) this.$$handler = null; + + return returnValue; +} + +function fixEvent(event) +{ + event.preventDefault = fixEvent.preventDefault; + event.stopPropagation = fixEvent.stopPropagation; + return event; +} +fixEvent.preventDefault = function() +{ + this.returnValue = false; +} +fixEvent.stopPropagation = function() +{ + this.cancelBubble = true; +} + +// This little snippet fixes the problem that the onload attribute on the body-element will overwrite +// previous attached events on the window object for the onload event +if (!window.addEventListener) +{ + document.onreadystatechange = function() + { + if (window.onload && window.onload != handleEvent) + { + addEvent(window, 'load', window.onload); + window.onload = handleEvent; + } + } +} \ No newline at end of file