dragDropHandler.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /**************************************************************************
  2. * Licensed Materials - Property of IBM *
  3. * *
  4. * IBM Cognos Products: AGS *
  5. * *
  6. * (C) Copyright IBM Corp. 2005, 2018 *
  7. * *
  8. * US Government Users Restricted Rights - Use, duplication or disclosure *
  9. * restricted by GSA ADP Schedule Contract with IBM Corp. *
  10. **************************************************************************/
  11. var cf = parent.getConfigFrame();
  12. // information about a dropZone
  13. function DropZone(id, menuString, defaultText, updateAgentItems, tab_id,overwrite,allowFunctionsAndParams,elementProcessor){
  14. this.id = id;
  15. this.tab_id = tab_id;
  16. this.menuString = menuString;
  17. this.defaultText = defaultText;
  18. this.updateAgentItems = updateAgentItems;
  19. this.overwrite = overwrite;
  20. this.allowFunctionsAndParams = allowFunctionsAndParams;
  21. this.elementProcessor = elementProcessor;
  22. }
  23. function DropZoneHandler()
  24. {
  25. // save some values
  26. this.registeredObjectsIndex = new Object();
  27. }
  28. // export save the id of the element that we're inserting into
  29. DropZoneHandler.prototype.saveId = function(id)
  30. {
  31. this.id = id;
  32. }
  33. //this method is for dynamicly named groups of dropzones, that may have implicit agent item deletion
  34. //if one of them vanishes
  35. DropZoneHandler.prototype.registerDropZoneGroup = function(dropzones, dropzone_group_id)
  36. {
  37. var fields = new Array();
  38. for(var i = 0; dropzones && dropzones.length && i < dropzones.length; i++)
  39. {
  40. //this will probably have been made already in the normal register dropzone method
  41. var fieldContent = parent.getAgentItemsListener().getFieldContent(dropzones[i].id, dropzone_group_id);
  42. //field content not vital for hte drop zone group members
  43. fields.push(fieldContent ? fieldContent : new parent.FieldContent(dropzones[i].id, dropzone_group_id, ""));
  44. }
  45. parent.getAgentItemsListener().registerDynamicGroup(fields, dropzone_group_id, window.name);
  46. }
  47. //handy method to use the new Dropzone object
  48. //cant overload in script... so use new name
  49. DropZoneHandler.prototype.registerDropZoneObject = function(dropzone)
  50. {
  51. this.registerDropZone(dropzone.id, dropzone.menuString, dropzone.defaultText, dropzone.updateAgentItems,
  52. dropzone.tab_id,dropzone.overwrite,dropzone.allowFunctionsAndParams,dropzone.elementProcessor);
  53. }
  54. //check the id is a dropzone
  55. DropZoneHandler.prototype.isDropZone = function(anId)
  56. {
  57. return (this.registeredObjectsIndex[anId] && this.registeredObjectsIndex[anId] != 'undefined');
  58. }
  59. //check the id is a dropzone and if so is it one that overwrites entries with a drop
  60. DropZoneHandler.prototype.isOverWrite = function(anId)
  61. {
  62. return (this.registeredObjectsIndex[anId] && this.registeredObjectsIndex[anId].overwrite && this.registeredObjectsIndex[anId].overwrite == true);
  63. }
  64. // set the following methods as part of the dropZoneHandler
  65. DropZoneHandler.prototype.registerDropZone = function(id, menuString, defaultText, updateAgentItems, task_id, overwrite, allowFunctionsAndParams,elementProcessor){
  66. if (document.forms[0] != null) {
  67. // create the onsubmit handler
  68. if (!this.registeredObjectsIndex.submit) {
  69. this.registeredObjectsIndex.submit = document.forms[0].submit;
  70. document.forms[0].submit = submitHandler;
  71. }
  72. }
  73. attachDropEvents(id);
  74. this.registeredObjectsIndex[id] = elementProcessor;
  75. // create an entry in the registeredObjectsIndex
  76. if (!this.registeredObjectsIndex[id] || this.registeredObjectsIndex[id] == 'undefined') {
  77. elementProcessor = new ElementProcessor(id,defaultText,overwrite);
  78. this.registeredObjectsIndex[id] = elementProcessor;
  79. }
  80. if (allowFunctionsAndParams && allowFunctionsAndParams != 'undefined' && allowFunctionsAndParams != '') {
  81. elementProcessor.allowFunctionsAndParams = allowFunctionsAndParams;
  82. }
  83. if (updateAgentItems && updateAgentItems != 'undefined' && updateAgentItems != '') {
  84. elementProcessor.updateAgentItems = updateAgentItems;
  85. }
  86. if (elementProcessor.isEmpty && (typeof elementProcessor.isEmpty === "function") && elementProcessor.isEmpty()) {
  87. elementProcessor.setDefaultValue();
  88. }
  89. //register for manual agent item deletion
  90. parent.getAgentItemsListener().registerField(id, task_id, elementProcessor.getElementValue(), window.name);
  91. }
  92. DropZoneHandler.prototype.registerPreEventHandler = function(id, event, func)
  93. {
  94. var registeredObject = this.registeredObjectsIndex[id];
  95. registeredObject["pre" + event] = func;
  96. }
  97. DropZoneHandler.prototype.registerPostEventHandler = function(id, event, func)
  98. {
  99. var registeredObject = this.registeredObjectsIndex[id];
  100. registeredObject["post" + event] = func;
  101. }
  102. DropZoneHandler.prototype.registerDropHandler = function(id, func)
  103. {
  104. var registeredObject = this.registeredObjectsIndex[id];
  105. registeredObject["dropfunc"] = func;
  106. }
  107. DropZoneHandler.prototype.registerUpdateValueHandler = function(id, func)
  108. {
  109. var registeredObject = this.registeredObjectsIndex[id];
  110. registeredObject["updatevaluefunc"] = func;
  111. }
  112. DropZoneHandler.prototype.handleSubmit = function(event)
  113. {
  114. // loop over all the value that we've got registered
  115. for (var id in this.registeredObjectsIndex)
  116. {
  117. var registeredObject = this.registeredObjectsIndex[id];
  118. if (registeredObject.removeDefaultValue) {
  119. // set to nothing
  120. registeredObject.removeDefaultValue();
  121. }
  122. }
  123. // call the correct function
  124. var submit_function = this.registeredObjectsIndex.submit;
  125. // give this back to the form object
  126. document.forms[0].submit = submit_function;
  127. // now call submit on the form
  128. document.forms[0].submit();
  129. }
  130. DropZoneHandler.prototype.handleOnDragOver = function(event)
  131. {
  132. // have to create a textRange for the entity
  133. var id = parent.agsFormUtils.getEventId(event);
  134. var srcElem = parent.agsFormUtils.getElementByIdOrName(id);
  135. var tagName = srcElem?srcElem.tagName:"";
  136. var registeredObject = this.registeredObjectsIndex[id];
  137. //Make sure we are dropping into an element we have registered
  138. if (!registeredObject || registeredObject.tagName != tagName) {
  139. return;
  140. }
  141. // if we're in default mode then do nothing
  142. registeredObject.setDropLocation(event);
  143. // have to prevent the default handling
  144. event.returnValue = false;
  145. }
  146. DropZoneHandler.prototype.handleOnDragEnter = function(event)
  147. {
  148. // set the cursor shape
  149. event.dataTransfer.dropEffect="move";
  150. // have to prevent the default handling
  151. event.returnValue = false;
  152. }
  153. DropZoneHandler.prototype.handleCancelDefault = function(event)
  154. {
  155. // just override the default handling of the drag
  156. event.returnValue = false;
  157. }
  158. /**
  159. * This function will take a given id and set it as the
  160. * default droppable one and then insert into it.
  161. * If id is not defined we use the already saved one.
  162. */
  163. DropZoneHandler.prototype.handleInsert = function(id) {
  164. //We have an id so use it.
  165. if (id && id != 'undefined') {
  166. this.saveId(id);
  167. }
  168. //Make sure we have an element id that we may have visited.
  169. if (this.id && this.id != 'undefined') {
  170. this.handleDrop(null);
  171. }
  172. }
  173. DropZoneHandler.prototype.handleDrop = function(event)
  174. {
  175. //check that drag originated from tree.
  176. if (!cf.cfgGet("selectedTreeNodes")) {
  177. return;
  178. }
  179. // it's possible that we're dropping into more than a single
  180. // frame - and yet the correct drop handler is called cause it's
  181. // attached to the individual elements in the frames - so save the
  182. // frame that we're dropping into
  183. cf.dropFrame = window.name;
  184. // get the id of the element that we're dropping into
  185. //IE calls the ondrop event before the onfocus.
  186. // get the id of the element that we're dropping into
  187. //if no event then we use the id, this is to handle the insert
  188. var id = event && event != 'undefined'?parent.agsFormUtils.getEventId(event):this.id;
  189. //Save it for later usage.
  190. this.saveId(id);
  191. var registeredObject = this.registeredObjectsIndex[id];
  192. if (registeredObject["preondrop"] != null) {
  193. registeredObject["preondrop"]();
  194. }
  195. this.process();
  196. if (registeredObject["postondrop"] != null) {
  197. registeredObject["postondrop"]();
  198. }
  199. if (registeredObject.isEmpty()) {
  200. // check to make sure something was entered
  201. // set the default value
  202. registeredObject.setDefaultValue();
  203. }
  204. }
  205. DropZoneHandler.prototype.handleOnFocus = function(event)
  206. {
  207. // it's possible that we're dropping into more than a single
  208. // frame - and yet the correct drop handler is called cause it's
  209. // attached to the individual elements in the frames - so save the
  210. // frame that we're dropping into
  211. cf.dropFrame = window.name;
  212. // get the id of the element that got the focus
  213. // get the id of the element that we're dropping into
  214. var id = parent.agsFormUtils.getEventId(event);
  215. //hang on to this so that the double click insert from meta data tree knows what to insert to
  216. this.saveId(id);
  217. var registeredObject = this.registeredObjectsIndex[id];
  218. if (registeredObject["preonfocus"] != null) {
  219. registeredObject["preonfocus"]();
  220. }
  221. registeredObject.removeDefaultValue();
  222. if (registeredObject["postonfocus"] != null) {
  223. registeredObject["postonfocus"]();
  224. }
  225. // stop the event propogation
  226. event.cancelBubble = true;
  227. }
  228. DropZoneHandler.prototype.handleOnBlur = function(event)
  229. {
  230. // get the id of the element that we're leaving
  231. var id = parent.agsFormUtils.getEventId(event);
  232. var registeredObject = this.registeredObjectsIndex[id];
  233. if (registeredObject["preonblur"] != null) {
  234. registeredObject["preonblur"](id);
  235. }
  236. // if we do not have a default value there at the moment, then set it
  237. if (registeredObject.isEmpty()) {
  238. registeredObject.setDefaultValue();
  239. }
  240. if (registeredObject["postonblur"] != null) {
  241. registeredObject["postonblur"](id);
  242. }
  243. // put this here - the post on blur handler the ability to alter the contents
  244. // of the field - so we have to wait till right at the last moment to check
  245. // the contents of the field.
  246. parent.getAgentItemsListener().updateField(id, this.getElementValue(id));
  247. }
  248. DropZoneHandler.prototype.process = function(item)
  249. {
  250. if (!this.id || this.id == 'undefined') {
  251. return;
  252. }
  253. // get the registered object if there is one
  254. var registeredObject = this.registeredObjectsIndex[this.id];
  255. var selectedNodesHolder = cf.cfgGet("selectedTreeNodes");
  256. var allowDrop = selectedNodesHolder.containsFunctions || selectedNodesHolder.containsParams;
  257. if (!registeredObject || !isFocusable(registeredObject.element) || (!registeredObject.allowFunctionsAndParams && allowDrop)) {
  258. return;
  259. }
  260. //this causes the action listener to roll all edits into one reversable action
  261. cf.getRedoUndoManager().startEditActionBatch();
  262. // get what we want to drop
  263. var dropText = processSelectedNodes();
  264. //Nothing to drop.
  265. if (!dropText || dropText.length < 1) {
  266. return;
  267. }
  268. registeredObject.removeDefaultValue();
  269. //This should be removed later on. The caller should create its own
  270. //drop thingy that will regsietered and used by drop zone handler.
  271. //For now keep the same functionality
  272. if (registeredObject["dropfunc"] != null) {
  273. // send the data to the function
  274. registeredObject["dropfunc"](dropText.join(""),this.id);
  275. } else {
  276. registeredObject.process(dropText);
  277. }
  278. cf.getRedoUndoManager().stopEditActionBatch();
  279. }
  280. DropZoneHandler.prototype.getDropZoneValue = function(id)
  281. {
  282. var id = (!id || id == 'undefined')?this.id:id;
  283. var registeredObject = this.registeredObjectsIndex[id];
  284. var value = "";
  285. if (registeredObject) {
  286. // if it's defaulted
  287. if (!registeredObject.isDefaultMode()) {
  288. value = registeredObject.getElementValue();
  289. }
  290. }
  291. return value;
  292. }
  293. DropZoneHandler.prototype.setDropZoneValue = function(value, id)
  294. {
  295. var id = (!id || id == 'undefined')?this.id:id;
  296. var registeredObject = this.registeredObjectsIndex[id];
  297. // set the value into the object
  298. // if it's defaulted
  299. if (registeredObject) {
  300. // set the value
  301. registeredObject.setElementValue(value);
  302. }
  303. // put this here - update the contents of the field.
  304. parent.getAgentItemsListener().updateField(id, this.getElementValue(id));
  305. }
  306. DropZoneHandler.prototype.updateDropZoneValue = function(value, id)
  307. {
  308. var id = (!id || id == 'undefined')?this.id:id;
  309. var registeredObject = this.registeredObjectsIndex[id];
  310. // set the value into the object
  311. // if it's defaulted
  312. if (registeredObject) {
  313. // set the value
  314. registeredObject.updateElementValue(value);
  315. }
  316. // put this here - update the contents of the field.
  317. parent.getAgentItemsListener().updateField(id, this.getElementValue(id));
  318. cf.agentHasChanged(true);
  319. }
  320. DropZoneHandler.prototype.setDefault = function(id)
  321. {
  322. var id = (!id || id == 'undefined')?this.id:id;
  323. var registeredObject = this.registeredObjectsIndex[id];
  324. if (registeredObject) {
  325. registeredObject.setDefaultValue();
  326. }
  327. }
  328. DropZoneHandler.prototype.removeDefault = function(id)
  329. {
  330. var id = (!id || id == 'undefined')?this.id:id;
  331. var registeredObject = this.registeredObjectsIndex[id];
  332. if (registeredObject) {
  333. registeredObject.removeDefaultValue();
  334. }
  335. }
  336. DropZoneHandler.prototype.dropZoneDefaulted = function(id)
  337. {
  338. var id = (!id || id == 'undefined')?this.id:id;
  339. var registeredObject = this.registeredObjectsIndex[id];
  340. if (registeredObject != null) {
  341. // set the display default flag
  342. return registeredObject.isDefaultMode();
  343. }
  344. return null;
  345. }
  346. DropZoneHandler.prototype.updateAgentItems = function(id)
  347. {
  348. var id = (!id || id == 'undefined')?this.id:id;
  349. // check again
  350. if (id != null) {
  351. var registeredObject = this.registeredObjectsIndex[id];
  352. if (registeredObject != null) {
  353. // check to see if we should add to the agent items tree
  354. return registeredObject.updateAgentItems;
  355. }
  356. }
  357. return false;
  358. }
  359. function submitHandler(event) {
  360. // forward on to the object
  361. droppy.handleSubmit(event);
  362. }
  363. function dropHandler(event) {
  364. // foward on to the object
  365. droppy.handleDrop(event);
  366. }
  367. function cancelDefaultHandler(event) {
  368. // forward on to the object
  369. droppy.handleCancelDefault(event);
  370. }
  371. function onDragEnterHandler(event)
  372. {
  373. // forward to the object
  374. droppy.handleOnDragEnter(event);
  375. }
  376. function onDragOverHandler(event) {
  377. // forward to the object
  378. droppy.handleOnDragOver(event);
  379. }
  380. function onFocusHandler(event) {
  381. // forward to the object
  382. droppy.handleOnFocus(event);
  383. }
  384. function onBlurHandler(event) {
  385. // forward on to the object
  386. droppy.handleOnBlur(event);
  387. }
  388. function attachDropEvents(id) {
  389. var element = parent.agsFormUtils.getElementByIdOrName(id);;
  390. if (element)
  391. {
  392. if (cf.browserCheck.isIE5Up())
  393. {
  394. element.attachEvent("ondrop",dropHandler);
  395. element.attachEvent("ondragenter",onDragEnterHandler);
  396. element.attachEvent("ondragover",onDragOverHandler);
  397. element.attachEvent("onblur", onBlurHandler);
  398. }
  399. else if (cf.browserCheck.isNav6Up())
  400. {
  401. element.addEventListener("drop",dropHandler,true);
  402. element.addEventListener("dragenter",cancelDefaultHandler, true);
  403. element.addEventListener("dragover",cancelDefaultHandler, true);
  404. element.addEventListener("blur", onBlurHandler, true);
  405. }
  406. parent.eventHandlerChainUtil.doEventHandlerChain(element, "focus", onFocusHandler);
  407. }
  408. }
  409. /**
  410. * Processes selected nodes and returns the drop text.
  411. */
  412. function processSelectedNodes() {
  413. var dropText = new Array();
  414. var selectedNodesHolder = cf.cfgGet("selectedTreeNodes");
  415. //check that drag originated from tree.
  416. if (selectedNodesHolder) {
  417. var selectedNodes = selectedNodesHolder.allNodes;
  418. //Build the drop text.
  419. for (var i = 0; i < selectedNodes.length; i++)
  420. {
  421. // get the object from the node
  422. var obj = selectedNodes[i].getValue();
  423. var type = selectedNodes[i].getNodeTypeObject();
  424. if (obj) {
  425. //Get the insert value making sure to get the long or the short name correctly.
  426. //Make sure we get the correct drop value. IF we should update agnt Items we need
  427. //short name. otherwise it is a long name.
  428. //dropText.push(droppy.updateAgentItems()?cf.createDataItemObj(obj.name).getDropValue():obj.getDropValue());
  429. //Is the item we have insertable?
  430. if (type.m_oProps.isAgentItemsInsertable() && droppy.updateAgentItems()) {
  431. //Insert into data items tree
  432. obj = cf.addAgentItem(obj).getValue();
  433. }
  434. dropText.push(obj.getDropValue());
  435. //Back to sneakiness.....
  436. parent.getAgentItemsListener().addItemToField(droppy.id, obj);
  437. }
  438. }
  439. }
  440. return dropText;
  441. }
  442. /**
  443. * Processes selected nodes and returns the drop text.
  444. * This functions is used in email_options_ags.xts file which uses the RTE CKEditor
  445. */
  446. function processSelectedNodes_ForCKEditorRTE() {
  447. var dropText = new Array();
  448. var nodesHolder = new parent.SelectedNodesHolder();
  449. cf.cfgSet("selectedTreeNodes",nodesHolder);
  450. var selectedNodesHolder = cf.cfgGet("selectedTreeNodes");
  451. //check that drag originated from tree.
  452. if (selectedNodesHolder) {
  453. var selectedNodes = selectedNodesHolder.allNodes;
  454. //Build the drop text.
  455. for (var i = 0; i < selectedNodes.length; i++)
  456. {
  457. // get the object from the node
  458. var obj = selectedNodes[i].getValue();
  459. var type = selectedNodes[i].getNodeTypeObject();
  460. if (obj) {
  461. //Get the insert value making sure to get the long or the short name correctly.
  462. //Make sure we get the correct drop value. IF we should update agnt Items we need
  463. //short name. otherwise it is a long name.
  464. //dropText.push(droppy.updateAgentItems()?cf.createDataItemObj(obj.name).getDropValue():obj.getDropValue());
  465. //Is the item we have insertable?
  466. if (cf.addAgentItem(obj) != null && cf.addAgentItem(obj).getValue() != null) {
  467. obj = cf.addAgentItem(obj).getValue();
  468. }
  469. if(obj.getDropValue() != null)
  470. {
  471. dropText.push(obj.getDropValue());
  472. }
  473. //Back to sneakiness.....
  474. // parent.getAgentItemsListener().addItemToField(droppy.id, obj);
  475. }
  476. }
  477. }
  478. //We are done remove it.
  479. cf.cfgRemove("selectedTreeNodes");
  480. return dropText;
  481. }
  482. DropZoneHandler.prototype.getElementValue = function(id)
  483. {
  484. var registeredObject = this.registeredObjectsIndex[id];
  485. var srcValue = "";
  486. if (registeredObject) {
  487. srcValue = registeredObject.getElementValue();
  488. }
  489. return srcValue;
  490. }
  491. DropZoneHandler.prototype.setElementValue = function(id, value)
  492. {
  493. var registeredObject = this.registeredObjectsIndex[id];
  494. if (registeredObject) {
  495. registeredObject.setElementValue(value);
  496. }
  497. }
  498. DropZoneHandler.prototype.updateElementValue = function(id, value)
  499. {
  500. var registeredObject = this.registeredObjectsIndex[id];
  501. if (registeredObject) {
  502. registeredObject.updateElementValue(value);
  503. }
  504. }
  505. /**
  506. * This is to check whether we can set this element to focus.
  507. */
  508. function isFocusable(element) {
  509. // This is a hack for Mozilla. focus() fails on DIV tags, so we set a custom attribute in the
  510. // markup to suppress the focus() check. See suppressionDialog.xts for example.
  511. //
  512. if (element.getAttribute('focusable')==null) {
  513. try {
  514. element.focus();
  515. } catch(e) {
  516. return false;
  517. }
  518. }
  519. return true;
  520. }
  521. //check browser version if IE10
  522. function isIE10() {
  523. var my_browser = navigator.appName;
  524. var b_version = navigator.appVersion;
  525. var my_version = b_version.split(";");
  526. var trident_Version = my_version[4];
  527. var version_value = false;
  528. if(my_browser == "Microsoft Internet Explorer" && trident_Version >= " Trident/6.0"){
  529. version_value = true;
  530. }
  531. return version_value;
  532. }
  533. /**
  534. * I recommend that you inherit from this class, if you want to obverride certain
  535. * functionality and maintain others.
  536. * to do inhertiance here is what you would have to do:
  537. *
  538. * function myElementProcessor(some param) {some code to define additional and overriden functions}
  539. * myElementProcessor.prototype = new ElementProcessor();
  540. *
  541. * usage would be:
  542. * var a = new myElementProcessor();
  543. * 'a' would contain all the properties of ElementProcessor as well as any overriden and new ones
  544. * functions/properties defined by myElementProcessort.
  545. */
  546. function ElementProcessor(id,defaultValue,overwrite) {
  547. this.id = id;
  548. this.element = parent.agsFormUtils.getElementByIdOrName(id);
  549. if (this.element) {
  550. this.tagName = this.element.tagName;
  551. } else {
  552. this.tagName = null;
  553. return;
  554. }
  555. this.tagName = this.element.tagName;
  556. this.defaultValue = defaultValue && defaultValue !='undefined'?defaultValue:'';
  557. this.overwrite = overwrite && overwrite != 'undefined'?overwrite:false;
  558. this.updateAgentItems = false;
  559. this.allowFunctionsAndParams = false;
  560. this.isDefaultMode = function () {return this.defaultValue.localeCompare(this.getElementValue()) == 0;}
  561. this.isEmpty = function () {
  562. if(this.getElementValue())
  563. {
  564. return this.getElementValue().localeCompare("") == 0;
  565. }
  566. else
  567. {
  568. return true;
  569. }
  570. }
  571. this.setDefaultValue = function () {this.setElementValue(this.defaultValue);this.updateStyle(true);}
  572. this.removeDefaultValue = function() {if (this.isDefaultMode()) {this.setElementValue("");this.updateStyle(false);} }
  573. }
  574. ElementProcessor.prototype.setDropLocation = function(event) {
  575. //We were checking for default mode in here.
  576. //However this caused problems for drop zones where the
  577. //the cursor was moving all over the place and where the cursor
  578. //is you are able to insert text.
  579. //So I changed this to always create the textrange and set the focus.
  580. if (cf.browserCheck.isIE5Up()) {
  581. // create a textRange - bases on the element
  582. var textRange = this.element.createTextRange();
  583. if (this.element.disabled) {
  584. return;
  585. }
  586. this.element.focus();
  587. try {
  588. //The div being dragged is sometimes slow moving which
  589. //will result in the cursor moving over the div and if
  590. //we are over a drop zone with text, where the cursor
  591. //is beign shifted from character to character you will
  592. //get an error with the pointer location.
  593. //Try catch will; resolve this problem for now. (always has been there)
  594. // set the position of the textRange
  595. if(isIE10()){
  596. textRange.moveToPoint(event.clientX, event.clientY);
  597. }
  598. else{
  599. textRange.moveToPoint(event.x, event.y);
  600. }
  601. //textRange.moveToPoint(event.x, event.y);
  602. } catch(e) {
  603. //do nothing
  604. }
  605. // text input fields do not position the cursor so move the insertion point to the end.
  606. if(this.tagName == "INPUT" ) {
  607. textRange.moveEnd("textedit");
  608. textRange.collapse(false);
  609. }
  610. textRange.select();
  611. }
  612. }
  613. /**
  614. * Drop text is an array of text.
  615. */
  616. ElementProcessor.prototype.process = function(dropText) {
  617. if (dropText) {
  618. if (this.overwrite) {
  619. if (dropText.length == 1) {
  620. this.setElementValue("");
  621. } else {
  622. alert(parent.singleItemAllowed_string);
  623. return;
  624. }
  625. }
  626. this.defaultTextProcess(dropText.join(""));
  627. parent.getAgentItemsListener().updateField(this.id, this.getElementValue(this.id));
  628. } else {
  629. return;
  630. }
  631. }
  632. ElementProcessor.prototype.updateStyle = function(useDefault) {
  633. // check to see if we are dropping into an area with default text
  634. if (this.tagName.localeCompare("TEXTAREA") == 0) {
  635. // check to see if we have got hint text
  636. if (this.defaultValue.length > 0 && useDefault) {
  637. this.element.className="hintText_active";
  638. } else {
  639. this.element.className="hintText_inactive";
  640. }
  641. }
  642. }
  643. ElementProcessor.prototype.getElementValue = function() {
  644. var prop = this.getElementProperty();
  645. var value = null;
  646. if (prop) {
  647. if (this.element) {
  648. value = this.element[prop];
  649. }
  650. }
  651. return value;
  652. }
  653. ElementProcessor.prototype.setElementValue = function(value) {
  654. var isDefault = false;
  655. //alert("setting element value");
  656. if (!value && value != "") {
  657. isDefault = true;
  658. value = this.defaultValue;
  659. }else if ("" == value) {
  660. isDefault = true;
  661. }else{
  662. isDefault = this.defaultValue.localeCompare(value) == 0;
  663. }
  664. this.updateStyle(isDefault);
  665. var prop = this.getElementProperty();
  666. this.element[prop] = value;
  667. }
  668. ElementProcessor.prototype.updateElementValue = function(value) {
  669. if (!value || value == 'undefined') {
  670. return;
  671. }
  672. if (this.isDefaultMode()) {
  673. this.removeDefaultValue();
  674. }
  675. var prop = this.getElementProperty();
  676. //Give the element we are updating focus.
  677. //it is a bit strange that we update value
  678. //without giving focus.
  679. if (isFocusable(this.element)) {
  680. this.element.focus();
  681. }
  682. if (this.overwrite) {
  683. this.element[prop] = value;
  684. } else {
  685. this.element[prop] += value;
  686. }
  687. }
  688. ElementProcessor.prototype.getElementProperty = function() {
  689. if (this.tagName) {
  690. // check to see if we are dropping into an area with default text
  691. if (this.tagName.localeCompare("TEXTAREA") == 0) {
  692. // need to some more local processing of what we're going to insert
  693. return "value";
  694. } else if (this.tagName.localeCompare("INPUT") == 0) {
  695. // need to do some more local processing of what we're going to insert
  696. return "value";
  697. } else if (this.tagName.localeCompare("TD") == 0) {
  698. return "innerHTML";
  699. } else if (this.tagName.localeCompare("BODY") == 0) {
  700. return "innerHTML";
  701. }
  702. }
  703. }
  704. ElementProcessor.prototype.defaultTextProcess = function(dropText) {
  705. // this is the part that actually inserts the data
  706. if (cf.browserCheck.isIE5Up()) {
  707. // Get selection & text range from selection.
  708. var sel = document.selection;
  709. var textRange = sel.createRange();
  710. this.element.focus();
  711. //test if the selection can be used for the drop
  712. if(!this.useSelection(this.element,textRange.parentElement())) {
  713. textRange = this.element.createTextRange();
  714. textRange.moveEnd("textedit");
  715. textRange.collapse(false);
  716. textRange.select();
  717. }
  718. textRange.text = dropText;
  719. //collapse the range to the right after dropping
  720. textRange.collapse(false);
  721. } else if (this.element.selectionStart || this.element.selectionStart == '0') {
  722. // MOZ
  723. //
  724. var startPos = this.element.selectionStart;
  725. var endPos = this.element.selectionEnd;
  726. this.element.focus();
  727. this.element.value = this.element.value.substring(0, startPos) + dropText + this.element.value.substr(endPos);
  728. } else {
  729. this.element.focus();
  730. this.updateElementValue(dropText);
  731. }
  732. }
  733. /*
  734. * Check if the selection exists as a child element inside the drop control. This is needed to be done
  735. * because it is possible to select arbitrary areas of non-input HTML on a web page. Also, some drop controls
  736. * have HTML inside them such as the email body, which can contain such things as HTML tables.
  737. *
  738. * this.element The drop control
  739. * selElement The current document selection
  740. * return true if this selElement is a child of the drop control
  741. */
  742. ElementProcessor.prototype.useSelection = function (our_element,selElement)
  743. {
  744. //set the found bool
  745. var found = our_element == selElement;
  746. //check for child nodes in the drop control
  747. if (our_element.hasChildNodes())
  748. {
  749. //get the children
  750. var children = our_element.childNodes;
  751. //loop through the children
  752. for (var i=0;(i<children.length && !found);i++)
  753. {
  754. var child = children[i];
  755. found = child == our_element;
  756. //recurse lower
  757. if (!found) {
  758. found = this.useSelection(child,selElement);
  759. }
  760. }
  761. }
  762. return found;
  763. }
  764. var droppy = new DropZoneHandler();
  765. // register something with the dropZonehandler