Skip to content

Instantly share code, notes, and snippets.

@odoe
Last active December 18, 2015 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odoe/5800348 to your computer and use it in GitHub Desktop.
Save odoe/5800348 to your computer and use it in GitHub Desktop.
ArcGIS InfoTemplate builder helper. Works for dynamic content as well.
/**
* @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);
@raji-knr
Copy link

raji-knr commented Dec 2, 2013

I am new to Javascript. Could you show a working sample for this with Identify results

@DavidSpriggs
Copy link

@odoe Can you include the css file for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment