Skip to content

Commit

Permalink
fully working steps, needs unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fredkingham committed Jan 19, 2017
1 parent 4a4552e commit 48233c3
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 103 deletions.
16 changes: 11 additions & 5 deletions pathway/pathways.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,14 @@ def redirect_url(self, patient):
class Pathway(discoverable.DiscoverableFeature):
module_name = "pathways"
pathway_service = "Pathway"
step_wrapper_template_url = "/templates/pathway/step_wrappers/default.html"
pathway_insert = ".pathwayInsert"


# any iterable will do, this should be overridden
steps = []

# the class that we append the compiled form onto
append_to = ".appendTo"

def __init__(self, patient_id=None, episode_id=None):
self.episode_id = episode_id
Expand Down Expand Up @@ -234,15 +236,18 @@ def to_dict(self):
display_name=self.display_name,
icon=getattr(self, "icon", None),
save_url=self.save_url(),
append_to=self.append_to,
pathway_insert=self.pathway_insert,
template_url=self.template_url,
pathway_service=self.pathway_service
pathway_service=self.pathway_service,
step_wrapper_template_url=self.step_wrapper_template_url
)


class WizardPathway(Pathway, AbstractBase):
pathway_service = "WizardPathway"
template_url = "/templates/pathway/wizard_pathway.html"
step_wrapper_template_url = "/templates/pathway/step_wrappers/wizard.html"
pathway_insert = ".pathwayInsert"


class PagePathway(Pathway, AbstractBase):
Expand All @@ -251,14 +256,15 @@ class PagePathway(Pathway, AbstractBase):
at once, rather than as a set of steps.
"""
template_url = "/templates/pathway/page_pathway.html"
step_wrapper_template_url = "/templates/pathway/step_wrappers/page.html"


class ModalWizardPathway(Pathway, AbstractBase):
pathway_service = "WizardPathway"
template_url = "/templates/pathway/modal_wizard_pathway.html"
append_to = ".modal-content"
pathway_insert = ".modal-content"


class ModalPagePathway(Pathway, AbstractBase):
template_url = "/templates/pathway/modal_page_pathway.html"
append_to = ".modal-content"
pathway_insert = ".modal-content"
11 changes: 4 additions & 7 deletions pathway/static/js/pathway/services/pathway.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ angular.module('opal.services').service('Pathway', function(
this.save_url = pathwayDefinition.save_url;
this.stepDefinitions = pathwayDefinition.steps;
this.template_url = pathwayDefinition.template_url;
this.append_to = pathwayDefinition.append_to;
this.pathway_insert = pathwayDefinition.pathway_insert;
this.display_name = pathwayDefinition.display_name;
this.icon = pathwayDefinition.icon;
this.step_wrapper_template_url = pathwayDefinition.step_wrapper_template_url;
this.episode = episode;
};

Expand All @@ -31,8 +32,8 @@ angular.module('opal.services').service('Pathway', function(
);
pathwayTemplateLoader.load(
self.scope,
self.append_to,
self.stepTemplateWrapper,
self.pathway_insert,
self.step_wrapper_template_url,
self.template_url,
self.steps
);
Expand All @@ -59,10 +60,6 @@ angular.module('opal.services').service('Pathway', function(
},
preSave: function(editing){},
valid: function(editing){ return true },
stepTemplateWrapper: function(loadedHtml, index){
// wraps the loaded template
return loadedHtml
},
finish: function(editing){
var self = this;
editing = angular.copy(editing);
Expand Down
49 changes: 31 additions & 18 deletions pathway/static/js/pathway/services/pathway_template_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,53 @@ angular.module('opal.services').service('pathwayTemplateLoader', function(
return $q.all(templatesToLoad);
};

var loadAllTemplates = function(template_url, steps){
var loadAllTemplates = function(template_url, step_wrapper_template_url, steps){
// returns the pathway template as the first template, then the step templates
return $q.all([getTemplatePromise(template_url), getStepTemplates(steps)]);
return $q.all([getTemplatePromise(template_url), getTemplatePromise(step_wrapper_template_url), getStepTemplates(steps)]);
};

var loadInStep = function(step, index, newScope, append_to, stepTemplateWrapper){
$templateRequest(step.template_url).then(function(loadedHtml){
loadedHtml = stepTemplateWrapper(loadedHtml, index);
var result = $compile(loadedHtml)(step.scope);
$(append_to).find(".to_append").append(result);
var loadInSteps = function(pathwayTemplate, steps, step_template_node, step_wrapper_template_url){
/*
* load in the template wrapper (should already be cached)
* load in steps
* inject the step into the template wrapper
* compile the output of this with the steps scope
* aggregate all the step templates and inject them into the pathway template
*/
getTemplatePromise(step_wrapper_template_url).then(function(stepTemplateWrapper){
getStepTemplates(steps).then(function(stepTemplates){
var allSteps = _.map(stepTemplates, function(stepTemplate, index){
var step = steps[index];
var wrappedTemplate = angular.element(angular.copy(stepTemplateWrapper))
wrappedTemplate.find(step_template_node).replaceWith(stepTemplate);
return $compile(wrappedTemplate)(step.scope);
});
$(pathwayTemplate).find(".to_append").append(allSteps);
});
});
};

var injectSteps = function(loadedHtml, newScope, append_to, stepTemplateWrapper, steps){
var injectSteps = function(loadedHtml, newScope, pathway_insert, step_template_node, step_wrapper_template_url, steps){
/*
* injects the pathway base templates, then the step templates
* injects the pathway base templates into the document, then the step templates
*/
var baseTemplate = loadedHtml[0];
baseTemplate = baseTemplate;
var result = $compile(baseTemplate)(newScope);
$(append_to).append(result);
var parentDocument = $(pathway_insert);

if(!$(append_to).size()){
parentDocument.append(result);

if(!$(pathway_insert).size()){
throw "Unable to find base template to append to";
}
_.each(steps, function(step, index){
loadInStep(step, index, newScope, append_to, stepTemplateWrapper);
});
loadInSteps(parentDocument, steps, step_template_node, step_wrapper_template_url)
};

return {
load: function(newScope, append_to, stepTemplateWrapper, pathway_template_url, steps){
loadAllTemplates(pathway_template_url, steps).then(function(loadedHtml){
injectSteps(loadedHtml, newScope, append_to, stepTemplateWrapper, steps);
load: function(newScope, pathway_insert, step_wrapper_template_url, pathway_template_url, steps){
loadAllTemplates(pathway_template_url, step_wrapper_template_url, steps).then(function(loadedHtml){
var step_template_node = ".step-template";
injectSteps(loadedHtml, newScope, pathway_insert, step_template_node, step_wrapper_template_url, steps);
});
}
};
Expand Down
4 changes: 0 additions & 4 deletions pathway/static/js/pathway/services/wizard_pathway.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ angular.module('opal.services').service('WizardPathway', function(Pathway){
},
showNext: function(editing){
return true;
},
stepTemplateWrapper: function(loadedHtml, index){
// wraps the loaded template
return "<div ng-if='pathway.currentIndex === " + index + "'>" + loadedHtml + "</div>";
}
};
_.extend(WizardPathway.prototype, additionalPrototype);
Expand Down
17 changes: 9 additions & 8 deletions pathway/static/js/pathwaytest/pathway_service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ describe('Pathway', function() {
var FieldTranslater, pathwayScope;

var pathwayDefinition = {
'append_to': '.appendTo',
'icon': undefined,
'save_url': '/pathway/add_patient/sav',
'pathway_service': 'Pathway',
'steps': [
pathway_insert: '.pathwayInsert',
icon: undefined,
save_url: '/pathway/add_patient/sav',
pathway_service: 'Pathway',
steps: [
{
'step_controller': 'FindPatientCtrl',
'icon': 'fa fa-user',
Expand All @@ -23,8 +23,9 @@ describe('Pathway', function() {
'title': 'Location'
}
],
'template_url': '/templates/pathway/wizard_pathway.html',
'title': 'Add Patient'
step_wrapper_template_url: "/templates/pathway/step_wrappers/wizard.html",
template_url: '/templates/pathway/wizard_pathway.html',
title: 'Add Patient'
};

beforeEach(function(){
Expand Down Expand Up @@ -56,7 +57,7 @@ describe('Pathway', function() {
expect(pathway.save_url).toEqual("/pathway/add_patient/sav");
expect(pathway.stepDefinitions).toEqual(pathwayDefinition.steps);
expect(pathway.template_url).toEqual(pathwayDefinition.template_url);
expect(pathway.append_to).toEqual(pathwayDefinition.append_to);
expect(pathway.pathway_insert).toEqual(pathwayDefinition.pathway_insert);
expect(pathway.display_name).toEqual(pathwayDefinition.display_name);
expect(pathway.icon).toEqual(pathwayDefinition.icon);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
describe('pathwayTemplateLoader', function() {

});
12 changes: 2 additions & 10 deletions pathway/static/js/pathwaytest/pathway_wizard_service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ describe('WizardPathway', function() {
var pathwayScope, $q, pathwayTemplateLoader;

var pathwayDefinition = {
'append_to': '.appendTo',
'pathway_insert': '.pathwayInsert',
'icon': undefined,
'save_url': '/pathway/add_patient/sav',
'pathway_service': 'WizardPathway',
step_wrapper_template_url: "/templates/pathway/step_wrappers/wizard.html",
'steps': [
{
'step_controller': 'DefaultStep',
Expand Down Expand Up @@ -124,13 +125,4 @@ describe('WizardPathway', function() {
expect(pathway.showNext({})).toBe(true);
});
});

describe("stepTemplateWrapper", function(){
it("should wrap the returned html", function(){
var result = pathway.stepTemplateWrapper("<a>something</a>", 0);
expect(result).toEqual(
"<div ng-if='pathway.currentIndex === 0'><a>something</a></div>"
);
});
});
});
3 changes: 0 additions & 3 deletions pathway/templates/pathway/modal_wizard_pathway.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
</div>
{% endblock %}
{% endblock pathway_header %}
{% block pathway_body %}
{% include "pathway/partial/wizard_pathway_body.html" %}
{% endblock pathway_body %}
{% block pathway_footer %}
<div class="col-md-10 col-md-push-1">
{% include "pathway/partial/wizard_next_button.html" %}
Expand Down
3 changes: 2 additions & 1 deletion pathway/templates/pathway/page_pathway.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends "pathway/pathway_base.html" %}

{% block pathway_body %}
<div class="to_append"></div>
<div class="to_append">
</div>
{% endblock pathway_body %}
20 changes: 0 additions & 20 deletions pathway/templates/pathway/partial/wizard_pathway_body.html
Original file line number Diff line number Diff line change
@@ -1,20 +0,0 @@
<div class="pathway-process-steps-padding">
<div class="row">
<div class="col-md-8 col-md-push-2">
<div class="panel panel-default">
<div class="panel-heading">
<h3>
<span ng-show="pathway.currentStep.icon">
<i class="[[ pathway.currentStep.icon ]]"></i>
</span>
[[ pathway.currentStep.display_name ]]
<small ng-show="pathway.numSteps > 1">[[ pathway.currentIndex + 1 ]] of [[ pathway.numSteps ]]</small>
</h3>
</div>
<div class="panel-body">
<div class="to_append"></div>
</div>
</div>
</div>
</div>
</div>
2 changes: 1 addition & 1 deletion pathway/templates/pathway/pathway_detail.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div class="appendTo"></div>
<div class="pathwayInsert"></div>
1 change: 1 addition & 0 deletions pathway/templates/pathway/step_wrappers/default.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span><div class="step-template"></div></span>
17 changes: 17 additions & 0 deletions pathway/templates/pathway/step_wrappers/page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="row">
<div class="col-md-8 col-md-push-2">
<div class="panel panel-default">
<div class="panel-heading">
<h3>
<span ng-show="step.icon">
<i class="[[ step.icon ]]"></i>
</span>
[[ step.display_name ]]
</h3>
</div>
<div class="panel-body">
<div class="step-template"></div>
</div>
</div>
</div>
</div>
20 changes: 20 additions & 0 deletions pathway/templates/pathway/step_wrappers/wizard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<div ng-show="pathway.currentStep.display_name === step.display_name" class="pathway-process-steps-padding">
<div class="row">
<div class="col-md-8 col-md-push-2">
<div class="panel panel-default">
<div class="panel-heading">
<h3>
<span ng-show="step.icon">
<i class="[[ step.icon ]]"></i>
</span>
[[ step.display_name ]]
<small ng-show="pathway.numSteps > 1">[[ pathway.currentIndex + 1 ]] of [[ pathway.numSteps ]]</small>
</h3>
</div>
<div class="panel-body">
<div class="step-template"></div>
</div>
</div>
</div>
</div>
</div>
25 changes: 0 additions & 25 deletions pathway/templates/pathway/wizard_pathway.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,6 @@
</div>
{% endblock %}
{% endblock header %}


{% block pathway_body %}
<div class="pathway-process-steps-padding">
<div class="row">
<div class="col-md-8 col-md-push-2">
<div class="panel panel-default">
<div class="panel-heading">
<h3>
<span ng-show="pathway.currentStep.icon">
<i class="[[ pathway.currentStep.icon ]]"></i>
</span>
[[ pathway.currentStep.display_name ]]
<small ng-show="pathway.numSteps > 1">[[ pathway.currentIndex + 1 ]] of [[ pathway.numSteps ]]</small>
</h3>
</div>
<div class="panel-body">
<div class="to_append"></div>
</div>
</div>
</div>
</div>
</div>
{% endblock pathway_body %}

{% block pathway_footer %}
<div ng-hide="pathway.currentScope.hideFooter" class="panel-footer">
<div class="row">
Expand Down
2 changes: 1 addition & 1 deletion pathway/tests/test_pathways.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,6 @@ def test_vanilla_to_dict(self):
self.assertEqual(as_dict["save_url"], reverse(
"pathway_create", kwargs=dict(name="dog_owner")
))
self.assertEqual(as_dict["append_to"], ".appendTo")
self.assertEqual(as_dict["pathway_insert"], ".pathwayInsert")
self.assertEqual(as_dict["template_url"], "/somewhere")
self.assertEqual(as_dict["pathway_service"], "Pathway")

0 comments on commit 48233c3

Please sign in to comment.