/* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| BI and PM: prmt *| (C) Copyright IBM Corp. 2002, 2011 *| *| US Government Users Restricted Rights - Use, duplication or *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp. *| *+------------------------------------------------------------------------+ */ /* CSearch.js This script is used to provide interactivity for the selectWithSearch prompt component. */ //Constructor to create a selectWithSearch component // oSubmit: the control used to submit values to the server // oLstResults: the results list box // oLstChoices: the choices list box // oForm: the form // oSearchBox: the type in search box // oImgSearch: the search icon, used for animation // sRef: the name of this object // oShowOptions: the object that will show/hide the options // oMatchAnywhere: starts with / contains // oMatchAll: and / or // bRequired: boolean, specify if user input is required // sSubmitType: 'default' will submit as a standard form // 'XML' will convert the submission to XML and submit // bMultiple: boolean, specify whether multiple values are allowed or not // oSubmitFlag: a hidden variable used to tell the server whether this control has submitted a search // oSelectOptions: a hidden variable used to resupply the search results to the server // oResultsSizer: object used to resize the results list box // oChoicesSizer: object used to resize the choices list box // oSearchButton: the search button // oInsertButton: the insert button // oRemoveButton: the remove button // sSearchFuntion: // sParameterName: name of the parameter to send in the _searchParameter. function CSearch(oSubmit, oLstResults, oLstChoices, oForm, oSearchBox, oImgSearch, sRef, oShowOptions, oMatchAnywhere, oMatchAll, bRequired, sSubmitType, oErrorFeedback, bMultiple, oSubmitFlag, oSelectOptions, oResultsSizer, oChoicesSizer, oSearchButton, oInsertButton, oRemoveButton, sSearchFunction, sParameterName, sCVId) { this.setCVId(sCVId); this.m_oSubmit = oSubmit; this.m_oForm = oForm; this.m_oSearchBox = oSearchBox; this.m_oLstResults = oLstResults; this.m_oLstChoices = oLstChoices; this.m_oSelectOptions = oSelectOptions; this.m_bRequired = bRequired; this.m_bValid = false; this.m_sSubmitType = sSubmitType; this.m_oErrorFeedback = oErrorFeedback; this.m_sErrorFeedbackClass = "clsFeedbackWidgetParseErrorArrowLeft"; this.m_sRef = sRef; // the promptId is the ref without CV's ID. this.m_sPromptId = this.m_sRef.replace(new RegExp(sCVId + "$"), K_PRMT_sEMPTY); this.m_oShowOptions = oShowOptions; this.m_oMatchAnywhere = oMatchAnywhere; this.m_oMatchAll = oMatchAll; this.m_sSearchFunction = sSearchFunction; this.m_sParameterName = sParameterName; if (oSearchButton) { this.m_oSearchButton = oSearchButton; } if (oInsertButton) { this.m_oInsertButton = oInsertButton; } if (oRemoveButton) { this.m_oRemoveButton = oRemoveButton; } if (bMultiple) { this.m_bMultiple = bMultiple; } else { this.m_bMultiple = false; } //skin folder this.m_sSkin = (typeof getPromptSkin != K_PRMT_sUNDEFINED ? getPromptSkin() : K_PRMT_sDEFAULTSKIN); this.m_oImgSearch = oImgSearch; this.imgBusyTrue = new Image(); this.imgBusyTrue.src = this.m_sSkin + "/prompting/images/prompt_search_ani.gif"; this.imgBusyFalse = new Image(); this.imgBusyFalse.src = this.m_sSkin + "/prompting/images/prompt_search.gif"; this.m_oImgErrorFalse= new Image(); this.m_oImgErrorFalse.src = this.m_sSkin + "/prompting/images/error_timed_small_off.gif"; this.m_oImgErrorTrue = new Image(); this.m_oImgErrorTrue.src = this.m_sSkin + "/prompting/images/error_timed_small.gif"; this.m_oSubmitFlag = oSubmitFlag; this.checkData(); this.stopAnimateSearchIcon(); this.m_bSubmitChoices = true; //resize the list to content if necessary if (this.m_sRef) { if ((oResultsSizer)) { this.m_oResultsSizer = oResultsSizer; this.m_oResultsTester = new Object(); this.m_oResultsTester.m_bSizeAvailable = false; this.m_oResultsTester.m_iResizeTestCounter = 0; this.m_iIntervalResults = window.setInterval ('search'+ this.m_sRef +'.testResize(search'+ this.m_sRef +'.m_oResultsSizer, search'+ this.m_sRef +'.m_oLstResults, search'+ this.m_sRef +'.m_oResultsTester,search'+ this.m_sRef +'.m_iIntervalResults)',250); } if(oChoicesSizer) { this.m_oChoicesSizer = oChoicesSizer; this.m_oChoicesTester = new Object(); this.m_oChoicesTester.m_bSizeAvailable = false; this.m_oChoicesTester.m_iResizeTestCounter = 0; this.m_iIntervalChoices = window.setInterval ('search'+ this.m_sRef +'.testResize(search'+ this.m_sRef +'.m_oChoicesSizer, search'+ this.m_sRef +'.m_oLstChoices, search'+ this.m_sRef +'.m_oChoicesTester,search'+ this.m_sRef +'.m_iIntervalChoices)',250); } } //check status of characters (for compatibility with IME) this.m_bFirstInput = true; this.m_iCheckStatusInterval = window.setInterval ('search'+ this.m_sRef +'.checkSearchButtonState()', 1000); //check the state of the search button this.checkSearchButtonState(); var sDefaultWidth = "200px"; if (typeof SYSTEMPROPERTY_CSEARCH_AUTO_RESIZE_RESULT_LIST != K_PRMT_sUNDEFINED && SYSTEMPROPERTY_CSEARCH_AUTO_RESIZE_RESULT_LIST === true) { sDefaultWidth = "auto"; } this.m_oLstResults.style.width = sDefaultWidth; this.m_oLstChoices.style.width = sDefaultWidth; //check the states of the insert and remove buttons this.update(); } CSearch.prototype = new CPromptControl(); //select all items function CSearch_selectAll(col) { for (var i=0; i < col.options.length; i++) { col.options[i].selected = true; } //check the states of the insert and remove buttons this.checkInsertRemove(); } //remove selection from all items function CSearch_deSelectAll(col) { for (var i=0; i < col.options.length; i++) { col.options[i].selected = false; } //check the states of the insert and remove buttons this.checkInsertRemove(); } //perform any special processing for the server. //this function will wrap all list items in XML. //This is required for cases where the unselected items need //to be submitted too. function CSearch_preProcess() { //prepare form for submission var i=0; if (this.m_sSubmitType == K_PRMT_sXML) { var sURLValues = K_PRMT_sEMPTY; if ((this.m_oLstChoices.options.length > 0) && (this.m_bSubmitChoices == true)) { if (this.m_bMultiple == true) { for(i = 0; i // // // function CSearch_processResults() { var sURLValues = ""; if (this.m_oLstResults.options.length > 0) { for(var i = 0; i K_PRMT_sEMPTY) { for (var i=0; i < fromCol.options.length; i++) { if ( fromCol.options[i].selected == true && !this.contains( toCol.options, fromCol.options[i] ) ) { toCol.options[toCol.options.length] = new Option(fromCol.options[i].text, fromCol.options[i].value, false, false); } } } this.update(); return false; } function CSearch_contains( aOptions, oOption ) { if ( aOptions && oOption ) { var sText = oOption.text; var sValue = oOption.value; for (var i=0; i < aOptions.length; i++) { if ( aOptions[i].text == sText && aOptions[i].value == sValue ) { return true; } } } return false; } //remove selections from the list of choices function CSearch_remove(col) { if (col.selectedIndex != -1) { var i=0; for (i=col.options.length-1; i > -1 ; i--) { if (col.options[i].selected == true) { col.options[i]=null; } } } this.update(); } //show the search in progress animation function CSearch_busyTrue(oImg) { //oImg.src = this.imgBusyTrue.src + '?' + Math.random(); //TODO //the animation is being overidden by the submit //oImg.src = this.imgBusyTrue.src + '?' + Math.random(); } //turn the search progress off function CSearch_busyFalse(oImg) { oImg.src = this.imgBusyFalse.src; } //toggle the busy search progress state on function CSearch_busy() { this.busyTrue(document.images['imgBusy']); } //start the search animation //mark the search control for submission function CSearch_startAnimateSearchIcon() { //tell the server to search after the form is submitted if (this.m_oSubmitFlag) { this.m_oSubmitFlag.value = "true"; } this.m_oImgSearch.src = this.imgBusyTrue.src; } function CSearch_stopAnimateSearchIcon() { this.m_oImgSearch.src = this.imgBusyFalse.src; } //show/hide the advanced search options function CSearch_toggleAdvanced(objId,imgId) { var oIdAdvancedOptions = document.getElementById(objId); var oimgAdvancedArrow = document.getElementById(imgId); //show the dialog if not already visible if (oIdAdvancedOptions.style.display != "none") { oIdAdvancedOptions.style.display = "none"; oimgAdvancedArrow.src = this.m_sSkin + "/prompting/images/prompt_option_expand.gif"; if (this.m_oShowOptions) { this.m_oShowOptions.value="false"; } } else { oIdAdvancedOptions.style.display = "inline"; oimgAdvancedArrow.src = this.m_sSkin + "/prompting/images/prompt_option_collapse.gif"; if (this.m_oShowOptions) { this.m_oShowOptions.value="true"; } } } //change the configuration of the advanced search options function CSearch_setOptions(sMatchAnywhere,sMatchAll) { this.m_oMatchAnywhere.value = sMatchAnywhere; this.m_oMatchAll.value = sMatchAll; } //validate the input into the control function CSearch_checkData() { if (this.m_oLstResults != this.m_oLstChoices) { if ((this.m_bRequired == true) && (this.m_oLstChoices.options.length === 0)) { this.m_bValid = false; this.checkFail(); return false; } else { this.m_bValid = true; this.checkPass(); return true; } } else { if ((this.m_bRequired == true) && ((this.m_oLstChoices.options.length === 0) || (this.m_oLstChoices.selectedIndex == -1))) { this.m_bValid = false; this.checkFail(); return false; } else { this.m_bValid = true; this.checkPass(); return true; } } } //submit the prompt control function CSearch_startSearch() { this.startAnimateSearchIcon(); while (this.m_oLstResults.hasChildNodes()) { this.m_oLstResults.removeChild(this.m_oLstResults.firstChild); } //prevent the single select search from sending //the user's choice to the server when the user //has pressed search if (this.m_bMultiple == false) { this.m_bSubmitChoices = false; } // allow overriding of default search functionality if (this.m_sSearchFunction != K_PRMT_sEMPTY) { eval(this.m_sSearchFunction + "();"); } else { var oCV = this.getCV(); if (oCV && typeof oCV.submitPromptValues == K_PRMT_sFUNCTION && typeof ViewerDispatcherEntry == K_PRMT_sFUNCTION) { var oReq = new ViewerDispatcherEntry(oCV); oReq.addFormField("ui.action", K_ACTION_FORWARD); // regular expression to remove CV's ID from the input names. var reStripCVID = new RegExp("_sws_" + this.m_sRef); // regular expression to match this search control. var reThisSwsInput = new RegExp("sws.*" + this.m_sRef); for (var i in this.m_oForm.elements) { var oInput = this.m_oForm.elements[i]; if (typeof oInput == K_PRMT_sOBJECT && oInput && oInput.name && oInput.value) { if (oInput.name.match(reThisSwsInput)) { var sName = oInput.name; sName = sName.replace(reStripCVID, "_sws_" + this.m_sPromptId); oReq.addFormField(sName, oInput.value); } } } // set parameter name for the _searchParameter variable oReq.addFormField("_searchParameter", this.m_sParameterName); oReq.addFormField("_promptIdBasedNames", "true"); oReq.addFormField("_promptControl", K_ACTION_SEARCH); oCV.submitPromptValues(oReq); } else { SetPromptMethod(K_ACTION_FORWARD); SetPromptControl(K_ACTION_SEARCH); } } } //determine whether to enable or disable //the search button. If the text box is empty //then the search should be disabled. function CSearch_checkSearchButtonState() { if (this.m_oSearchButton) { if (this.m_oSearchBox.value == K_PRMT_sEMPTY) { this.m_oSearchButton.className = "clsPromptButton"; this.disableSearchButton(); } else { this.enableSearchButton(); if (this.m_bFirstInput == true) { this.m_bFirstInput = false; window.clearInterval(this.m_checkStatusInterval); } } } } //enable the search button function CSearch_enableSearchButton() { this.m_oSearchButton.disabled = false; } //disable the search button function CSearch_disableSearchButton() { this.m_oSearchButton.disabled = true; } //this function is used to submit the page if the user //presses the Enter key while in a field function CSearch_catchSearch(evt) { var oEvent = (evt) ? evt : ((event) ? event : null); //catch the Enter key code if (oEvent && oEvent.keyCode == 13 && typeof Search_DisableEnterKeyAutoSubmit != K_PRMT_sUNDEFINED && Search_DisableEnterKeyAutoSubmit !== true) { if (this.m_oSearchButton) { if (this.m_oSearchBox.value != K_PRMT_sEMPTY) { this.startSearch(); } } else { this.startSearch(); } if (typeof oEvent.stopPropagation == K_PRMT_sFUNCTION) { oEvent.stopPropagation(); } if (typeof oEvent.preventDefault == K_PRMT_sFUNCTION) { oEvent.preventDefault(); } oEvent.cancelBubble = true; return false; } } //catch the backspace key //some browsers (IE5.5 don't capture this event) function CSearch_keyPress(sKeyCode) { if (sKeyCode=='8') { //check the data that has been typed in this.checkSearchButtonState(); } return true; } //enable and disable the insert and remove buttons //based on what the user has selected function CSearch_checkInsertRemove() { if (this.m_oInsertButton) { if (this.m_oLstResults.selectedIndex == -1) { this.m_oInsertButton.disabled = true; this.m_oInsertButton.className = "clsInsertRemoveButton"; } else { this.m_oInsertButton.disabled = false; } } if (this.m_oRemoveButton) { if (this.m_oLstChoices.selectedIndex == -1) { this.m_oRemoveButton.disabled = true; this.m_oRemoveButton.className = "clsInsertRemoveButton"; } else { this.m_oRemoveButton.disabled = false; } } } //wait until the size of the list box is known //then trigger a resize -- used in conjunction with an interval function CSearch_testResize(oSizer, oLst, oTester, iInterval) { if (oTester.m_bSizeAvailable == false) { if (oSizer.width != 0 || oTester.m_iResizeTestCounter < 100) { oTester.m_bSizeAvailable = true; } else { oTester.m_iResizeTestCounter++; } } else { window.clearInterval(iInterval); } } //add a value to the list via javascript function CSearch_add(sDisplayValue, sInsertText) { this.m_oLstChoices.options[this.m_oLstChoices.options.length] = new Option(sDisplayValue, sInsertText, false, false); this.checkData(); //check the states of the insert and remove buttons this.checkInsertRemove(); } //add an item to the list without any checking by the control //this method provides an efficient way to add items, but //the update() method should be called to clean up the control //when finished adding function CSearch_addNoUpdate(sText, sValue, type, sel) { sText = sDecodeU003( sText ); sValue = sDecodeU003( sValue ); var iIndex = 0; if (type == "result") { if (sel == true) { iIndex = this.m_oLstResults.options.length; this.m_oLstResults.options[iIndex] = new Option(sText, sValue, true, true); // IE bug : Need to reselect the option, otherwise IE selects the previous one. this.m_oLstResults.options[iIndex].selected = true; } else { this.m_oLstResults.options[this.m_oLstResults.options.length] = new Option(sText, sValue, false, false); } } else { if (sel == true) { iIndex = this.m_oLstChoices.options.length; this.m_oLstChoices.options[iIndex] = new Option(sText, sValue, true, true); // IE bug : Need to reselect the option, otherwise IE selects the previous one. this.m_oLstChoices.options[iIndex].selected = true; } else { this.m_oLstChoices.options[this.m_oLstChoices.options.length] = new Option(sText, sValue, false, false); } } } function CSearch_addOptions(aOptions) { var sDisplayValue, sUseValue, sType, bSel; if (aOptions instanceof Array) { for (var idxOption = 0; idxOption < aOptions.length; idxOption++) { var aOpt = aOptions[idxOption]; if (aOpt && aOpt.length) { sDisplayValue = (aOpt.length > 0 ? aOpt[0] : K_PRMT_sEMPTY); sUseValue = (aOpt.length > 1 ? aOpt[1] : K_PRMT_sEMPTY); bSel = (aOpt.length > 2 ? aOpt[2] : false); sType = (aOpt.length > 3 ? aOpt[3] : null); this.addNoUpdate(sDisplayValue, sUseValue, sType, bSel); } } this.update(); } } //clean up the control function CSearch_update() { this.checkData(); //check the states of the insert and remove buttons this.checkInsertRemove(); CPromptControl_updateSelectWidth( this.m_oLstChoices ); CPromptControl_updateSelectWidth( this.m_oLstResults ); } //Prototypes to assign methods to new instances of the object CSearch.prototype.selectAll = CSearch_selectAll; CSearch.prototype.deSelectAll = CSearch_deSelectAll; CSearch.prototype.preProcess = CSearch_preProcess; CSearch.prototype.remove = CSearch_remove; CSearch.prototype.insert = CSearch_insert; CSearch.prototype.contains = CSearch_contains; CSearch.prototype.busyTrue = CSearch_busyTrue; CSearch.prototype.busyFalse = CSearch_busyFalse; CSearch.prototype.busy = CSearch_busy; CSearch.prototype.toggleAdvanced = CSearch_toggleAdvanced; CSearch.prototype.setOptions = CSearch_setOptions; CSearch.prototype.checkData = CSearch_checkData; CSearch.prototype.startAnimateSearchIcon = CSearch_startAnimateSearchIcon; CSearch.prototype.stopAnimateSearchIcon = CSearch_stopAnimateSearchIcon; CSearch.prototype.startSearch = CSearch_startSearch; CSearch.prototype.processResults = CSearch_processResults; CSearch.prototype.checkSearchButtonState = CSearch_checkSearchButtonState; CSearch.prototype.enableSearchButton = CSearch_enableSearchButton; CSearch.prototype.disableSearchButton = CSearch_disableSearchButton; CSearch.prototype.catchSearch = CSearch_catchSearch; CSearch.prototype.keyPress = CSearch_keyPress; CSearch.prototype.checkInsertRemove = CSearch_checkInsertRemove; CSearch.prototype.testResize = CSearch_testResize; CSearch.prototype.add = CSearch_add; CSearch.prototype.addNoUpdate = CSearch_addNoUpdate; CSearch.prototype.addOptions = CSearch_addOptions; CSearch.prototype.update = CSearch_update;