dhtmlXCommon.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Copyright Scand LLC http://www.scbr.com To use this component please contact
  3. * info@scbr.com to obtain license
  4. */
  5. /**
  6. * @desc: xmlLoader object
  7. * @type: private
  8. * @param: funcObject - xml parser function
  9. * @param: object - jsControl object
  10. * @topic: 0
  11. */
  12. function dtmlXMLLoaderObject(funcObject, dhtmlObject, async) {
  13. this.xmlDoc = "";
  14. if (arguments.length == 2) {
  15. this.async = true;
  16. } else {
  17. this.async = async;
  18. }
  19. this.onloadAction = funcObject || null;
  20. this.mainObject = dhtmlObject || null;
  21. return this;
  22. }
  23. /**
  24. * @desc: xml loading handler
  25. * @type: private
  26. * @param: dtmlObject - xmlLoader object
  27. * @topic: 0
  28. */
  29. dtmlXMLLoaderObject.prototype.waitLoadFunction = function(dhtmlObject) {
  30. this.check = function() {
  31. if (dhtmlObject.onloadAction != null) {
  32. if ((!dhtmlObject.xmlDoc.readyState)
  33. || (dhtmlObject.xmlDoc.readyState == 4)) {
  34. dhtmlObject.onloadAction(dhtmlObject.mainObject, null, null,
  35. null, dhtmlObject);
  36. dhtmlObject = null;
  37. }
  38. }
  39. };
  40. return this.check;
  41. };
  42. /**
  43. * @desc: return XML top node
  44. * @param: tagName - top XML node tag name (not used in IE, required for Safari
  45. * and Mozilla)
  46. * @type: private
  47. * @returns: top XML node
  48. * @topic: 0
  49. */
  50. dtmlXMLLoaderObject.prototype.getXMLTopNode = function(tagName) {
  51. if (this.xmlDoc.responseXML) {
  52. var temp = this.xmlDoc.responseXML.getElementsByTagName(tagName);
  53. var z = temp[0];
  54. } else {
  55. var z = this.xmlDoc.documentElement;
  56. }
  57. if (z) {
  58. return z;
  59. }
  60. if ((_isIE) && (!this._retry)) {
  61. // fall back to MS.XMLDOM
  62. this._retry = true;
  63. this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  64. this.xmlDoc.async = false;
  65. this.xmlDoc.load(this.filePath);
  66. return this.getXMLTopNode(tagName);
  67. }
  68. dhtmlxError.throwError("LoadXML", "Incorrect XML", [this.xmlDoc]);
  69. return document.createElement("DIV");
  70. };
  71. /**
  72. * @desc: load XML from string
  73. * @type: private
  74. * @param: xmlString - xml string
  75. * @topic: 0
  76. */
  77. dtmlXMLLoaderObject.prototype.loadXMLString = function(xmlString) {
  78. try {
  79. var parser = new DOMParser();
  80. this.xmlDoc = parser.parseFromString(xmlString, "text/xml");
  81. } catch (e) {
  82. this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  83. this.xmlDoc.async = this.async;
  84. this.xmlDoc.loadXML(xmlString);
  85. }
  86. this.onloadAction(this.mainObject);
  87. };
  88. /**
  89. * @desc: load XML
  90. * @type: private
  91. * @param: filePath - xml file path
  92. * @param: postMode - send POST request
  93. * @param: postVars - list of vars for post request
  94. * @topic: 0
  95. */
  96. dtmlXMLLoaderObject.prototype.loadXML = function(filePath, postMode, postVars) {
  97. this.filePath = filePath;
  98. try {
  99. this.xmlDoc = new XMLHttpRequest();
  100. this.xmlDoc.open(postMode ? "POST" : "GET", filePath, this.async);
  101. if (postMode) {
  102. // this.xmlDoc.setRequestHeader("Content-type",
  103. // "application/x-www-form-urlencoded");
  104. this.xmlDoc.setRequestHeader("Content-Type",
  105. "application/x-www-form-urlencoded;charset=utf-8");
  106. }
  107. else {
  108. this.xmlDoc.setRequestHeader("Content-Type",
  109. "application/x-www-form-urlencoded;charset=utf-8");
  110. }
  111. this.xmlDoc.onreadystatechange = new this.waitLoadFunction(this);
  112. this.xmlDoc.send(null || postVars);
  113. } catch (e) {
  114. if (document.implementation && document.implementation.createDocument) {
  115. this.xmlDoc = document.implementation.createDocument("", "", null);
  116. this.xmlDoc.onload = new this.waitLoadFunction(this);
  117. this.xmlDoc.load(filePath);
  118. } else {
  119. this.xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
  120. this.xmlDoc.open(postMode ? "POST" : "GET", filePath, this.async);
  121. if (postMode) {
  122. this.xmlDoc.setRequestHeader("Content-type",
  123. "application/x-www-form-urlencoded");
  124. }
  125. this.xmlDoc.onreadystatechange = new this.waitLoadFunction(this);
  126. this.xmlDoc.send(null || postVars);
  127. }
  128. }
  129. };
  130. /**
  131. * @desc: destructor, cleans used memory
  132. * @type: private
  133. * @topic: 0
  134. */
  135. dtmlXMLLoaderObject.prototype.destructor = function() {
  136. this.onloadAction = null;
  137. this.mainObject = null;
  138. this.xmlDoc = null;
  139. return null;
  140. };
  141. /**
  142. * @desc: Call wrapper
  143. * @type: private
  144. * @param: funcObject - action handler
  145. * @param: dhtmlObject - user data
  146. * @returns: function handler
  147. * @topic: 0
  148. */
  149. function callerFunction(funcObject, dhtmlObject) {
  150. this.handler = function(e) {
  151. if (!e) {
  152. e = event;
  153. }
  154. funcObject(e, dhtmlObject);
  155. return true;
  156. };
  157. return this.handler;
  158. }
  159. /**
  160. * @desc: Calculate absolute position of html object
  161. * @type: private
  162. * @param: htmlObject - html object
  163. * @topic: 0
  164. */
  165. function getAbsoluteLeft(htmlObject) {
  166. var xPos = htmlObject.offsetLeft;
  167. var temp = htmlObject.offsetParent;
  168. while (temp != null) {
  169. xPos += temp.offsetLeft;
  170. temp = temp.offsetParent;
  171. }
  172. return xPos;
  173. }
  174. /**
  175. * @desc: Calculate absolute position of html object
  176. * @type: private
  177. * @param: htmlObject - html object
  178. * @topic: 0
  179. */
  180. function getAbsoluteTop(htmlObject) {
  181. var yPos = htmlObject.offsetTop;
  182. var temp = htmlObject.offsetParent;
  183. while (temp != null) {
  184. yPos += temp.offsetTop;
  185. temp = temp.offsetParent;
  186. }
  187. return yPos;
  188. }
  189. /**
  190. * @desc: Convert string to it boolean representation
  191. * @type: private
  192. * @param: inputString - string for covertion
  193. * @topic: 0
  194. */
  195. function convertStringToBoolean(inputString) {
  196. if (typeof(inputString) == "string") {
  197. inputString = inputString.toLowerCase();
  198. }
  199. switch (inputString) {
  200. case "1" :
  201. case "true" :
  202. case "yes" :
  203. case "y" :
  204. case 1 :
  205. case true :
  206. return true;
  207. break;
  208. default :
  209. return false;
  210. }
  211. }
  212. /**
  213. * @desc: find out what symbol to use as url param delimiters in further params
  214. * @type: private
  215. * @param: str - current url string
  216. * @topic: 0
  217. */
  218. function getUrlSymbol(str) {
  219. if (str.indexOf("?") != -1) {
  220. return "&";
  221. } else {
  222. return "?";
  223. }
  224. }
  225. function dhtmlDragAndDropObject() {
  226. this.lastLanding = 0;
  227. this.dragNode = 0;
  228. this.dragStartNode = 0;
  229. this.dragStartObject = 0;
  230. this.tempDOMU = null;
  231. this.tempDOMM = null;
  232. this.waitDrag = 0;
  233. if (window.dhtmlDragAndDrop) {
  234. return window.dhtmlDragAndDrop;
  235. }
  236. window.dhtmlDragAndDrop = this;
  237. return this;
  238. }
  239. dhtmlDragAndDropObject.prototype.removeDraggableItem = function(htmlNode) {
  240. htmlNode.onmousedown = null;
  241. htmlNode.dragStarter = null;
  242. htmlNode.dragLanding = null;
  243. };
  244. dhtmlDragAndDropObject.prototype.addDraggableItem = function(htmlNode,
  245. dhtmlObject) {
  246. htmlNode.onmousedown = this.preCreateDragCopy;
  247. htmlNode.dragStarter = dhtmlObject;
  248. this.addDragLanding(htmlNode, dhtmlObject);
  249. };
  250. dhtmlDragAndDropObject.prototype.addDragLanding = function(htmlNode,
  251. dhtmlObject) {
  252. htmlNode.dragLanding = dhtmlObject;
  253. };
  254. dhtmlDragAndDropObject.prototype.preCreateDragCopy = function(e) {
  255. if (window.dhtmlDragAndDrop.waitDrag) {
  256. window.dhtmlDragAndDrop.waitDrag = 0;
  257. document.body.onmouseup = window.dhtmlDragAndDrop.tempDOMU;
  258. document.body.onmousemove = window.dhtmlDragAndDrop.tempDOMM;
  259. return false;
  260. }
  261. window.dhtmlDragAndDrop.waitDrag = 1;
  262. window.dhtmlDragAndDrop.tempDOMU = document.body.onmouseup;
  263. window.dhtmlDragAndDrop.tempDOMM = document.body.onmousemove;
  264. window.dhtmlDragAndDrop.dragStartNode = this;
  265. window.dhtmlDragAndDrop.dragStartObject = this.dragStarter;
  266. document.body.onmouseup = window.dhtmlDragAndDrop.preCreateDragCopy;
  267. document.body.onmousemove = window.dhtmlDragAndDrop.callDrag;
  268. if ((e) && (e.preventDefault)) {
  269. e.preventDefault();
  270. return false;
  271. }
  272. return false;
  273. };
  274. dhtmlDragAndDropObject.prototype.callDrag = function(e) {
  275. if (!e) {
  276. e = window.event;
  277. }
  278. dragger = window.dhtmlDragAndDrop;
  279. if ((e.button == 0) && (_isIE)) {
  280. return dragger.stopDrag();
  281. }
  282. if (!dragger.dragNode) {
  283. dragger.dragNode = dragger.dragStartObject._createDragNode(
  284. dragger.dragStartNode, e);
  285. if (!dragger.dragNode) {
  286. return dragger.stopDrag();
  287. }
  288. dragger.gldragNode = dragger.dragNode;
  289. document.body.appendChild(dragger.dragNode);
  290. document.body.onmouseup = dragger.stopDrag;
  291. dragger.waitDrag = 0;
  292. dragger.dragNode.pWindow = window;
  293. dragger.initFrameRoute();
  294. }
  295. if (dragger.dragNode.parentNode != window.document.body) {
  296. var grd = dragger.gldragNode;
  297. if (dragger.gldragNode.old) {
  298. grd = dragger.gldragNode.old;
  299. }
  300. // if (!document.all) dragger.calculateFramePosition();
  301. grd.parentNode.removeChild(grd);
  302. var oldBody = dragger.dragNode.pWindow;
  303. if (_isIE) {
  304. var div = document.createElement("Div");
  305. div.innerHTML = dragger.dragNode.outerHTML;
  306. dragger.dragNode = div.childNodes[0];
  307. } else {
  308. dragger.dragNode = dragger.dragNode.cloneNode(true);
  309. }
  310. dragger.dragNode.pWindow = window;
  311. dragger.gldragNode.old = dragger.dragNode;
  312. document.body.appendChild(dragger.dragNode);
  313. oldBody.dhtmlDragAndDrop.dragNode = dragger.dragNode;
  314. }
  315. dragger.dragNode.style.left = e.clientX + 15
  316. + (dragger.fx ? dragger.fx * (-1) : 0)
  317. + (document.body.scrollLeft || document.documentElement.scrollLeft)
  318. + "px";
  319. dragger.dragNode.style.top = e.clientY + 3
  320. + (dragger.fy ? dragger.fy * (-1) : 0)
  321. + (document.body.scrollTop || document.documentElement.scrollTop)
  322. + "px";
  323. if (!e.srcElement) {
  324. var z = e.target;
  325. } else {
  326. z = e.srcElement;
  327. }
  328. dragger.checkLanding(z, e.clientX, e.clientY);
  329. };
  330. dhtmlDragAndDropObject.prototype.calculateFramePosition = function(n) {
  331. // this.fx = 0, this.fy = 0;
  332. if (window.name) {
  333. var el = parent.frames[window.name].frameElement.offsetParent;
  334. var fx = 0;
  335. var fy = 0;
  336. while (el) {
  337. fx += el.offsetLeft;
  338. fy += el.offsetTop;
  339. el = el.offsetParent;
  340. }
  341. if ((parent.dhtmlDragAndDrop)) {
  342. var ls = parent.dhtmlDragAndDrop.calculateFramePosition(1);
  343. fx += ls.split("_")[0] * 1;
  344. fy += ls.split("_")[1] * 1;
  345. }
  346. if (n) {
  347. return fx + "_" + fy;
  348. } else {
  349. this.fx = fx;
  350. }
  351. this.fy = fy;
  352. }
  353. return "0_0";
  354. };
  355. dhtmlDragAndDropObject.prototype.checkLanding = function(htmlObject, x, y) {
  356. if ((htmlObject) && (htmlObject.dragLanding)) {
  357. if (this.lastLanding) {
  358. this.lastLanding.dragLanding._dragOut(this.lastLanding);
  359. }
  360. this.lastLanding = htmlObject;
  361. this.lastLanding = this.lastLanding.dragLanding._dragIn(
  362. this.lastLanding, this.dragStartNode, x, y);
  363. } else {
  364. if ((htmlObject) && (htmlObject.tagName != "BODY")) {
  365. this.checkLanding(htmlObject.parentNode, x, y);
  366. } else {
  367. if (this.lastLanding) {
  368. this.lastLanding.dragLanding._dragOut(this.lastLanding, x, y);
  369. }
  370. this.lastLanding = 0;
  371. }
  372. }
  373. };
  374. dhtmlDragAndDropObject.prototype.stopDrag = function(e, mode) {
  375. dragger = window.dhtmlDragAndDrop;
  376. if (!mode) {
  377. dragger.stopFrameRoute();
  378. var temp = dragger.lastLanding;
  379. dragger.lastLanding = null;
  380. if (temp) {
  381. temp.dragLanding._drag(dragger.dragStartNode,
  382. dragger.dragStartObject, temp);
  383. }
  384. }
  385. dragger.lastLanding = null;
  386. if ((dragger.dragNode) && (dragger.dragNode.parentNode == document.body)) {
  387. dragger.dragNode.parentNode.removeChild(dragger.dragNode);
  388. }
  389. dragger.dragNode = 0;
  390. dragger.gldragNode = 0;
  391. dragger.fx = 0;
  392. dragger.fy = 0;
  393. dragger.dragStartNode = 0;
  394. dragger.dragStartObject = 0;
  395. document.body.onmouseup = dragger.tempDOMU;
  396. document.body.onmousemove = dragger.tempDOMM;
  397. dragger.tempDOMU = null;
  398. dragger.tempDOMM = null;
  399. dragger.waitDrag = 0;
  400. };
  401. dhtmlDragAndDropObject.prototype.stopFrameRoute = function(win) {
  402. if (win) {
  403. window.dhtmlDragAndDrop.stopDrag(1, 1);
  404. }
  405. for (var i = 0; i < window.frames.length; i++) {
  406. if ((window.frames[i] != win) && (window.frames[i].dhtmlDragAndDrop)) {
  407. window.frames[i].dhtmlDragAndDrop.stopFrameRoute(window);
  408. }
  409. }
  410. if ((parent.dhtmlDragAndDrop) && (parent != window) && (parent != win)) {
  411. parent.dhtmlDragAndDrop.stopFrameRoute(window);
  412. }
  413. };
  414. dhtmlDragAndDropObject.prototype.initFrameRoute = function(win, mode) {
  415. if (win) {
  416. window.dhtmlDragAndDrop.preCreateDragCopy();
  417. window.dhtmlDragAndDrop.dragStartNode = win.dhtmlDragAndDrop.dragStartNode;
  418. window.dhtmlDragAndDrop.dragStartObject = win.dhtmlDragAndDrop.dragStartObject;
  419. window.dhtmlDragAndDrop.dragNode = win.dhtmlDragAndDrop.dragNode;
  420. window.dhtmlDragAndDrop.gldragNode = win.dhtmlDragAndDrop.dragNode;
  421. window.document.body.onmouseup = window.dhtmlDragAndDrop.stopDrag;
  422. window.waitDrag = 0;
  423. if (((!_isIE) && (mode)) && ((!_isFF) || (_FFrv < 1.8))) {
  424. window.dhtmlDragAndDrop.calculateFramePosition();
  425. }
  426. }
  427. if ((parent.dhtmlDragAndDrop) && (parent != window) && (parent != win)) {
  428. parent.dhtmlDragAndDrop.initFrameRoute(window);
  429. }
  430. for (var i = 0; i < window.frames.length; i++) {
  431. if ((window.frames[i] != win) && (window.frames[i].dhtmlDragAndDrop)) {
  432. window.frames[i].dhtmlDragAndDrop.initFrameRoute(window,
  433. ((!win || mode) ? 1 : 0));
  434. }
  435. }
  436. };
  437. var _isFF = false;
  438. var _isIE = false;
  439. var _isOpera = false;
  440. var _isKHTML = false;
  441. var _isMacOS = false;
  442. if (navigator.userAgent.indexOf("Macintosh") != -1) {
  443. _isMacOS = true;
  444. }
  445. if ((navigator.userAgent.indexOf("Safari") != -1)
  446. || (navigator.userAgent.indexOf("Konqueror") != -1)) {
  447. _isKHTML = true;
  448. } else {
  449. if (navigator.userAgent.indexOf("Opera") != -1) {
  450. _isOpera = true;
  451. } else {
  452. if (navigator.appName.indexOf("Microsoft") != -1) {
  453. _isIE = true;
  454. } else {
  455. _isFF = true;
  456. var _FFrv = parseFloat(navigator.userAgent.split("rv:")[1]);
  457. }
  458. }
  459. }
  460. // deprecated, use global constant instead
  461. // determines if current browser is IE
  462. function isIE() {
  463. if (navigator.appName.indexOf("Microsoft") != -1) {
  464. if (navigator.userAgent.indexOf("Opera") == -1) {
  465. return true;
  466. }
  467. }
  468. return false;
  469. }
  470. // multibrowser Xpath processor
  471. dtmlXMLLoaderObject.prototype.doXPath = function(xpathExp, docObj) {
  472. if ((_isOpera) || (_isKHTML)) {
  473. return this.doXPathOpera(xpathExp, docObj);
  474. }
  475. if (_isIE) {// IE
  476. if (!docObj) {
  477. docObj = this.xmlDoc;
  478. }
  479. return docObj.selectNodes(xpathExp);
  480. } else {// Mozilla
  481. var nodeObj = docObj;
  482. if (!docObj) {
  483. if (!this.xmlDoc.nodeName) {
  484. docObj = this.xmlDoc.responseXML;
  485. } else {
  486. docObj = this.xmlDoc;
  487. }
  488. }
  489. if (docObj.nodeName.indexOf("document") != -1) {
  490. nodeObj = docObj;
  491. } else {
  492. nodeObj = docObj;
  493. docObj = docObj.ownerDocument;
  494. }
  495. var rowsCol = new Array();
  496. var col = docObj.evaluate(xpathExp, nodeObj, null,
  497. XPathResult.ANY_TYPE, null);
  498. var thisColMemb = col.iterateNext();
  499. while (thisColMemb) {
  500. rowsCol[rowsCol.length] = thisColMemb;
  501. thisColMemb = col.iterateNext();
  502. }
  503. return rowsCol;
  504. }
  505. };
  506. if ((window.Node) && (!_isKHTML)) {
  507. Node.prototype.removeNode = function(removeChildren) {
  508. var self = this;
  509. if (Boolean(removeChildren)) {
  510. return this.parentNode.removeChild(self);
  511. } else {
  512. var range = document.createRange();
  513. range.selectNodeContents(self);
  514. return this.parentNode.replaceChild(range.extractContents(), self);
  515. }
  516. };
  517. }
  518. function _dhtmlxError(type, name, params) {
  519. if (!this.catches) {
  520. this.catches = new Array();
  521. }
  522. return this;
  523. }
  524. _dhtmlxError.prototype.catchError = function(type, func_name) {
  525. this.catches[type] = func_name;
  526. };
  527. _dhtmlxError.prototype.throwError = function(type, name, params) {
  528. if (this.catches[type]) {
  529. return this.catches[type](type, name, params);
  530. }
  531. if (this.catches["ALL"]) {
  532. return this.catches["ALL"](type, name, params);
  533. }
  534. alert("Error type: " + arguments[0] + "\nDescription: " + arguments[1]);
  535. return null;
  536. };
  537. window.dhtmlxError = new _dhtmlxError();
  538. // opera fake, while 9.0 not released
  539. // multibrowser Xpath processor
  540. dtmlXMLLoaderObject.prototype.doXPathOpera = function(xpathExp, docObj) {
  541. // this is fake for Opera
  542. var z = xpathExp.replace(/[\/]+/gi, "/").split("/");
  543. var obj = null;
  544. var i = 1;
  545. if (!z.length) {
  546. return [];
  547. }
  548. if (z[0] == ".") {
  549. obj = [docObj];
  550. } else {
  551. if (z[0] == "") {
  552. obj = this.xmlDoc.responseXML.getElementsByTagName(z[i].replace(
  553. /\[[^\]]*\]/g, ""));
  554. i++;
  555. } else {
  556. return [];
  557. }
  558. }
  559. for (i; i < z.length; i++) {
  560. obj = this._getAllNamedChilds(obj, z[i]);
  561. }
  562. if (z[i - 1].indexOf("[") != -1) {
  563. obj = this._filterXPath(obj, z[i - 1]);
  564. }
  565. return obj;
  566. };
  567. dtmlXMLLoaderObject.prototype._filterXPath = function(a, b) {
  568. var c = new Array();
  569. var b = b.replace(/[^\[]*\[\@/g, "").replace(/[\[\]\@]*/g, "");
  570. for (var i = 0; i < a.length; i++) {
  571. if (a[i].getAttribute(b)) {
  572. c[c.length] = a[i];
  573. }
  574. }
  575. return c;
  576. };
  577. dtmlXMLLoaderObject.prototype._getAllNamedChilds = function(a, b) {
  578. var c = new Array();
  579. for (var i = 0; i < a.length; i++) {
  580. for (var j = 0; j < a[i].childNodes.length; j++) {
  581. if (a[i].childNodes[j].tagName == b) {
  582. c[c.length] = a[i].childNodes[j];
  583. }
  584. }
  585. }
  586. return c;
  587. };