md_script.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. // md_script.js <!-- for Dokeos metadata/*.php -->
  2. // <!-- 2006/05/16 -->
  3. // Copyright (C) 2006 rene.haentjens@UGent.be - see metadata/md_funcs.php -->
  4. // Part 1: General funcs & Keyword Tree: copied (with modifs) from SelKwds.xsl
  5. String.prototype.trim = function()
  6. {
  7. return this.replace(/^\s*/,"").replace(/\s*$/,""); // \f\n\r\t\v
  8. } // Dave Anderson, dbforums.com/arch/195/2003/3/724117
  9. String.prototype.removeExtraSpaces = function()
  10. {
  11. return this.replace(/\s+/g, ' ').trim();
  12. }
  13. function makeWindow(url, htmlText)
  14. {
  15. var newWindow = window.open(url, '',
  16. 'toolbar=no, location=no, directories=no, status=yes, '+
  17. 'menubar=yes, scrollbars=yes, resizable=yes, ' +
  18. 'width=800, height=600, left=10, top=10');
  19. if (url == '') newWindow.document.write('<html><body>' +
  20. htmlText + '</body></html>');
  21. return newWindow;
  22. }
  23. function isNotW3C()
  24. {
  25. if (!document.getElementById)
  26. alert('Sorry, the buttons only work with W3C browsers. ' +
  27. 'Use FireFox or IE6 or Moz1.7 or type in keywords manually...');
  28. return !document.getElementById;
  29. }
  30. function openOrCloseHelp(btn)
  31. {
  32. if (isNotW3C()) return false;
  33. document.getElementById('moreHelp').className =
  34. (btn.value == "?") ? "dvo" : "dvc";
  35. btn.value = (btn.value == "?") ? "¿" : "?";
  36. }
  37. function hasTagAndClass(obj, tag, cl)
  38. {
  39. return obj.tagName && (obj.tagName.toUpperCase() == tag) &&
  40. (obj.className == cl);
  41. }
  42. function openOrClose(btn) // show or hide part of keyword-tree
  43. {
  44. var oldcl = (btn.value == "-") ? "dvo" : "dvc";
  45. var newcl = (oldcl == "dvo") ? "dvc" : "dvo";
  46. btn.value = (oldcl == "dvo") ? "+" : "-" ;
  47. var ch = btn.parentNode.childNodes; // opera crashes on with()
  48. for (var i = 0; i < ch.length; i++) // netscape requires .item
  49. if (hasTagAndClass(ch.item(i), 'DIV', oldcl))
  50. ch.item(i).className = newcl;
  51. }
  52. function openOrCloseHere(div, wrong) // show or hide recursively
  53. {
  54. var ch = div.childNodes;
  55. for (var i = 0; i < ch.length; i++)
  56. {
  57. var thisCh = ch.item(i);
  58. if (thisCh.className == 'btn' && thisCh.value == wrong)
  59. openOrClose(thisCh)
  60. else if (thisCh.className == 'dvo' || thisCh.className == 'dvc')
  61. openOrCloseHere(thisCh, wrong);
  62. }
  63. }
  64. function openOrCloseAll(btn) // show or hide whole keyword-tree
  65. {
  66. if (isNotW3C()) return false;
  67. var wrong = (btn.value == "--") ? "-" : "+" ;
  68. btn.value = (wrong == "-") ? "++" : "--";
  69. openOrCloseHere(btn.parentNode, wrong);
  70. }
  71. var selspans = new Array; // selected SPANs with keywords
  72. function deselect(span)
  73. {
  74. for (var s in selspans) if (selspans[s] == span) delete selspans[s];
  75. }
  76. function copyToClipboard(allKwds) // md_script: not used
  77. {
  78. if (window.clipboardData)
  79. {
  80. window.clipboardData.setData("Text", '<' + allKwds + '>\r\n');
  81. return;
  82. }
  83. netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
  84. var gClipboardHelper = Components.classes['@mozilla.org/widget/clipboardhelper;1']
  85. .getService(Components.interfaces.nsIClipboardHelper);
  86. gClipboardHelper.copyString('<' + allKwds + '>\n');
  87. }
  88. function getSelectedKeywords()
  89. {
  90. var sortedKwds = new Array, allKwds = '';
  91. for (var s in selspans)
  92. sortedKwds.push(selspans[s].parentNode.getAttribute('level')
  93. + '.' + selspans[s].innerHTML)
  94. sortedKwds.sort(); // according to level, which follows the tree
  95. for (var k in sortedKwds)
  96. {
  97. var someWords = sortedKwds[k];
  98. allKwds += ', ' + someWords.substr(someWords.indexOf('.')+1);
  99. }
  100. return allKwds.replace(/[,_\s]+/g, ', ').substr(2); // strip ', '
  101. }
  102. var orkwds; // array of ', kw, ' strings
  103. function selectOriginal(div)
  104. {
  105. var ch = div.childNodes;
  106. for (var i = 0; i < ch.length; i++)
  107. {
  108. var thisCh = ch.item(i); if (!thisCh.tagName) continue;
  109. thisTag = thisCh.tagName.toUpperCase();
  110. if (thisTag == 'SPAN')
  111. {
  112. var parkwds = ','+ thisCh.innerHTML.replace(/\s*/g,"") +',';
  113. for (var k = 0; k < orkwds.length; k++)
  114. if (parkwds.indexOf(orkwds[k]) >=0 )
  115. {
  116. deselect(thisCh); selspans.push(thisCh);
  117. thisCh.className = "lbs";
  118. openToTop(div, div.className); break;
  119. }
  120. }
  121. else if (thisTag == 'DIV')
  122. {
  123. selectOriginal(thisCh);
  124. }
  125. }
  126. }
  127. function openToTop(div, divclass)
  128. {
  129. if (!div.parentNode) return; var pdiv = div.parentNode;
  130. if (!pdiv.className) return; var pclass = pdiv.className;
  131. if (pclass != 'dvo' && pclass != 'dvc') return;
  132. if (divclass == 'dvc')
  133. {
  134. var ch = pdiv.childNodes;
  135. for (var i = 0; i < ch.length; i++)
  136. {
  137. var btn = ch.item(i);
  138. if (hasTagAndClass(btn, 'INPUT', 'btn'))
  139. if (btn.value == '+') openOrClose(btn);
  140. }
  141. }
  142. openToTop(pdiv, pclass);
  143. }
  144. function deselectAll(ev, btn)
  145. {
  146. if (isNotW3C()) return false;
  147. if (!ev) var ev = window.event;
  148. var kws = document.getElementById('kwds_string');
  149. for (var s in selspans) selspans[s].className = "lbl";
  150. selspans = new Array;
  151. document.getElementById('btnOpenOrCloseAll').value = "--";
  152. if (!ev.altKey) { kws.value = ''; return; }
  153. // md_script: the calling HTML should define var kwdswere!
  154. // in SelKwds.xsl, they are typed in by user or fetched from PPT
  155. var kwdsarray = kwdswere.split(','), allKwds = '';
  156. for (var k = 0; k < kwdsarray.length; k++)
  157. {
  158. var kwd = kwdsarray[k].trim();
  159. if (kwd.substr(0,1) == '-') kwd = kwd.substr(1);
  160. if (kwd != '') allKwds += '§,' + kwd + ',';
  161. }
  162. if (allKwds == '') return;
  163. orkwds = allKwds.substr(1).split('§');
  164. selectOriginal(btn.parentNode);
  165. allKwds = getSelectedKeywords(); kws.value = allKwds;
  166. // no copyToClipboard(allKwds);
  167. allKwds = ','+ allKwds.replace(/\s*/g,"") +','; var missing = '';
  168. for (k = 0; k < orkwds.length; k++)
  169. if (allKwds.indexOf(orkwds[k]) < 0 ) missing += orkwds[k];
  170. if (missing != '') alert('!= ' + missing.replace(/,+/g," "));
  171. }
  172. function selectOrDeselect(span, newcl)
  173. {
  174. span.className = newcl; deselect(span);
  175. if (newcl == "lbs") selspans.push(span);
  176. }
  177. function alsoParents(div, oldcl, newcl)
  178. {
  179. while (div.parentNode)
  180. {
  181. div = div.parentNode; var ch = div.childNodes;
  182. for (var i = 0; i < ch.length; i++)
  183. if (hasTagAndClass(ch.item(i), 'SPAN', oldcl))
  184. selectOrDeselect(ch.item(i), newcl);
  185. }
  186. }
  187. function spanClick(span, ev) // md_script: no parents in search
  188. {
  189. var mda = getObj("mda"); if (!ev) ev = window.event;
  190. if (ev.shiftKey && ev.altKey)
  191. {
  192. makeWindow('', '<pre>' + span.parentNode.innerHTML
  193. .replace(/&/g, "&amp;").replace(/"/g, "&quot;")
  194. .replace(/</g, "&lt;") .replace(/>/g, "&gt;") + '</pre>');
  195. return; // debugging...
  196. }
  197. if (ev.ctrlKey || span.className == "lbs")
  198. {
  199. selectOrDeselect(span, "lbl"); // deselect
  200. if (document.selection) document.selection.empty();
  201. if (ev.altKey) alsoParents(span.parentNode, "lbs", "lbl");
  202. }
  203. else
  204. {
  205. selectOrDeselect(span, "lbs"); // select (search: no parents)
  206. if (mda && !ev.altKey) alsoParents(span.parentNode, "lbl", "lbs");
  207. if (!mda && ev.altKey) // mda does not exist in search form
  208. if (span.innerHTML.substr(0, 1) == '-')
  209. span.innerHTML = (', ' + span.innerHTML).replace(/, -/g, ", ").substr(2);
  210. else span.innerHTML = (', ' + span.innerHTML).replace(/, /g, ", -").substr(2);
  211. }
  212. var allKwds = getSelectedKeywords(); // no copyToClipboard(allKwds);
  213. document.getElementById('kwds_string').value = allKwds;
  214. }
  215. var KWDS_ARRAY = new Array, nkw = 0, pU; // alphabetic list popup
  216. function makeAlphaList(div) // md_script: not used (hopefully)
  217. {
  218. var ch = div.childNodes;
  219. for (var i = 0; i < ch.length; i++)
  220. {
  221. var thisCh = ch.item(i); if (!thisCh.tagName) continue;
  222. thisTag = thisCh.tagName.toUpperCase();
  223. if (thisTag == 'SPAN')
  224. {
  225. var parkwds= thisCh.innerHTML.replace(/\s*/g,"").split(',');
  226. for (k in parkwds) KWDS_ARRAY[nkw++] = parkwds[k];
  227. }
  228. else if (thisTag == 'DIV') makeAlphaList(thisCh);
  229. }
  230. }
  231. function pU_show(anchor, offsetX, offsetY, defH) // XY: rel. to anchor
  232. {
  233. if (!anchor) return;
  234. thisx = anchor.offsetLeft; thisy = anchor.offsetTop;
  235. while ((anchor = anchor.offsetParent))
  236. {
  237. thisx += anchor.offsetLeft; thisy += anchor.offsetTop;
  238. }
  239. thisx += offsetX; thisy += offsetY;
  240. pU.style.left = thisx + "px";
  241. pU.style.top = thisy + "px";
  242. pU.style.height = defH; var maxH = pU.offsetHeight;
  243. for (var curH = 20; curH <= maxH; curH += 20)
  244. {
  245. pU.style.height = curH + 'px';
  246. if (curH >= pU.scrollHeight) break;
  247. }
  248. // scrollHeight can be smaller than current in IE, not in Moz
  249. pU.style.visibility = "visible";
  250. }
  251. function pU_hide()
  252. {
  253. if (pU) pU.style.visibility = "hidden";
  254. }
  255. function takeTypeIn(kws, oX, oY, defH)
  256. {
  257. if (isNotW3C()) return;
  258. if (!pU)
  259. {
  260. pU = document.getElementById('popup');
  261. if (!KWDS_ARRAY.length)
  262. {
  263. makeAlphaList(document.getElementById('maindiv'));
  264. KWDS_ARRAY.sort();
  265. }
  266. }
  267. if (!(curValue = kws.value.toLowerCase())) return;
  268. var kwLines = '';
  269. for (pos = 0; pos < KWDS_ARRAY.length; pos++)
  270. if (KWDS_ARRAY[pos].toLowerCase().indexOf(curValue) == 0)
  271. kwLines += '<div class="pul" onMouseOver="this.className=' +
  272. "'lbl'" + '"; onMouseOut="this.className=' + "'pul'" +
  273. '">' + KWDS_ARRAY[pos] + '</div>';
  274. if (kwLines == '') {pU_hide(); return; }
  275. pU.innerHTML = kwLines; pU_show(kws, oX, oY, defH);
  276. }
  277. function pU_clicked(ev)
  278. {
  279. if (!pU) return false; if (!ev) var ev = window.event;
  280. var t = (ev.srcElement) ? ev.srcElement : ev.originalTarget;
  281. try {var kw = t.innerHTML;} catch(exc) {return false;} // Moz
  282. while (true) try
  283. {
  284. if (t.id == pU.id) return kw ? kw : true; t = t.parentNode;
  285. }
  286. catch(exc) {return false;} // Moz: t.parentNode uncatched exc.
  287. }
  288. function pU_select(kw)
  289. {
  290. if (kw === true) return;
  291. var kws = document.getElementById('kwds_string');
  292. var maindiv = document.getElementById('maindiv');
  293. var ch = maindiv.childNodes;
  294. for (var i = 0; i < ch.length; i++)
  295. {
  296. var btn = ch.item(i);
  297. if (hasTagAndClass(btn, 'INPUT', 'btn'))
  298. if (btn.value == '+') openOrClose(btn);
  299. }
  300. orkwds = new Array(',' + kw + ','); selectOriginal(maindiv);
  301. kws.value = getSelectedKeywords(); // no copyToClipboard(kws.value);
  302. pU_hide();
  303. }
  304. // Part 2: Metadata Updates: W3C, IE4 and NS4 browsers
  305. function isValidChar(ev, pattern, flags)
  306. {
  307. // e.g. onKeyPress="return isValidChar(event, '[a-z]', 'i');"
  308. if (!ev) var ev = window.event; // PPK, see below
  309. var kc = (ev.keyCode) ? ev.keyCode : ev.which; // PPK
  310. return (new RegExp(pattern, flags)).test(String.fromCharCode(kc));
  311. // PPK= Peter-Paul Koch, www.quirksmode.org
  312. }
  313. function checkValid(inputField, pattern, flags)
  314. {
  315. // e.g. onKeyUp="checkValid(this, '^[a-z]{2,8}$', 'i');"
  316. var fieldColor = (new RegExp(pattern, flags)).test(inputField.value) ? '#000000' : '#D8366C';
  317. var fieldStyle = (document.getElementById || document.all) ?
  318. inputField.style : inputField;
  319. if (fieldStyle) fieldStyle.color = fieldColor;
  320. // OK for all browsers (see devedge.netscape.com
  321. // /library/xref/2003/css-support/css1/mastergrid.html):
  322. // color, background-color (not on NN4), display block/none (NN4?),
  323. // overflow hidden/scroll/auto (not on NN4),
  324. // position relative/static,
  325. // text-align left/right/center, text-indent,
  326. // font-style normal/italic, font-weight normal/bold,
  327. // font-family serif/sans-serif/monospace,
  328. // border-style none/solid/double/groove/ridge/inset/outset.
  329. }
  330. function getObj(name) // PPK
  331. {
  332. return (document.getElementById) ? document.getElementById(name)
  333. : (document.all) ? document.all[name] // IE4
  334. : (document.layers) ? document.layers[name] // NS4
  335. : null; // With NS4, nested layers are not supported!
  336. }
  337. function spc(path, value) // set pending change in form field mda
  338. {
  339. var mda = getObj("mda");
  340. if (mda) mda.value += "\n" + path + '=' + value;
  341. }
  342. function spcSel(path, selbox) // set pending change, language selection
  343. {
  344. var mda = getObj("mda");
  345. if (mda) mda.value += "\n" + path + '=' +
  346. selbox.options[selbox.selectedIndex].value;
  347. }
  348. function checkBeforeSubmit(ev)
  349. {
  350. if (!ev) var ev = window.event;
  351. if (ev.ctrlKey && ev.altKey)
  352. {
  353. var mdt = getObj("mdt"); if (!mdt) return false;
  354. makeWindow('', '<pre>' +
  355. ('<?xml version="1.0" encoding="UTF-16"?>' +
  356. ' <!-- From browser, save as Text/Unicode -->\n\n' +
  357. mdt.value)
  358. .replace(/&/g, '&amp;').replace(/"/g, '&quot;')
  359. .replace(/</g, '&lt;').replace(/>/g, '&gt;') +
  360. '</pre>');
  361. return false;
  362. }
  363. var kwdsnow = getObj("kwds_string"); if (!kwdsnow) return true;
  364. if (kwdsnow.value == kwdswere) return true; // unchanged
  365. // note: calling HTML should define var kwdswere!
  366. var language = kwdsnow.title;
  367. var mda = getObj("mda");
  368. if (!mda) { alert('? Form does not contain mda'); return false; }
  369. var kwdsarray = kwdswere.split(',');
  370. for (var k = 0; k < kwdsarray.length; k++) // delete old
  371. if (kwdsarray[k].trim() != '')
  372. mda.value += "\nmetadata/lom/general/keyword[-1]~";
  373. kwdsarray = kwdsnow.value
  374. .replace(/[!-,:-@\[-\^{-~\s]+/g, ',').split(',');
  375. for (k = 0; k < kwdsarray.length; k++)
  376. {
  377. var newkw = kwdsarray[k].trim();
  378. if (newkw != '') mda.value +=
  379. "\nmetadata/lom/general!keyword" +
  380. "\nmetadata/lom/general/keyword[-1]!string=" + newkw +
  381. "\nmetadata/lom/general/keyword[-1]/string/@language=" + language;
  382. }
  383. return true;
  384. }
  385. function setPendingOperation(op, ev)
  386. {
  387. if (!ev) var ev = window.event;
  388. var mda = getObj("mda");
  389. if (!mda) { alert('? Form does not contain mda'); return false; }
  390. if (op == '!!' || (op == '~~' && confirm(mda.title)))
  391. {
  392. mda.value = op; return true;
  393. }
  394. return false;
  395. }
  396. function prepSearch(ev)
  397. {
  398. if (!ev) var ev = window.event;
  399. var mdsc = getObj("mdsc");
  400. if (!mdsc) { alert('? Form does not contain mdsc'); return false; }
  401. var kwdsnow = getObj("kwds_string"); if (!kwdsnow) return true;
  402. if (kwdsnow.value == '') return true;
  403. if (!KWDS_ARRAY.length)
  404. {
  405. makeAlphaList(getObj('maindiv'));
  406. KWDS_ARRAY.sort();
  407. }
  408. var restricttokwds = false, checkbox = getObj("restricttokwds");
  409. if (checkbox) restricttokwds = checkbox.checked;
  410. var kwdsarray = kwdsnow.value
  411. .replace(/[!-,:-@\[-\^{-~\s]+/g, ',').split(',');
  412. for (var k = 0; k < kwdsarray.length; k++)
  413. {
  414. var newkw = kwdsarray[k].trim().toLowerCase();
  415. if (newkw != '')
  416. {
  417. var realkw = false;
  418. if (restricttokwds)
  419. for (pos = 0; pos < KWDS_ARRAY.length; pos++)
  420. if (KWDS_ARRAY[pos].toLowerCase() == newkw)
  421. { realkw = true; break; }
  422. mdsc.value += "\n" + newkw + (realkw ? '-kw' : '');
  423. }
  424. }
  425. return true;
  426. }
  427. var CRLF = "\n"; // generates clickable tree and populates KWDS_ARRAY
  428. function traverseKwObj(node, parlev, num) // see KWTREE_OBJECT in md_funcs
  429. {
  430. var curlev = '00' + (num+1), kwn = '', html = '';
  431. curlev = parlev + curlev.substr(curlev.length-3);
  432. for (i in (names = node.n.split("_")))
  433. if (nn = names[i]) { KWDS_ARRAY.push(nn); kwn += ', ' + nn; }
  434. for (j in node.c) html += traverseKwObj(node.c[j], curlev, Math.abs(j));
  435. return (parlev == '') ? html :
  436. '<div noWrap="1" class="dvc" level="' + curlev + '">' + CRLF +
  437. '<input type="button" class="' +
  438. (html ? 'btn" value="+" onClick="openOrClose(this);"/>' :
  439. 'lfn" value=" "/>') + '&#xa0;' + CRLF +
  440. '<span class="lbl" onClick="spanClick(this, event);"' +
  441. (node.pt ? ' title="' + node.pt + '">' : '>') +
  442. kwn.substr(2) + '</span>' + CRLF +
  443. (node.cm ? '<i>' + node.cm + '</i>' : '') +
  444. html +
  445. '</div>' + CRLF;
  446. }
  447. // The End