PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/StuffCounter/character-sheet.xml_files/common.js

http://luskycode.codeplex.com
JavaScript | 322 lines | 311 code | 7 blank | 4 comment | 79 complexity | 14ba92de1c3ee20dbc3e73e4e093e8f9 MD5 | raw file
  1. var Browser = {
  2. a : navigator.userAgent.toLowerCase()
  3. }
  4. Browser = {
  5. ie : /*@cc_on ! @*/ false,
  6. ie6 : Browser.a.indexOf('msie 6') != -1,
  7. ie7 : Browser.a.indexOf('msie 7') != -1,
  8. opera : !!window.opera,
  9. safari : Browser.a.indexOf('safari') != -1,
  10. safari3 : Browser.a.indexOf('applewebkit/5') != -1,
  11. mac : Browser.a.indexOf('mac') != -1
  12. }
  13. function $blizz(e) {
  14. if(typeof e == 'string')
  15. return document.getElementById(e);
  16. return e;
  17. }
  18. String.prototype.trim = function() {
  19. return this.replace(/^\s+|\s+$/g, '');
  20. }
  21. function indexOf(array, elt /*, from*/) {
  22. // SpiderMonkey implementation
  23. var len = array.length;
  24. var from = Number(arguments[2]) || 0;
  25. from = (from < 0) ? Math.ceil(from) : Math.floor(from);
  26. if(from < 0)
  27. from += len;
  28. for(; from < len; from++)
  29. if(from in array && array[from] === elt)
  30. return from;
  31. return -1;
  32. }
  33. function createElement(name, attrs, doc, xmlns) {
  34. var doc = doc || document;
  35. var elm;
  36. if(doc.createElementNS)
  37. elm = doc.createElementNS(xmlns || 'http://www.w3.org/1999/xhtml', name);
  38. else
  39. elm = doc.createElement(name);
  40. if(attrs)
  41. for(attr in attrs)
  42. elm.setAttribute(attr, attrs[attr]);
  43. return elm;
  44. }
  45. function appendText(node, text, replaceNewlines) {
  46. if(replaceNewlines) {
  47. var frag = document.createDocumentFragment();
  48. var lines = text.split(/\n/g);
  49. for(var i = 0; i < lines.length; i++) {
  50. frag.appendChild(document.createTextNode(lines[i]))
  51. if(i < lines.length - 1)
  52. frag.appendChild(createElement('br'));
  53. }
  54. node.appendChild(frag);
  55. } else
  56. node.appendChild(document.createTextNode(text));
  57. }
  58. function removeText(parent) {
  59. var nodes = parent.childNodes;
  60. for(var node, i = 0; node = nodes[i]; i++) {
  61. if(node.nodeType == 3 || node.nodeName.toLowerCase() == 'br') { // Node.TEXT_NODE == 3
  62. parent.removeChild(node);
  63. i--;
  64. }
  65. }
  66. }
  67. function setDisplay(e, display) {
  68. $blizz(e).style.display = display;
  69. }
  70. function hide(e) {
  71. setDisplay(e, 'none');
  72. }
  73. function show(e) {
  74. setDisplay(e, '');
  75. }
  76. function visible(e) {
  77. return $blizz(e).style.display != 'none';
  78. }
  79. function toggle(e) {
  80. (visible(e) ? hide : show)(e);
  81. }
  82. function getElementsByClassName(className, element, tagName) { // non live list
  83. var regexp = new RegExp('(^|\\s)' + className + '(\\s|$)');
  84. var tagName = tagName || '*';
  85. var element = element || document;
  86. var elements = (tagName == '*' && element.all) ? element.all : element.getElementsByTagName(tagName);
  87. var found = [];
  88. for(var i = 0, elm; elm = elements[i]; i++)
  89. if(regexp.test(elm.className))
  90. found.push(elm);
  91. return found;
  92. }
  93. function hasClassName(e, className) {
  94. return indexOf($blizz(e).className.split(/\s+/), className) > -1;
  95. }
  96. function addClassName(e, className) {
  97. var e = $blizz(e);
  98. if(!hasClassName(e, className))
  99. e.className += (e.className ? ' ' : '') + className;
  100. }
  101. function removeClassName(e, classNames) {
  102. var e = $blizz(e);
  103. var classes = e.className.split(/\s+/);
  104. var rem = classNames.split(/\s+/);
  105. var ret = [];
  106. for(var i = 0; i < classes.length; i++)
  107. if(indexOf(rem, classes[i]) == -1)
  108. ret.push(classes[i]);
  109. e.className = ret.join(' ');
  110. }
  111. function removeChildren(e) {
  112. while(e.firstChild)
  113. e.removeChild(e.firstChild);
  114. }
  115. function bind(method, scope, args) {
  116. if(!args)
  117. args = [];
  118. return function() {
  119. return method.apply(scope, args);
  120. }
  121. }
  122. function bindWithArgs(method, scope, args) { // TODO replace bind()
  123. if(!args)
  124. args = [];
  125. return function() {
  126. var len = arguments.length;
  127. var arr = new Array(len);
  128. while(len--)
  129. arr[len] = arguments[len];
  130. return method.apply(scope, args.concat(arr));
  131. }
  132. }
  133. function bindEventListener(method, scope, args) {
  134. if(!args)
  135. args = [];
  136. return function(event) {
  137. return method.apply(scope, [(event || window.event)].concat(args));
  138. }
  139. }
  140. function addEvent(obj, type, func) {
  141. if(obj.addEventListener) {
  142. obj.addEventListener(type, func, false);
  143. return true;
  144. } else if(obj.attachEvent || Browser.ie)
  145. return obj.attachEvent('on' + type, func);
  146. return false;
  147. }
  148. function stopEvent(e) {
  149. if(e.stopPropagation)
  150. e.stopPropagation();
  151. else
  152. e.cancelBubble = true; // IE
  153. }
  154. function addStylesheet(href, media) {
  155. document.getElementsByTagName('head')[0].appendChild(createElement('link', {
  156. rel: 'stylesheet',
  157. type: 'text/css',
  158. media: media || 'screen, projection',
  159. href: href
  160. }));
  161. }
  162. function loadScript(src, id) {
  163. var head = document.getElementsByTagName('head')[0];
  164. var script = createElement('script', {
  165. 'type': 'text/javascript',
  166. 'src': src
  167. });
  168. if(id) {
  169. var old = document.getElementById(id);
  170. if(old)
  171. old.parentNode.removeChild(old);
  172. script.id = id;
  173. }
  174. head.appendChild(script);
  175. }
  176. function padLeft(str, len, fillChar) {
  177. while(str.length < len)
  178. str = fillChar + str;
  179. return str;
  180. }
  181. function printf(str) {
  182. var argc = arguments.length;
  183. for(var i = 1; i < argc; i++) {
  184. var re = new RegExp('\\{' + (i-1) + '\\}', 'g');
  185. str = str.replace(re, arguments[i]);
  186. }
  187. return str;
  188. }
  189. function duplicateNodes(parent, searchClassName, resultClassName, insertAfter) {
  190. var nodes = getElementsByClassName(searchClassName, parent);
  191. for(var node, i = 0; node = nodes[i]; i++) {
  192. var clone = node.cloneNode(true);
  193. removeClassName(clone, searchClassName);
  194. addClassName(clone, resultClassName);
  195. node.parentNode.insertBefore(clone, insertAfter ? node.nextSibling : node);
  196. }
  197. }
  198. var Timer = {
  199. _ids: {},
  200. set: function(id, code, timeout, allowMultiple) {
  201. if(Timer._ids[id] != null && !allowMultiple)
  202. Timer.clear(id);
  203. Timer._ids[id] = window.setTimeout(code, timeout);
  204. },
  205. clear: function(id) {
  206. window.clearTimeout(Timer._ids[id]);
  207. delete Timer._ids[id];
  208. }
  209. }
  210. function CustomSelect(handle, options, handleActive, optionsActive, multiClick, closeDelay) {
  211. this.handle = $blizz(handle);
  212. this.options = $blizz(options);
  213. this.closeDelay = closeDelay || CustomSelect.DEFAULT_HIDE_DELAY;
  214. this.classNames = {
  215. handleActive: handleActive,
  216. optionsActive: optionsActive || handleActive
  217. }
  218. this.timerId = 'select' + CustomSelect.instances;
  219. CustomSelect.instances++;
  220. CustomSelect._attachEventListeners(this, multiClick);
  221. }
  222. CustomSelect.DEFAULT_HIDE_DELAY = 1000;
  223. CustomSelect.instances = 0;
  224. CustomSelect.openInstance = null;
  225. CustomSelect.init = function(selects) {
  226. for(var select, i = 0; select = selects[i]; i++)
  227. new CustomSelect(select[0], select[1], select[2], select[3], select[4], select[5]);
  228. }
  229. CustomSelect._attachEventListeners = function(s, multiClick) {
  230. var eventListeners = [
  231. [s.options, 'mousedown', s.cancelCloseDelayed],
  232. [s.handle, 'mousedown', s.toggle],
  233. [s.options, 'mouseover', s.cancelClose],
  234. [s.handle, 'mouseover', s.cancelClose],
  235. [s.options, 'mouseout', s.closePassive],
  236. [s.handle, 'mouseout', s.closePassive]
  237. ];
  238. if(!multiClick)
  239. eventListeners.push([s.options, 'click', s.close]);
  240. // not implemented: closing options after blur on element within multi-click options node
  241. eventListeners.push([s.handle, 'blur', s.close]);
  242. for(var el, i = 0; el = eventListeners[i]; i++)
  243. addEvent(el[0], el[1], bind(el[2], s));
  244. }
  245. // small delays to account for browsers firing events in different orders
  246. CustomSelect.prototype = {
  247. toggle: function() {
  248. this.cancelClose();
  249. if(this.options.style.display == "block"){
  250. this._deactivate()
  251. }else{
  252. this._activate();
  253. }
  254. },
  255. close: function() {
  256. this.closePassive(50);
  257. },
  258. closePassive: function(delay) {
  259. Timer.set(this.timerId, bind(this._deactivate, this), delay || this.closeDelay);
  260. },
  261. cancelClose: function() {
  262. Timer.clear(this.timerId);
  263. },
  264. cancelCloseDelayed : function() {
  265. window.setTimeout(bind(this.cancelClose, this), 10);
  266. },
  267. _activate: function() {
  268. if(CustomSelect.openInstance) // deactivate other menu instantly, no delay
  269. CustomSelect.openInstance._deactivate();
  270. CustomSelect.openInstance = this;
  271. this.options.style.display = "block";
  272. },
  273. _deactivate: function() {
  274. if(CustomSelect.openInstance == this)
  275. CustomSelect.openInstance = null;
  276. this.options.style.display = "none";
  277. }
  278. }
  279. var XHR = {
  280. getJSON: function(uri, preventCaching, callback, errorCallback) {
  281. // IE 6 compatibility
  282. var xhr = (typeof XMLHttpRequest != "undefined") ? new XMLHttpRequest() : new window.ActiveXObject("Microsoft.XMLHTTP");
  283. xhr.open("GET", uri, true);
  284. if(preventCaching)
  285. xhr.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
  286. xhr.onreadystatechange = function() {
  287. if(xhr.readyState == 4) {
  288. if(xhr.status == 200 && xhr.responseText) {
  289. if(callback)
  290. callback(xhr.responseText);
  291. else
  292. eval(xhr.responseText);
  293. } else if(errorCallback)
  294. errorCallback(xhr.status);
  295. }
  296. }
  297. xhr.send(null);
  298. }
  299. }
  300. function toggle_first(targ)
  301. {
  302. targ.className = (targ.className.indexOf("open")>1)?"firsts_achievement firsts_closed":"firsts_achievement firsts_open"
  303. }
  304. function fnews_close(targ)
  305. { gfather = document.getElementById("featured_module")
  306. thisn = targ.parentNode.parentNode.parentNode
  307. thisn.parentNode.removeChild(thisn)
  308. gf = gfather.getElementsByTagName("div"); nentry = new Array();
  309. for(x=0;x<gf.length;x++){ if(gf[x].className.indexOf("news_mod_entry")>-1) nentry.push(gf[x]); }
  310. if(!nentry.length) gfather.parentNode.removeChild(gfather)
  311. }
  312. function frst_valid(frm,fld)
  313. { if(fld.value =='') { fld.style.borderColor='#e00900'; }
  314. else { frm.submit(); }
  315. }