Il faut écouter l'événement « readystatechange » plutôt que « load » pour récupérer...
[Plinn.git] / skins / ajax_scripts / root_handlers.js
1 // © Benoît PIN 2006-2009
2 // http://plinn.org
3 // Licence GPL
4 //
5 //
6
7 /* Ajax for everything : if an click event has not been intercepted before */
8
9 var AjaxLinkHandler;
10
11 (function(){
12
13 var reProtocol = /^\s*[a-z]+:/;
14
15 AjaxLinkHandler = function(locationPollInterval) {
16 var thisHandler = this;
17 this.previousHash = '#' + location.href;
18 location.hash = location.href;
19 setInterval(function(){thisHandler.checkLocation();}, locationPollInterval)
20 addListener(document.body, 'click', function(evt){thisHandler.handleClick(evt);});
21 if (browser.isIE55 || browser.isIE6up) {
22 var ie_historyFrm = document.createElement('iframe');
23 ie_historyFrm.setAttribute('src', portal_url() + '/scrape_ie_history');
24 with (ie_historyFrm.style) {
25 border="0";
26 width="1px";
27 height="1px";
28 position="absolute";
29 bottom="0";
30 right="0";
31 visibility="visible";
32 }
33 document.body.appendChild(ie_historyFrm);
34 this.historyFrame = ie_historyFrm;
35 }
36 };
37
38 AjaxLinkHandler.prototype.checkLocation = function() {
39 if ((this.previousHash != location.hash) && location.hash) {
40 var rawUrl = unescape(location.hash.slice(1));
41
42 var urlHash = rawUrl.split('#');
43 var url = urlHash[0];
44 var hash = urlHash[1];
45
46 var ajaxParams='ajax=1&_browserObjectUrl=' + escape(absolute_url());
47 var urlQueryStart = url.indexOf('?');
48 if (urlQueryStart != -1)
49 url += '&' + ajaxParams;
50 else
51 url += '?' + ajaxParams;
52
53 try {
54 var fi = new FragmentImporter(url);
55 if (hash) {
56 var thisHandler = this;
57 fi.onAfterPopulate = function(){thisHandler.loadHash('#' + hash);};
58 }
59 fi.load(rawUrl);
60 }
61 catch (e) {
62 window.location.href = rawUrl;
63 }
64 }
65 this.previousHash = location.hash;
66 };
67
68 AjaxLinkHandler.prototype.handleClick = function(evt){
69 var target = getTargetedObject(evt);
70 while (target.nodeName != 'A') {
71 target = target.parentNode;
72 if (target == document.body)
73 return;
74 }
75 target.blur();
76 // prevent click glitches from IE :((
77 if (browser.isIE55 || browser.isIE6up) {
78 if (_disableRootClickHandler)
79 return;
80 else {
81 _disableRootClickHandler = true;
82 setTimeout("_disableRootClickHandler=false", 100);
83 }
84 }
85
86 if (target.target)
87 return;
88
89 var url;
90 var m = reProtocol.exec(target.getAttribute('href', 2));
91 if (m) {
92 var protocol = m[0];
93 if (protocol == location.protocol)
94 url = target.href;
95 else
96 return;
97 }
98 else
99 url = absolute_url() + '/' + target.getAttribute('href', 2);
100
101 if (!url) return;
102
103 if (browser.isGecko)
104 url = encodeURIComponent(url);
105 var query = target.search;
106 if ((query && query.search("noajax=1") != -1) || target.name == 'noajax')
107 return;
108
109 disableDefault(evt);
110 this.loadUrl(url);
111 };
112
113 if (browser.isIE55 || browser.isIE6up) {
114 AjaxLinkHandler.prototype.loadUrl = function(url) {
115 if (location.hash.slice(1) == url)
116 url += '#'
117 this.historyFrame.contentWindow.location.search = '?url=' + escape(url);
118 location.hash = escape(url);
119 };
120 }
121 else {
122 AjaxLinkHandler.prototype.loadUrl = function(url) {
123 if (location.hash.slice(1) == url)
124 url += '#'
125 location.hash = escape(url);
126 };
127 }
128
129 AjaxLinkHandler.prototype.loadHash = function(hash) {
130 this.previousHash = hash;
131 location.hash = hash;
132 };
133
134 AjaxLinkHandler.prototype.ie_loadHistory = function(url) {
135 location.hash = escape(url);
136 };
137
138 function ajaxSubmitFormHandler(evt) {
139 var target = getTargetedObject(evt);
140 if (target.nodeName == 'INPUT' && (target.type == 'submit' || target.type == 'image')) {
141 var form = target;
142 while (form != document) {
143 form = form.parentNode;
144 if (form.nodeName == 'FORM') {
145 var fm = new FormManager(form, document.getElementById("mainCell"));
146 fm.submitButton = target;
147 //disableDefault(evt);
148 break;
149 }
150 }
151 }
152
153 }
154
155 function _addRootHandlers() {
156 if ((AJAX_CONFIG & 1) == 1) {
157 window.linkHandler = new AjaxLinkHandler(200);
158 if (browser.isIE55 || browser.isIE6up) {
159 _disableRootClickHandler = false;
160 }
161 }
162 if ((AJAX_CONFIG & 2) == 2) {
163 addListener(document, 'click', ajaxSubmitFormHandler);
164 }
165 }
166
167 registerStartupFunction(_addRootHandlers);
168
169 })();