Требование: PHP => 5.3, часть 2
[cswowd.git] / js / utils.js
blob08603251a3ceb85450661e634f0d16404f76d53b
1 //
2 // Custom project scripts
3 //
4 function $() {
5 var elements = new Array();
6 for (var i = 0; i < arguments.length; i++) {
7 var element = arguments[i];
8 if (typeof element == 'string')
9 element = document.getElementById(element);
10 if (arguments.length == 1)
11 return element;
12 elements.push(element);
14 return elements;
17 function getElementsByClass(searchClass,node,tag)
19 var classElements = new Array();
20 if ( node == null )
21 node = document;
22 if ( tag == null )
23 tag = '*';
24 var els = node.getElementsByTagName(tag);
25 var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
26 for (i = 0; i < els.length; i++)
27 if ( pattern.test(els[i].className) )
28 classElements.push(els[i]);
29 return classElements;
32 function getBody(){
33 return document.body||document.documentElement||(document.getElementsByTagName ? document.getElementsByTagName("body")[0] : null);
36 function getPageRect(){
37 var b = document.body || document.documentElement,
38 sTop = window.pageYOffset || b.scrollTop,
39 sLeft = window.pageXOffset || b.scrollLeft,
40 sWidth= b.clientWidth || window.innerWidth,
41 sHeight= b.clientHeight || window.innerHeight,
42 oWidth= b.scrollWidth || b.offsetWidth,
43 oHeight= b.scrollHeight || b.offsetHeight;
44 return {top: sTop, left: sLeft, width: sWidth, height: sHeight, scrollX: oWidth, scrollY: oHeight};
47 function getBounds(e)
49 var left = e.offsetLeft, top = e.offsetTop, width = e.offsetWidth, height = e.offsetHeight;
50 while (e = e.offsetParent){
51 left += e.offsetLeft;
52 top += e.offsetTop;
54 return {left: left, top: top, width: width, height: height};
57 function insertElement(parent, tag, id)
59 if(parent.insertAdjacentHTML)
61 parent.insertAdjacentHTML("afterBegin", '<'+tag+ ' id="'+id+'"></'+tag+'>');
62 return document.getElementById(id);
64 else if(document.createElement && parent.appendChild)
66 var el = document.createElement(tag);
67 el.id = id;
68 parent.appendChild(el);
69 return el;
71 return 0;
73 function addEvent(el, sEvt, PFnc)
75 if(el)
77 if(el.addEventListener)
78 el.addEventListener(sEvt, PFnc, false);
79 else
80 el.attachEvent("on" + sEvt, PFnc);
83 function addLoadEvent(func)
85 var oldonload = window.onload;
86 if (typeof window.onload != 'function')
87 window.onload = func;
88 else
89 window.onload = function(){oldonload();func();}
91 function removeEvent(el, sEvt, PFnc)
93 if(el)
95 if(el.removeEventListener)
96 el.removeEventListener(sEvt, PFnc, false);
97 else
98 el.detachEvent("on" + sEvt, PFnc);
101 function ChangeCssProperty(myclass, element, value)
103 var CSSRules = document.styleSheets[0].rules || document.styleSheets[0].cssRules;
104 for (var i = 0; i < CSSRules.length; i++)
105 if (CSSRules[i].selectorText.toLowerCase() == myclass.toLowerCase())
106 CSSRules[i].style[element] = value;
108 function getOpaSettings()
110 var p = null;
111 var s = document.body.style;
112 if (typeof s.opacity == 'string') p = 'opacity';
113 else if (typeof s.MozOpacity == 'string') p = 'MozOpacity';
114 else if (typeof s.KhtmlOpacity == 'string') p = 'KhtmlOpacity';
115 else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) p='filter';
116 return p;
118 function setOpacity(oElem, nOpacity)
120 var p = getOpaSettings();
121 if (p=='filter')
122 setOpacity = new Function('oElem', 'nOpacity', 'if (nOpacity >= 100) {oElem.style.filter = ""; return;} var oAlpha = oElem.filters["DXImageTransform.Microsoft.alpha"] || oElem.filters.alpha; if (oAlpha) oAlpha.opacity = nOpacity;else {oElem.style.zoom = 1;oElem.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")";}');
123 else if (p)
124 setOpacity = new Function('oElem', 'nOpacity', 'oElem.style.'+p+' = nOpacity/100;');
125 else
126 setOpacity = new Function(oElem, nOpacity);
127 return setOpacity(oElem, nOpacity);
129 function getOpacity(oElem)
131 var p = getOpaSettings();
132 if (p=='filter')
133 getOpacity = new Function('oElem', 'var m = oElem.style.filter.match(/alpha\(opacity=(.+)\)/i); return m ? parseFloat(m[1]) : 1;');
134 else if (p)
135 getOpacity = new Function('oElem', 'var v = oElem.style.'+p+'; return v*100;');
136 else
137 getOpacity = new Function(oElem);
138 return getOpacity(oElem);
140 Number.prototype.Timer = function(s, iT, bUrge){
141 this.EndTimer();
142 if(!this.value || bUrge)
143 this.value = window.setTimeout(s, iT);
145 Number.prototype.EndTimer = function(){
146 if(this.value){
147 window.clearTimeout(this.value);
148 this.value = 0;
151 function parseURL(url)
153 // Example "https://www.example.com:8080/some/path/index.html?p=1&q=2&r=3#some-hash"
154 var patern =
155 // #0 URL
156 "^" +
157 // #1 PROTOCOL, "https"
158 "(?:([^:/\\?#]*):)?" +
159 // #2 HOST, "www.example.com",
160 "(?://([^:/\\?#]*))" +
161 // #3 PORT, "8080"
162 "(?::([^/\\?#]*))?" +
163 // #4 PATH, "/some/path/index.html"
164 "(?:([^\\?#]*))" +
165 // #5 QUERY, "p=1&q=2&r=3"
166 "(?:\\?([^#]*))?" +
167 // #6 FRAGMENT, "some-hash"
168 "(?:#(.*))?" + "$";
169 return RegExp(patern).exec(url);
173 // Link event add function
175 function parseHref(element)
177 var tip={'item':'i', 'spell':'s', 'enchant':'e', 'npc':'c', 'faction':'f'};
178 var ext={'jpg':1, 'jpeg':1, 'png':1};
179 var c = element.getElementsByTagName("a");
180 for (var i = 0; i < c.length; i++)
182 var a = c[i];
183 var url = parseURL(a.href);
184 if (!url) continue;
185 // Parse file
186 if (!url[4]) continue;
187 var e = url[4].split('.');
188 if (e.length > 1 && ext[e[e.length-1]]) a.onclick = showLightbox;
189 // Parse params
190 if (!url[5]) continue;
191 var p=url[5].split('&');
192 var r=p[0].split('=');
193 // Add tooltip event
194 if (p.length==1&&tip[r[0]]&&r[1]&&!a.id) {a.id=tip[r[0]]+r[1]; a.onmouseover = tt_hrefTip;}
195 // Add light box event
196 else if(r[0]=='map' && !a.onclick) a.onclick = showAjaxBox;
200 // Upload data as HTML in obj.innerHTML via ajax functions
202 function ajaxCacheHtml(element, url) {
203 my_AJAX.addCache(element.innerHTML, 'ajax.php'+url);
206 function ajaxCacheHtmlId(elementId, url) {
207 ajaxCacheHtml(document.getElementById(elementId), url);
209 function uploadFromHref(link, elementId) {
210 var url = link.href.substring(link.href.indexOf('?'), link.href.length);
211 uploadHtmlToId(url, elementId);
212 return false;
214 function uploadHtmlToId(url, elementId) {
215 uploadHtml(url, $(elementId));
217 function uploadHtml(url, element)
219 var obj = element;
220 function callback(text) {
221 obj.innerHTML = text;
222 execHTMLScripts(obj);
223 parseHref(obj);
225 my_AJAX.GETupload('ajax.php'+url, callback);
228 // Report tabs support functions
230 function report_setSelect(tab, selected)
232 var l = tab.id.split(':');
233 var element = $(l[1]);
234 if (!element) return;
235 element.style.display = selected ? "block" : "none";
236 tab.className = selected ? 'selected' : '';
238 function report_selectTab(page)
240 var tab = $('report_tabs');
241 if (!tab) return;
242 var els = tab.getElementsByTagName('li');
243 for (i = 0; i < els.length; i++)
244 report_setSelect(els[i], els[i].id == page.id)
246 function report_hideHeaders()
248 ChangeCssProperty('TABLE.report TR.head', 'display', 'none');
250 function report_addTab(name, elementId, selected)
252 var tab = $('report_tabs');
253 if (!tab) return;
254 tab.innerHTML += '<li id="t:' + elementId + '" onClick="report_selectTab(this);"><a>' + name + '</a></li>';
255 report_setSelect($('t:' + elementId), selected);
258 // Debug layer
260 var debugDiv = 0;
261 document.write('<div id=debug></div>');
262 function outDebug(text)
264 var debugDiv = $('debug');
265 if (!debugDiv)
267 debugDiv = document.createElement('div');
268 document.body.appendChild(debugDiv);
269 debugDiv.style.position = 'fixed';
270 debugDiv.style.border = '1px solid';
271 debugDiv.style.background = '#EEE';
272 debugDiv.style.top = 0;
273 debugDiv.style.left = 0;
274 debugDiv.style.width = '800px';
276 debugDiv.innerHTML=text + '<br>';