Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to add custom field formatters #618

Merged
merged 6 commits into from
Oct 10, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions viewer/js/config/identify.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
define([
'dojo/i18n!./nls/main'
], function (i18n) {
'dojo/i18n!./nls/main',
'dojo/_base/lang'
], function (i18n, lang) {

var linkTemplate = '<a href="{url}" target="_blank">{text}</a>';
function directionsFormatter (noValue, attributes) {
return lang.replace(linkTemplate, {
url: 'https://www.google.com/maps/dir/' + attributes.Address + ' Louisville, KY',
text: 'Get Directions'
});
}
return {
map: true,
mapClickMode: true,
Expand Down Expand Up @@ -30,6 +38,13 @@ define([
2: {
title: i18n.identify.louisvillePubSafety.policeStation,
fieldInfos: [{
// example of adding a 'calculated' or formatted field
// click on a louisville kentucky police station to see
// the result
fieldName: 'Directions',
visible: true,
formatter: directionsFormatter
}, {
fieldName: 'Name',
visible: true
}, {
Expand Down Expand Up @@ -62,4 +77,4 @@ define([
}
}
};
});
});
35 changes: 30 additions & 5 deletions viewer/js/gis/dijit/Identify.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ define([
'esri/TimeExtent',
'dojo/text!./Identify/templates/Identify.html',
'dojo/i18n!./Identify/nls/resource',
'./Identify/Formatters',

'dijit/form/Form',
'dijit/form/FilteringSelect',
'xstyle/css!./Identify/css/Identify.css'
], function (declare, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, MenuItem, lang, array, all, topic, query, domStyle, domClass, Moveable, Memory, IdentifyTask, IdentifyParameters, PopupTemplate, FeatureLayer, TimeExtent, IdentifyTemplate, i18n) {
], function (declare, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, MenuItem, lang, array, all, topic, query, domStyle, domClass, Moveable, Memory, IdentifyTask, IdentifyParameters, PopupTemplate, FeatureLayer, TimeExtent, IdentifyTemplate, i18n, Formatters) {

return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], {
widgetsInTemplate: true,
Expand All @@ -47,6 +48,19 @@ define([
'shape_len', 'shape.stlength()',
'shape.area', 'shape_area', 'shape.starea()'
],
/**
* field type mappings to their default formatter functions
* overriding this object will globally replace the default
* formatter function for the field type
* @type {Object<Function>}
*/
defaultFormatters: {
'esriFieldTypeSmallInteger': Formatters.formatInt,
'esriFieldTypeInteger': Formatters.formatInt,
'esriFieldTypeSingle': Formatters.formatFloat,
'esriFieldTypeDouble': Formatters.formatFloat,
'esriFieldTypeDate': Formatters.formatDate
},

postCreate: function () {
this.inherited(arguments);
Expand Down Expand Up @@ -313,11 +327,21 @@ define([
return;
}
}
fSet.push(result.feature);
var feature = this.getFormattedFeature(result.feature);
// console.log(feature);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obviously it won't affect anything but it would be good to remove this commented line.

fSet.push(feature);
}, this);
}, this);
this.map.infoWindow.setFeatures(fSet);
},
getFormattedFeature: function (feature) {
array.forEach(feature.infoTemplate.info.fieldInfos, function (info) {
if (typeof info.formatter === 'function') {
feature.attributes[info.fieldName] = info.formatter(feature.attributes[info.fieldName], feature.attributes);
}
});
return feature;
},
identifyError: function (err) {
this.map.infoWindow.hide();
topic.publish('viewer/handleError', {
Expand Down Expand Up @@ -409,19 +433,20 @@ define([
this.addDefaultFieldInfo(fieldInfos, {
fieldName: foundField[0].name,
label: foundField[0].alias,
visible: true
visible: true,
formatter: typeof this.defaultFormatters[foundField[0].type] !== 'undefined' ? this.defaultFormatters[foundField[0].type] : undefined
});
}
}));

// from the fields layer
} else if (layer.fields) {

array.forEach(layer.fields, lang.hitch(this, function (field) {
this.addDefaultFieldInfo(fieldInfos, {
fieldName: field.name,
label: field.alias,
visible: true
visible: true,
formatter: typeof this.defaultFormatters[field.type] !== 'undefined' ? this.defaultFormatters[field.type] : undefined
});
}));
}
Expand Down
21 changes: 21 additions & 0 deletions viewer/js/gis/dijit/Identify/Formatters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
define([
'dojo/number',
'dojo/date/locale'
], function (number, locale) {
return {
formatInt: function (value) {
return number.format(value);
},
formatFloat: function (value) {
return number.format(value, {
places: 3
});
},
formatDate: function (value) {
var date = new Date(value);
return locale.format(date, {
formatLength: 'short'
});
}
};
});