Skip to content

Commit

Permalink
Merge pull request #5199 from dimagi/add-fixtures-to-graph-config
Browse files Browse the repository at this point in the history
Add fixtures to available data sources in graph configuration
  • Loading branch information
Nick Pellegrino committed Jan 5, 2015
2 parents bd483d8 + b887965 commit e38075b
Show file tree
Hide file tree
Showing 8 changed files with 773 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ var DetailScreenConfig = (function () {

this.graph_extra = new uiElement.GraphConfiguration({
childCaseTypes: this.screen.childCaseTypes,
fixtures: this.screen.fixtures,
lang: this.lang,
langs: this.screen.langs,
name: this.header.val()
Expand Down Expand Up @@ -562,6 +563,7 @@ var DetailScreenConfig = (function () {
this.langs = options.langs || [];
this.properties = options.properties;
this.childCaseTypes = options.childCaseTypes;
this.fixtures = options.fixtures;
// The column key is used to retreive the columns from the spec and
// as the name of the key in the data object that is sent to the
// server on save.
Expand Down Expand Up @@ -818,6 +820,7 @@ var DetailScreenConfig = (function () {
saveUrl: that.saveUrl,
columnKey: columnType,
childCaseTypes: spec.childCaseTypes,
fixtures: spec.fixtures,
containsSortConfiguration: columnType == "short",
containsParentConfiguration: columnType == "short",
containsFilterConfiguration: columnType == "short",
Expand Down
50 changes: 34 additions & 16 deletions corehq/apps/app_manager/static/app_manager/js/graph-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* lang
* langs
* childCaseTypes
* fixtures
* @param serverRepresentationOfGraph
* Object corresponding to a graph configuration saved in couch.
* @constructor
Expand Down Expand Up @@ -128,8 +129,8 @@ uiElement.GraphConfiguration = function(moduleOptions, serverRepresentationOfGra
* An object in the form returned of that returned by self.val(). That is,
* in the same form as the the graph configuration saved in couch.
* @param moduleOptions
* Additional options that are derived from the module like lang, langs,
* and childCaseTypes.
* Additional options that are derived from the module (or app) like lang,
* langs, childCaseTypes, and fixtures.
* @returns {{}}
*/
function getGraphViewModelJS(serverGraphObject, moduleOptions){
Expand Down Expand Up @@ -181,6 +182,7 @@ uiElement.GraphConfiguration = function(moduleOptions, serverRepresentationOfGra
};
});
ret.childCaseTypes = moduleOptions.childCaseTypes;
ret.fixtures = moduleOptions.fixtures;

return ret;
}
Expand Down Expand Up @@ -332,19 +334,20 @@ var GraphViewModel = function(moduleOptions){
'zoom': 'true or false'
};
self.childCaseTypes = moduleOptions.childCaseTypes || [];
self.fixtures = moduleOptions.fixtures || [];

self.selectedGraphType.subscribe(function(newValue) {
// Recreate the series objects to be of the correct type.
self.series(_.map(self.series(), function(series){
return new (self.getSeriesConstructor())(ko.toJS(series), self.childCaseTypes);
return new (self.getSeriesConstructor())(ko.toJS(series), self.childCaseTypes, self.fixtures);
}));
});

self.populate = function(obj){
self.graphDisplayName(obj.graphDisplayName);
self.selectedGraphType(obj.selectedGraphType);
self.series(_.map(obj.series, function(o){
return new (self.getSeriesConstructor())(o, self.childCaseTypes);
return new (self.getSeriesConstructor())(o, self.childCaseTypes, self.fixtures);
}));
self.annotations(_.map(obj.annotations, function(o){
return new Annotation(o);
Expand All @@ -367,13 +370,14 @@ var GraphViewModel = function(moduleOptions){
}));

self.childCaseTypes = obj.childCaseTypes.slice(0);
self.fixtures = obj.fixtures.slice(0);
};

self.removeSeries = function (series){
self.series.remove(series);
};
self.addSeries = function (series){
self.series.push(new (self.getSeriesConstructor())({}, self.childCaseTypes));
self.series.push(new (self.getSeriesConstructor())({}, self.childCaseTypes, self.fixtures));
};
/**
* Return the proper Series object constructor based on the current state
Expand Down Expand Up @@ -412,26 +416,34 @@ var Annotation = function(original){
Annotation.prototype = new LocalizableValue();
Annotation.prototype.constructor = Annotation;

var GraphSeries = function (original, childCaseTypes){
var GraphSeries = function (original, childCaseTypes, fixtures){
PairConfiguration.apply(this, [original]);
var self = this;
original = original || {};
childCaseTypes = childCaseTypes || [];
fixtures = fixtures || [];

function origOrDefault(prop, fallback){
return original[prop] === undefined ? fallback : original[prop];
}

self.getFixtureInstanceId = function(fixtureName){
return "item-list:" + fixtureName;
};

/**
* Return the default value for the data path field based on the given source.
* This is used to change the data path field when a new source type is selected.
* @param source
* @returns {string}
*/
self.getDefaultDataPath = function(source){
if (source == "custom"){
if (source.type == "custom"){
return "instance('name')/root/path-to-point/point";
} else {
return "instance('casedb')/casedb/case[@case_type='"+source+"'][index/parent=current()/@case_id][@status='open']";
} else if (source.type == 'case') {
return "instance('casedb')/casedb/case[@case_type='"+source.name+"'][index/parent=current()/@case_id][@status='open']";
} else if (source.type == 'fixture') {
return "instance('" + self.getFixtureInstanceId(source.name) + "')/tablename_list/" + source.name;
}
};

Expand All @@ -440,10 +452,16 @@ var GraphSeries = function (original, childCaseTypes){
_.map(childCaseTypes, function(s){
return {
'text': "Child case: " + s,
'value' : s
'value': {'type': 'case', 'name': s}
};
}).concat([{'text':'custom', 'value':'custom'}])
}).concat(_.map(fixtures, function(s){
return {
'text': "Lookup table: " + s,
'value': {type: 'fixture', name: s}
};
})).concat([{'text':'custom', 'value': 'custom'}])
));

self.selectedSource = ko.observable(origOrDefault('selectedSource', self.sourceOptions()[0]));
// Fix selectedSource reference:
// (selectedSource has to be a reference to an object in sourceOptions.
Expand All @@ -452,7 +470,7 @@ var GraphSeries = function (original, childCaseTypes){
if (!_.contains(self.sourceOptions(), self.selectedSource())){
var curSource = self.selectedSource();
var source = _.find(self.sourceOptions(), function(opt){
return opt.text == curSource.text && opt.value == curSource.value;
return _.isEqual(opt, curSource);
});
self.selectedSource(source);
}
Expand Down Expand Up @@ -484,8 +502,8 @@ var GraphSeries = function (original, childCaseTypes){
GraphSeries.prototype = new PairConfiguration();
GraphSeries.prototype.constructor = GraphSeries;

var XYGraphSeries = function(original, childCaseTypes){
GraphSeries.apply(this, [original, childCaseTypes]);
var XYGraphSeries = function(original, childCaseTypes, fixtures){
GraphSeries.apply(this, [original, childCaseTypes, fixtures]);
var self = this;
self.configPropertyOptions = self.configPropertyOptions.concat(['point-style', 'secondary-y']);
self.configPropertyHints['point-style'] = "'none', 'circle', 'x', 'diamond', ..."; //triangle and square are also options
Expand All @@ -495,8 +513,8 @@ var XYGraphSeries = function(original, childCaseTypes){
XYGraphSeries.prototype = new GraphSeries();
XYGraphSeries.constructor = XYGraphSeries;

var BubbleGraphSeries = function(original, childCaseTypes){
GraphSeries.apply(this, [original, childCaseTypes]);
var BubbleGraphSeries = function(original, childCaseTypes, fixtures){
GraphSeries.apply(this, [original, childCaseTypes, fixtures]);
var self = this;

self.radiusFunction = ko.observable(original.radiusFunction === undefined ? "" : original.radiusFunction);
Expand Down
8 changes: 7 additions & 1 deletion corehq/apps/app_manager/suite_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,13 @@ def get_all_xpaths(self):
result.add(field.header.text.xpath_function)
result.add(field.template.text.xpath_function)
except AttributeError:
pass # Its a Graph detail
# Its a Graph detail
# convert Template to GraphTemplate
s = etree.tostring(field.template.node)
template = load_xmlobject_from_string(s, xmlclass=GraphTemplate)
for series in template.graph.series:
result.add(series.nodeset)

result.discard(None)
return result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
var edit = {{ edit|JSON }};
var saveUrl = "{% url "edit_module_detail_screens" domain app.id module.id %}";
var parentModules = {{ parent_modules|JSON }};
var fixtures = {{ fixtures|JSON }};

for (var i = 0; i < details.length; i++) {
var detail = details[i];
Expand All @@ -64,6 +65,7 @@
saveUrl: saveUrl,
parentModules: parentModules,
childCaseTypes: detail.child_case_types,
fixtures: fixtures,
parentSelect: detail.parent_select,
contextVariables: COMMCAREHQ.app_manager.module_view
});
Expand Down

0 comments on commit e38075b

Please sign in to comment.