// Query class function query(inDivID, inDialogDivID, inSortDivID) { this.addCondition = function () { // How many conditions? if (this.conditions.length >= 5) { alert("Queries have a maximum of 5 conditions."); return false; } // Add a div to the container var newDiv = createElement('div', null, 'query_line'); this.div.appendChild(newDiv); // Add a new condition to array var newCondition = new condition(newDiv, this.conditions.length); this.conditions.push(newCondition); } this.removeCondition = function(inIndex) { // Check if this is the last one if (this.conditions.length == 1) { alert('You cannot remove the last condition.'); return false; } // Remove ths div this.div.removeChild(this.conditions[inIndex].div); // Move from current condition to end of array adjusting pointers for (var i = inIndex; i < this.conditions.length - 1; i++) { this.conditions[i] = this.conditions[i + 1]; this.conditions[i].index = i; this.conditions[i].setHTML(); } // Pop the last element this.conditions.pop(); } this.showDialog = function(inType, inPosX, inPosY, inParam1, e) { var buildHTML = ''; // Prevent the global click catcher from closing the menu if (window.event) event.cancelBubble=true; else if (e.stopPropagation) e.stopPropagation(); // Build HTML if (inType.toLowerCase() == 'chooseproperty') { // Print all prop types for (var pgIndex = 0; pgIndex < mySchema.propertyGroups.length; pgIndex++) { var curPG = mySchema.propertyGroups[pgIndex]; for (var pIndex = 0; pIndex < curPG.properties.length; pIndex++) { var curP = curPG.properties[pIndex]; if (curP.searchCondition == 1) { buildHTML += '
'; } } } // Size the dialog this.div_dialog.style.width = "300px"; } else if (inType.toLowerCase() == 'choosemust') { // Print options buildHTML += ''; buildHTML += ''; // Size the dialog this.div_dialog.style.width = "100px"; } else if (inType.toLowerCase() == 'choosecondition_enterstring' || inType.toLowerCase() == 'choosecondition_entermlstring' || inType.toLowerCase() == 'choosecondition_enterurl' || inType.toLowerCase() == 'choosecondition_enterusername') { // Print options buildHTML += ''; buildHTML += ''; buildHTML += ''; buildHTML += ''; buildHTML += ''; // Size the dialog this.div_dialog.style.width = "150px"; } else if (inType.toLowerCase() == 'choosecondition_enterinteger' || inType.toLowerCase() == 'choosecondition_enterdecimal') { // Print options buildHTML += ''; buildHTML += ''; buildHTML += ''; buildHTML += ''; // Size the dialog this.div_dialog.style.width = "150px"; } else if (inType.toLowerCase() == 'choosecondition_enterdate') { // Print options buildHTML += ''; buildHTML += ''; buildHTML += ''; buildHTML += ''; // Size the dialog this.div_dialog.style.width = "150px"; } else if (inType.toLowerCase() == 'choosecondition_choosestring') { // Print options buildHTML += ''; buildHTML += ''; // Size the dialog this.div_dialog.style.width = "150px"; } else if (inType.toLowerCase() == 'choosecondition_choosestrings') { // Print options buildHTML += ''; buildHTML += ''; // Size the dialog this.div_dialog.style.width = "150px"; } else if (inType.toLowerCase() == 'choosepropertychoice') { var prop = mySchema.getPropertyGroupByID(myQuery.conditions[inParam1].pgID).getPropertyByID(myQuery.conditions[inParam1].pID); // Print options for (var i = 0; i < prop.choices.length; i++) buildHTML += ''; // Size the dialog this.div_dialog.style.width = "200px"; } else if (inType.toLowerCase() == 'choosesortproperty') { // Print all prop types for (var pgIndex = 0; pgIndex < mySchema.propertyGroups.length; pgIndex++) { var curPG = mySchema.propertyGroups[pgIndex]; for (var pIndex = 0; pIndex < curPG.properties.length; pIndex++) { var curP = curPG.properties[pIndex]; if (curP.searchSort == 1) { buildHTML += ''; } } } // Size the dialog this.div_dialog.style.width = "300px"; } else if (inType.toLowerCase() == 'choosesortdirection') { // Up or down buildHTML += ''; buildHTML += ''; // Size the dialog this.div_dialog.style.width = "150px"; } // Show dialog with desired HTML this.div_dialog.innerHTML = buildHTML; this.div_dialog.style.left = inPosX + 'px'; this.div_dialog.style.top = inPosY + 'px'; this.div_dialog.style.display = 'block'; } this.hideDialog = function () { this.div_dialog.style.display = "none"; } this.toggleDialog = function(inType, inPosX, inPosY, inParam1, e) { if (this.div_dialog.style.display.toLowerCase() == 'block') this.hideDialog(); else this.showDialog(inType, inPosX, inPosY, inParam1, e); } this.execute = function() { // Cancel if input is invalid if (!this.validate()) return false; // Go to results location = "/search.php?" + this.getQueryString(); } this.validate = function() { // Get each condition to validate itself, stopping if one is invalid for (var i = 0; i < this.conditions.length; i++) if (!this.conditions[i].validate()) return false; // Success return true; } this.getQueryString = function() { var queryString = ''; // Pass sort info if we aren't using default if (this.sortPGID != null) { queryString += 'sortpgid=' + encodeURIComponent(this.sortPGID); queryString += '&sortpid=' + encodeURIComponent(this.sortPID); queryString += '&sortdir=' + encodeURIComponent(this.sortDirection); } for (var i = 0; i < this.conditions.length; i++) queryString += this.conditions[i].getQueryString(); return queryString; } this.setSortProperty = function(inPGID, inPID) { this.sortPGID = inPGID; this.sortPID = inPID; this.setSortHTML(); return true; } this.setSortDirection = function(inDirection) { this.sortDirection = inDirection; this.setSortHTML(); return true; } this.setSortHTML = function() { // Hotness is a special case (is it really?) var buildHTML = ''; if (this.sortPGID == null) { buildHTML = 'Sort by hotness'; buildHTML += ' from high to low'; } else { var pg = mySchema.getPropertyGroupByID(this.sortPGID); var p = pg.getPropertyByID(this.sortPID); buildHTML = 'Sort by ' + p.name.toLowerCase() + ''; buildHTML += ' from ' + this.sortDirection + ''; } this.div_sort.innerHTML = buildHTML; return true; } // Constructor // Instance vars this.conditions = []; // Instance divs this.div = document.getElementById(inDivID); this.div_dialog = document.getElementById(inDialogDivID); this.div_sort = document.getElementById(inSortDivID); // Add one condition this.addCondition(); // Choose the sort this.sortDirection = "high to low"; this.setSortProperty(null, null); } // CONDITION CLASS function condition(inDiv, inIndex) { this.getHTML = function () { // Get property from schema var prop = mySchema.getPropertyGroupByID(this.pgID).getPropertyByID(this.pID); // Delete button and condition number buildHTML = 'x'; buildHTML += (this.index + 1) + '. '; // Choose property buildHTML += ' ' + firstCap(prop.name) + ''; // Choose must buildHTML += ' '; buildHTML += this.must; buildHTML += ''; // Choose condition buildHTML += ' '; buildHTML += this.condition; buildHTML += ''; // If a parameter is needed add a text box or selection if (this.condition != 'exist') { if (prop.type.toLowerCase() == 'choosestring' || prop.type.toLowerCase() == 'choosestrings') { buildHTML += ' ' + this.value.toLowerCase() + ''; } else if (prop.type.toLowerCase() == 'enterinteger') { buildHTML += ' '; buildHTML += ' integer'; } else if (prop.type.toLowerCase() == 'enterdecimal') { buildHTML += ' '; buildHTML += ' decimal'; } else if (prop.type.toLowerCase() == 'enterdate') { buildHTML += ' '; buildHTML += ' mm/dd/yyyy'; } else { buildHTML += ' '; } } return buildHTML; } this.setHTML = function() { this.div.innerHTML = this.getHTML(); } this.setProperty = function(inPGID, inPID) { // Update instance variable this.pID = inPID; this.pgID = inPGID; // Get the property from the schema var prop = mySchema.getPropertyGroupByID(this.pgID).getPropertyByID(this.pID); // If this is a choice set the value to the first choice if (prop.type == 'choosestring' || prop.type == 'choosestrings') this.value = prop.choices[0]; else this.value = ''; // Reset condition and must this.must = "must"; this.condition = "exist"; // Set html this.setHTML(); // Close dialog return true; } this.setMust = function(inMust) { // Update var and set HTML this.must = inMust; this.setHTML(); // Close dialog return true; } this.setCondition = function(inCondition) { // Update var and set HTML this.condition = inCondition; this.setHTML(); // Close dialog return true; } this.setValue = function(inValue) { // Update var and set HTML this.value = inValue; this.setHTML(); // Close dialog return true; } this.validate = function() { // Get the prop from schema var prop = mySchema.getPropertyGroupByID(this.pgID).getPropertyByID(this.pID); // Return false if invalid if (prop.type.toLowerCase() == 'enterinteger') { if (this.condition != 'exist' && document.getElementById(this.value_input_id).value.search(/^-?[1234567890]+$/m) == -1) { alert('Please enter a valid integer for condition #' + (this.index+1) ); return false; } } else if (prop.type.toLowerCase() == 'enterdecimal') { if (this.condition != 'exist' && document.getElementById(this.value_input_id).value.search(/^-?[1234567890]+(\.[1234567890]+)?$/m) == -1) { alert('Please enter a valid decimal for condition #' + (this.index+1) ); return false; } } else if (prop.type.toLowerCase() == 'enterdate') { if (this.condition != 'exist' && document.getElementById(this.value_input_id).value.search(/^[1234567890]{1,2}\/[1234567890]{1,2}\/[1234567890]{4}$/m) == -1) { alert('Please enter a valid date in mm/dd/yy form for condition #' + (this.index+1) ); return false; } } // Valid return true; } this.getQueryString = function() { // Get the prop from schema var prop = mySchema.getPropertyGroupByID(this.pgID).getPropertyByID(this.pID); var queryString = ''; queryString = '&pgid' + this.index + '=' + encodeURIComponent(this.pgID); queryString += '&pid' + this.index + '=' + encodeURIComponent(this.pID); queryString += '&must' + this.index + '=' + encodeURIComponent(this.must); queryString += '&condition' + this.index + '=' + encodeURIComponent(this.condition); if (this.condition != 'exist') { if (prop.type == 'choosestring' || prop.type == 'choosestrings') queryString += '&value' + this.index + '=' + encodeURIComponent(this.value); else queryString += '&value' + this.index + '=' + encodeURIComponent(document.getElementById(this.value_input_id).value); } return queryString; } // Constructor // Instance vars this.index = inIndex; this.pID = null; this.pgID = null; this.must = 'must'; this.condition = 'exist'; this.value = ''; this.value_input_id = getUniqueId(); // Instance divs this.div = inDiv; // Set property index to the first property (does a setHTML) var PGID_Fundamentals = 13; var PID_Title = 52; this.setProperty(PGID_Fundamentals, PID_Title); } function firstCap(inText) { return inText.substring(0,1).toUpperCase() + inText.substring(1).toLowerCase(); }