/** * Add buttons that randomly update query filters but preserve the styles and names of the queries. * * This example depends on prior existaece of 3 analyses present in the workspace - q1, q2 and q3 * that are performed on "{http://www.intergraph.com/geomedia/gml}OM_USA_STATES" feature class * from "http://demo.hexagongeospatial.com/GWM_WFS_NonTransactional/service.svc/get" WFS service. * IDs of these queries must be q1, q2 and q3. * * 2017-03-29 Piotr Szrajber <piotr.szrajber@hexagongeospatial.com> */$GP.ready(function(){varmapServiceUrl="http://demo.hexagongeospatial.com/GWM_WFS_NonTransactional/service.svc/get",featureClassId="{http://www.intergraph.com/geomedia/gml}OM_USA_STATES",attributeId="{http://www.intergraph.com/geomedia/gml}STATE_NAME",timeout=60000,interval=300,attemptsLeft=timeout/interval;functionpickRandomSubset(array,size){varsubset=[],copy=array,index;while(subset.length!==size){index=Math.floor(Math.random()*copy.length);subset.push(copy[index]);copy=copy.slice(0,index).concat(copy.slice(index+1));}returnsubset;}functioncreateRandomFilter(stateNames){varfilterSize=3,randomSubset=pickRandomSubset(stateNames,filterSize);return{operator:"OR",operands:randomSubset.map(function(stateName){return{operator:"=",operands:["{http://www.intergraph.com/geomedia/gml}STATE_NAME",stateName]};})};}functionaddUpdateQueryButtons(result){$GP.edit.getAttributeValues({mapServiceId:result.ids[0],featureClassId:featureClassId,attributeId:attributeId},function(result2){varstateNames=result2.values.map(function(x){returnx.value;});$GP.queries.find({name:/q\d/},function(ret){ret.analyses.forEach(function(analysis){$GP.ui.toolbar.add({xtype:"tbbutton",text:"update "+analysis.get_name(),handler:function(b){$GP.queries.add({featureClassId:featureClassId,url:mapServiceUrl,definitionName:"WFS",queryId:analysis.get_id(),filters:[createRandomFilter(stateNames)],});}});});});});}functionwaitForMapService(){$GP.services.find({url:mapServiceUrl},addUpdateQueryButtons,function(){if(attemptsLeft--==0){$GP.ui.info("Couldn't find the map service. Is it initialized?");}else{setTimeout(waitForMapService,interval);}});}waitForMapService();});
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.