ieat-util.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* JavaScript for global utilities.
  2. * ===================================================================
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License as
  5. * published by the Free Software Foundation; either version 2 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. * 02111-1307 USA
  17. * ===================================================================
  18. * $Id: util.js,v 1.6 2005/08/23 06:29:08 matt Exp $
  19. * ===================================================================
  20. */
  21. var DEFAULT_LANG = 'en';
  22. var webContext = '/ieat'; // default context
  23. var myLang = 'en_US'; // default lang
  24. /**
  25. * Get a JavaScript message variable for a given language.
  26. *
  27. * This method will attempt to return the variable
  28. * associated with 'key' and 'lang'. The 'lang' format
  29. * can be 'en_US' or just 'en'. If the variable is found
  30. * it's value is returned, otherwise an empty string
  31. * is returned.
  32. *
  33. * If 'params' is passed, it should be an array of String
  34. * objects, which will be substituted into the message
  35. * value by index, starting at 1, with brace delimiters.
  36. * For example "The {1} value is required" along
  37. * with an array ['name'] would result in the message
  38. * "The name value is required."
  39. *
  40. * This is a crude way to store internationalized messages
  41. * for JavaScript functions.
  42. *
  43. * GLOBAL VARIABLE: myLang
  44. *
  45. * @param key the JavaScript variable name, without langugage
  46. * @param lang the desired language (optional)
  47. * @param params an array of parameter values
  48. */
  49. function getMessage(key,lang,params) {
  50. if ( !lang ) {
  51. lang = myLang;
  52. } else {
  53. if ( typeof(lang) != 'string' ) {
  54. if ( lang.length ) {
  55. // already an array
  56. params = lang;
  57. } else {
  58. params = new Array();
  59. params[0] = lang;
  60. }
  61. lang = myLang;
  62. }
  63. }
  64. if ( !lang ) {
  65. alert("! Language not available");
  66. return '';
  67. }
  68. var res;
  69. if ( eval('typeof ' +key+'_'+lang) != 'undefined' ) {
  70. res = eval(key+'_'+lang);
  71. } else {
  72. var idx = lang.indexOf('_')
  73. if ( idx > 0 ) {
  74. if ( eval('typeof ' +key+'_'+lang.substring(0,idx)) != 'undefined' ) {
  75. res = eval(key+'_'+lang.substring(0,idx));
  76. }
  77. }
  78. }
  79. if ( !res ) {
  80. res = '';
  81. } else if ( params ) {
  82. var i = 0;
  83. for ( i = 0; i < params.length; i++ ) {
  84. res = res.replace(new RegExp('\\{'+(i+1)+'\\}','g'),params[i]);
  85. }
  86. }
  87. return res;
  88. }
  89. function removeChildren(node) {
  90. if ( !node ) return;
  91. while ( node.hasChildNodes() ) {
  92. node.removeChild(node.firstChild);
  93. }
  94. }
  95. function replaceAttribute(node,attributeName,matchValue,replaceValue) {
  96. // NOTE nodeType 1 == Node.ELEMENT_NODE in sane browsers
  97. if ( node.nodeType == 1 && node.getAttribute(attributeName) != null ) {
  98. var attributeValue = node.getAttribute(attributeName);
  99. var re = new RegExp(matchValue);
  100. attributeValue = attributeValue.replace(re,replaceValue);
  101. node.setAttribute(attributeName,attributeValue);
  102. }
  103. if ( node.hasChildNodes() ) {
  104. var child = node.firstChild;
  105. while ( child != null ) {
  106. replaceAttribute(child,attributeName,matchValue,replaceValue);
  107. child = child.nextSibling;
  108. }
  109. }
  110. }
  111. function findNodeWithAttributeValue(node,attributeName,matchValue) {
  112. // NOTE nodeType 1 == Node.ELEMENT_NODE in sane browsers
  113. if ( node.nodeType == 1 && node.getAttribute(attributeName) != null ) {
  114. var attributeValue = node.getAttribute(attributeName);
  115. var re = new RegExp(matchValue);
  116. if ( attributeValue.search(re) >= 0 ) {
  117. return node;
  118. }
  119. }
  120. if ( node.hasChildNodes() ) {
  121. for ( var child = node.firstChild; child != null; child = child.nextSibling ) {
  122. var n = findNodeWithAttributeValue(child,attributeName,matchValue);
  123. if ( n != null ) {
  124. return n;
  125. }
  126. }
  127. }
  128. }
  129. /**
  130. * Event handler to trim <textarea> elements.
  131. *
  132. * This handler exists because browsers seem to freak out with XHTML
  133. * <textarea/> tags (without any content). Forms can install this
  134. * handler on all <textarea> objects by calling the
  135. * installTextAreaFocusHandler() method below.
  136. */
  137. function focusTrimTextArea(event) {
  138. if ( event.target && event.target.value && event.target.value.search(/^\s+$/) == 0 ) {
  139. event.target.value = '';
  140. }
  141. }
  142. /**
  143. * Sets the 'onfocus' handler for all <textarea> objects to the
  144. * focusTrimTextArea method.
  145. */
  146. function installTextAreaFocusHandler() {
  147. var textAreaNodes = document.getElementsByTagName('textarea');
  148. if ( textAreaNodes ) {
  149. for ( var i = 0; i < textAreaNodes.length; i++ ) {
  150. textAreaNodes.item(i).onfocus = focusTrimTextArea;
  151. }
  152. }
  153. }
  154. /**
  155. * Initialize <a rel="popup|..." popup links.
  156. *
  157. * Adapted from http://www.sitepoint.com/print/xhtml-strict-popups
  158. *
  159. * This allows us to code links without JS popups, but add popup
  160. * support to links dynamically as needed.
  161. */
  162. function popupWindows() {
  163. if ( !document.getElementsByTagName ) {
  164. // bummer, no support. links will open in window, not popup
  165. return;
  166. }
  167. var scrW = screen.availWidth;
  168. var scrH = screen.availHeight;
  169. var anchors = document.getElementsByTagName("a");
  170. for (var i = 0; i < anchors.length; i++) {
  171. var anchor = anchors[i];
  172. var rel = anchor.getAttribute("rel");
  173. if ( !(rel && rel.indexOf("popup|") == 0) ) {
  174. // not a popup link, skip
  175. continue;
  176. }
  177. var linkDest = anchor.getAttribute("href");
  178. var relSplit = rel.split("|");
  179. var windowAttributes = "";
  180. if (relSplit[1] > scrW) {
  181. pW = scrW - 10;
  182. } else {
  183. pW = relSplit[1];
  184. }
  185. if (relSplit[2] > scrH) {
  186. pH = scrH - 40;
  187. } else {
  188. pH = relSplit[2];
  189. }
  190. scrX = (scrW - pW - 10) * .5;
  191. scrY = (scrH - pH - 30) * .5;
  192. var windowAttributes = "width=" + pW + ",height=" + pH + ",left=" + scrX + ",top=" + scrY + ",screenX=" + scrX + ",screenY=" + scrY;
  193. windowAttributes += ",location=" + relSplit[4] + ",resizable=" + relSplit[4] + ",scrollbars=" + relSplit[4];
  194. anchor.onclick = popupWin(linkDest,windowAttributes);
  195. }
  196. }
  197. function popupWin(link,attribs) {
  198. return function(event) {
  199. window.open(link,'winPopup',attribs);
  200. return false;
  201. }
  202. }
  203. if ( !window.console ) {
  204. // mock console.log in browsers without native support
  205. var console = {
  206. log : function(o) {
  207. // ignore
  208. }
  209. };
  210. }
  211. var XwebLocale = new XwebLocaleClass();
  212. $(document).ready(function() {
  213. // look for our context path in the head/@profile attribute
  214. $('head').each(function() {
  215. var profile = $(this).attr('profile');
  216. if ( profile ) {
  217. var myContext = profile.match(/^(.*)\/profile.txt\?lang=(.*)$/);
  218. if ( myContext ) {
  219. webContext = myContext[1];
  220. myLang = myContext[2];
  221. }
  222. }
  223. });
  224. $.getJSON(webContext + '/messages.json',
  225. function(data){
  226. XwebLocale.initJson(data);
  227. });
  228. });