Skip to content

Commit

Permalink
Added check for notes while building question list plus its js test - f…
Browse files Browse the repository at this point in the history
  • Loading branch information
larryweya committed Sep 24, 2012
1 parent b8b7554 commit 1dfcdc1
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 66 deletions.
22 changes: 22 additions & 0 deletions js_tests/mocks/instance_view.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var QuestionData = [
{
name: 'group_one',
type: 'group',
label: 'A Group',
children: [
{
name: 'note_one',
type: 'group',
label: 'A Note',
children: [
{
name: 'a_question',
type: 'text',
label: 'A Question'
}
]
}
]
}
];
var questions = {};
15 changes: 15 additions & 0 deletions js_tests/specs/instance_view.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
EnvJasmine.load(EnvJasmine.mocksDir + "instance_view.mock.js");
EnvJasmine.load(EnvJasmine.jsDir + "main/static/js/underscore-min.js");
EnvJasmine.load(EnvJasmine.mocksDir + "../utils.js");
EnvJasmine.load(EnvJasmine.jsDir + "odk_viewer/static/js/instance.js");

describe("Instance View tests", function() {
it("checks that parseQuestions builds hierachy's as expected", function() {
expect(QuestionData).toBeDefined();
expect(questions).toBeDefined();
parseQuestions(QuestionData);
// must only have one question
expect(Object.size(questions)).toEqual(1);
expect(questions['note_one_a_question']).toBeDefined();
});
});
8 changes: 8 additions & 0 deletions js_tests/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// get size of an object
Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
51 changes: 51 additions & 0 deletions odk_viewer/static/js/instance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Question = function(questionData)
{
this.name = questionData.name;
this.type = questionData.type;
this.label = questionData.label;
}

Question.prototype.getLabel = function(language)
{
/// if plain string, return
if(typeof(this.label) == "string")
return this.label;
else if(typeof(this.label) == "object")
{
if(language && this.label.hasOwnProperty(language))
return this.label[language];
else
{
var label = null;
for(key in this.label)
{
label = this.label[key];
break;// break at first instance and return that
}
return label;
}

}
// return raw name
return this.name;
}

function parseQuestions(children, prefix, cleanReplacement)
{
var idx;
cleanReplacement = typeof cleanReplacement !== 'undefined' ? cleanReplacement : '_';

for(idx in children)
{
var question = children[idx];
if(question.hasOwnProperty('children') && (question.type == "group" || question.type == "note"))
{
parseQuestions(question.children, (question.name + cleanReplacement));
}
else
{
// TODO: question class that has accessor mesthods for type, label, language etc
questions[((prefix?prefix:'') + question.name)] = new Question(question);
}
}
}
83 changes: 17 additions & 66 deletions odk_viewer/templates/instance.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,6 @@ <h1>{% trans "Browse Form Data" %}(<a href="{% url main.views.show username xfor
var numRecords = null;
var browsePos = null;

Question = function(questionData)
{
this.name = questionData.name;
this.type = questionData.type;
this.label = questionData.label;
}

Question.prototype.getLabel = function(language)
{
/// if plain string, return
if(typeof(this.label) == "string")
return this.label;
else if(typeof(this.label) == "object")
{
if(language && this.label.hasOwnProperty(language))
return this.label[language];
else
{
var label = null;
for(key in this.label)
{
label = this.label[key];
break;// break at first instance and return that
}
return label;
}

}
// return raw name
return this.name;
};

(function($) {

app = $.sammy('#data', function() {
Expand Down Expand Up @@ -93,20 +61,20 @@ <h1>{% trans "Browse Form Data" %}(<a href="{% url main.views.show username xfor

// Delete modal
this.get('#del/:id', function(context) { with(this) {

$("#delete-modal").modal("show");

}
});


// Delete route
this.get('#delete/:id', function(context) {
this.get('#delete/:id', function(context) {

var id = this.params['id'];
var next = $('li.next').children('a').attr('href');
next = next.replace("#/", "");

var prev = $('li.prev').children('a').attr('href');
prev = prev.replace("#/", "");

Expand All @@ -122,7 +90,7 @@ <h1>{% trans "Browse Form Data" %}(<a href="{% url main.views.show username xfor

var query = '{"_id": ' + id + '}';
editData(context, query, next_prev);

// load count >> update record
$.getJSON(mongoAPIUrl, {'count': 1})
.success(function(data){
Expand Down Expand Up @@ -158,9 +126,9 @@ <h1>{% trans "Browse Form Data" %}(<a href="{% url main.views.show username xfor
//TODO: show loader
$.getJSON(editAPIUrl, {'query': query, 'limit':1})
.success(function(data){

loadData(context, next_prev);

})
.error(function(){
alert("{% trans 'BAD REQUEST' %}");
Expand All @@ -174,10 +142,10 @@ <h1>{% trans "Browse Form Data" %}(<a href="{% url main.views.show username xfor
$.getJSON(mongoAPIUrl, {'query': query, 'limit':1})
.success(function(data){
reDraw(context, data[0]);

//ADD EDIT AND BUTTON CHECK PERMISSION
updateButtons(data[0]);

//alert(data[0]['_id']);
// check if we initialised the browsePos
if(false)//TODO: find a way to increment browsePos client-side
Expand Down Expand Up @@ -229,7 +197,7 @@ <h1>{% trans "Browse Form Data" %}(<a href="{% url main.views.show username xfor
editbutton.removeClass('disabled');
editbutton.attr('href', 'edit/' + data['_id']);
*/

//Make Delete Button visible and add link
var deletebutton = $('a.btn-danger');
deletebutton.removeClass('disabled');
Expand Down Expand Up @@ -340,18 +308,18 @@ <h1>{% trans "Browse Form Data" %}(<a href="{% url main.views.show username xfor
var statusStr = '<div class="span6"><div class="dataTables_info"><h4 class="record-pos">{% trans "Record 1 of 6" %}</h4></div></div>';
var topStatus = $(statusStr);
topStatusNavRows.append(topStatus);

var pagerStr = '<div class="span6"><div class="dataTables_paginate paging_bootstrap pagination"><ul><li class="prev disabled"><a href="#">{% trans "← Previous" %}</a></li><li class="next disabled"><a href="#">{% trans "Next →" %} </a></li></ul></div></div>';
var topPager = $(pagerStr);

topStatusNavRows.append(topPager);
dataContainer.append(topStatusNavRows);

{% if can_edit %}
var editDelete = '<div class="row"><div class="span6"><a id="title_edit" href="#kate" class="btn small bind-edit disabled">{% trans "edit" %}</a>&nbsp;<a href="#"class="btn btn-small btn-danger">{% trans "Delete" %}</a></div></div>';
dataContainer.append(editDelete);
{% endif %}

var table = $('<table id="data-table" class="table table-bordered table-striped"></table');
var tHead = $('<thead><tr><th class="header" width="50%">{% trans "Question" %}</th><th class="header">{% trans "Response" %}</th></tr></thead>');
var tBody = $('<tbody></tbody>');
Expand Down Expand Up @@ -396,24 +364,6 @@ <h1>{% trans "Browse Form Data" %}(<a href="{% url main.views.show username xfor
setLanguage(languages[0]);
}

function parseQuestions(children, prefix)
{
var idx;
for(idx in children)
{
var question = children[idx];
if(question.hasOwnProperty('children') && question.type == "group")
{
parseQuestions(question.children, (question.name + cleanReplacement));
}
else
{
// TODO: question class that has accessor mesthods for type, label, language etc
questions[((prefix?prefix:'') + question.name)] = new Question(question);
}
}
}

function parseLanguages(children)
{
// run through question objects, stop at first question with label object and check it for multiple languages
Expand Down Expand Up @@ -452,4 +402,5 @@ <h3>{% trans "Delete Confirmation" %}</h3>
<a href="#" onclick="$('#delete-modal').modal('hide');" class="btn secondary">{% trans "Cancel" %}</a>
</div>
</div>
<script type="text/javascript" charset="utf-8" src="/static/js/instance.js"></script>
{% endblock %}
1 change: 1 addition & 0 deletions run_js_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./js_tests/EnvJasmine/bin/run_all_tests.sh --testDir=../ --configFile=../env_jasmine.conf.js

0 comments on commit 1dfcdc1

Please sign in to comment.