Last active
December 18, 2015 14:49
ArcGIS InfoTemplate builder helper. Works for dynamic content as well.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author rrubalcava@odoe.net (Rene Rubalcava) | |
*/ | |
/*global window document console define require esri */ | |
(function() { | |
'use strict'; | |
define([ | |
'dojo/_base/lang', | |
'dojox/lang/functional', | |
'esri/InfoTemplate' | |
], function(lang, func, InfoTemplate) { | |
function isValidKey(key) { | |
return !(key.indexOf('shape') > -1 || key === 'layername'|| key === 'objectid' || key === 'fid'); | |
} | |
function fieldValue(f, attr) { | |
if (!!attr) { | |
return attr[f]; | |
} else { | |
return ['${', f, '}'].join(''); | |
} | |
} | |
function defaultRow(f, attr) { | |
return ['<tr><td class="fieldName">', f, '</td><td>', fieldValue(f, attr), '</td></tr>'].join(''); | |
} | |
function linkRow(f, urlPrefix, attr) { | |
return ['<tr><td class="fieldName">', f, '</td><td><a href="', urlPrefix, fieldValue(f, attr), '">', fieldValue(f, attr), '</a></td></tr>'].join(''); | |
} | |
function parseInfoContent(content, opt, attr) { | |
return function(f) { | |
var fLowerCase = f.toLowerCase(); | |
if (isValidKey(fLowerCase)) { | |
if (fLowerCase !== opt.urlField) { | |
content.push(defaultRow(f, attr)); | |
} else { | |
content.push(linkRow(f, opt.urlPrefix, attr)); | |
} | |
} | |
}; | |
} | |
function contentSeed(feature, opt, useAttributes) { | |
var content = [], | |
attr = null; | |
if (useAttributes) { | |
attr = feature.attributes; | |
} | |
content.push('<table cellspacing="0" class="table table-striped table-condensed attr-info">'); | |
if (feature.layerName) { | |
content.push('<tr><td class="fieldName">SOURCE: </td><td class="fieldName">' + feature.layerName + '</td></tr>'); | |
} | |
// iterate over attributes to populate content | |
func.forEach(func.keys(feature.attributes), parseInfoContent(content, opt, attr)); | |
content.push('</table>'); | |
return content; | |
} | |
function real(obj) { | |
return obj || {}; | |
} | |
function optionsGen(opt) { | |
return lang.mixin(opt, { | |
urlField: 'urlField' in real(opt) ? opt.urlField.toLowerCase() : '', | |
urlPrefix: 'urlPrefix' in real(opt) ? opt.urlPrefix : '' | |
}); | |
} | |
/** | |
* Utility method to build the infotemplate for a | |
* graphic feature displaying all attributes in a table format. | |
* @param {esri.Graphic} feature Graphic to set infoTemplate. | |
* @return {esri.Graphic} Graphic with infoTemplate set. | |
*/ | |
function buildInfoTemplate(feature, options) { | |
//feature.setInfoTemplate( new InfoTemplate('', contentSeed(feature, optionsGen(options), false).join('')) ); | |
//return feature; | |
return new InfoTemplate('', contentSeed(feature, optionsGen(options), false).join('')); | |
} | |
/** | |
* Utility method to dynamically build the infotemplate content for a | |
* feature displaying all attributes in a table format. | |
* @param {Object} options to be used when building infoTemplate. | |
* @return {String} String of content for infoTemplate. | |
*/ | |
function dynamicInfoTemplate(options) { | |
var opt = optionsGen(options); // cache options since I can | |
return function(feature) { | |
return contentSeed(feature, opt, true).join(' '); | |
}; | |
} | |
function isGraphic(feature) { | |
return feature && feature.hasOwnProperty('geometry'); | |
} | |
function head(t) { | |
return t[0]; | |
} | |
function sec(t) { | |
return t[1]; | |
} | |
return function buildTemplate() { | |
return isGraphic(head(arguments)) ? buildInfoTemplate(head(arguments), sec(arguments)) : dynamicInfoTemplate(head(arguments)); | |
}; | |
}); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am new to Javascript. Could you show a working sample for this with Identify results