Skip to content

Commit

Permalink
completed makeover [Finishes #93556180]
Browse files Browse the repository at this point in the history
  • Loading branch information
Snkz committed May 4, 2015
1 parent 3841700 commit 5195ab8
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 136 deletions.
54 changes: 31 additions & 23 deletions dokomoforms/static/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ App.init = function(survey) {
// Load up any unsynced submissions
App.unsynced = JSON.parse(localStorage.unsynced)[self.survey.id] || [];

// Load up any unsynced facilities
App.unsynced_facilities =
JSON.parse(localStorage.unsynced_facilities || "{}");

// Load any facilities
App.facilities = JSON.parse(localStorage.facilities || "{}");
if (JSON.stringify(App.facilities) === "{}" && navigator.onLine) {
Expand All @@ -48,10 +52,6 @@ App.init = function(survey) {
);
}

// Load up any unsynced facilities
App.unsynced_facilities =
JSON.parse(localStorage.unsynced_facilities || "{}");

$('.sel')
.click(function(e) {
e.preventDefault();
Expand Down Expand Up @@ -112,7 +112,6 @@ App.sync = function() {
var unsynced = JSON.parse(localStorage.unsynced);
unsynced[self.survey.id] = App.unsynced;
localStorage['unsynced'] = JSON.stringify(unsynced);
console.log('localStorage', localStorage.unsynced);
};

App.message = function(text, style) {
Expand Down Expand Up @@ -530,11 +529,24 @@ Survey.prototype.submit = function() {
var response = ans.response;
var is_type_exception = ans.is_type_exception || false;
var metadata = ans.metadata || {};
var is_new_facility = metadata.is_new; //XXX: Should I remove this is new marking?

if (response == null) {
return;
}

if (is_new_facility) {
// Record this new facility for Revisit s)ubmission
App.unsynced_facilities[response.id] = {
'name': metadata.name, 'uuid': response.id,
'properties' : {'sector': metadata.sector},
'coordinates' : [response.lon, response.lat]
};

// Store it in facilities as well
App.facilities[response.id] = App.unsynced_facilities[response.id];
}

survey_answers.push({
question_id: q.question_id,
answer: response,
Expand Down Expand Up @@ -578,11 +590,8 @@ Survey.prototype.submit = function() {
// Save Submission data
App.unsynced.push(data);
var unsynced = JSON.parse(localStorage.unsynced);
console.log(unsynced);
unsynced[self.id] = App.unsynced;
console.log(unsynced);
localStorage['unsynced'] = JSON.stringify(unsynced);
console.log('localStorage', localStorage.unsynced);

App.message('Saved Submission!', 'message_success');
App.splash();
Expand Down Expand Up @@ -624,7 +633,6 @@ Widgets._input = function(question, page, footer, type) {
.find('.text_input')
.keyup(function() { //XXX: Change isn't sensitive enough on safari?
var ans_ind = $(page).find('input').index(this);
console.log('value recieved', ans_ind, this.value, typeof this.value);
question.answer[ans_ind] = {
response: self._validate(type, this.value, question.logic),
is_type_exception: false,
Expand Down Expand Up @@ -1227,7 +1235,6 @@ Widgets.facility = function(question, page, footer) {
e.preventDefault();
var rbutton = $(this).find('input[type=radio]').first();
var uuid = rbutton.val();
console.log(uuid);

var rbutton = rbutton;
if (question.answer[0] && question.answer[0].response.id === uuid) {
Expand Down Expand Up @@ -1290,7 +1297,6 @@ Widgets.facility = function(question, page, footer) {
.click(function() {
if (question.answer[0] && question.answer[0].metadata.is_new) {
$('.facility__btn').text("add facility");
console.log(question.answer[0]);
question.answer = [];
$('.question__add__facility').hide();
//$('.question__map').show();
Expand All @@ -1316,7 +1322,8 @@ Widgets.facility = function(question, page, footer) {

question.answer = [{
response: {'id': uuid, 'lat': lat, 'lon': lon },
metadata: {'name': name, 'sector': sector, 'is_new': true }
metadata: {'name': name, 'sector': sector, 'is_new': true },
failed_validation: Boolean(!name || !sector)
}];

$('.facility_uuid_input').val(uuid);
Expand All @@ -1325,15 +1332,9 @@ Widgets.facility = function(question, page, footer) {
$('.facility_sector_input').val(sector);

captureCallback = updateLocation;
console.log(question.answer[0]);
}

// Record this new facility for Revisit s)ubmission
//App.unsynced_facilities[uuid] = {
// 'name': 'New Facility', 'uuid': uuid,
// 'properties' : {'sector': 'other'},
// 'coordinates' : [lng, lat]
//};
console.log(question.answer[0]);

});

Expand All @@ -1342,14 +1343,19 @@ Widgets.facility = function(question, page, footer) {
.find('.facility_name_input')
.keyup(function() {
question.answer[0].metadata.name = this.value;
var name = this.value;
var sector = question.answer[0].metadata.sector;
question.answer[0].failed_validation = Boolean(!name || !sector);
});

// Sector input
$(page)
.find('.facility_sector_input')
.change(function() {
console.log("Sector", this.value);
question.answer[0].metadata.sector = this.value;
var sector = this.value;
var name = question.answer[0].metadata.name;
question.answer[0].failed_validation = Boolean(!name || !sector);
});

// Location callback
Expand Down Expand Up @@ -1380,6 +1386,11 @@ function getNearbyFacilities(lat, lng, rad, lim, cb) {
data.facilities.forEach(function(facility) {
facilities[facility.uuid] = facility;
});
// Add in our unsynced ones as well
Object.keys(App.unsynced_facilities).forEach(function(uuid) {
facilities[uuid] = App.unsynced_facilities[uuid];
});

App.facilities = facilities;
localStorage.setItem("facilities", JSON.stringify(facilities));
if (cb) {
Expand All @@ -1401,10 +1412,8 @@ function postNewFacility(facility) {
dataType: 'json',
success: function() {
App.message('Facility Added!', 'message_success');

// If posted, we don't an unsynced reference to it anymore
delete App.unsynced_facilities[facility.uuid];
App.facilities[facility.uuid] = facility;
},

headers: {
Expand All @@ -1417,7 +1426,6 @@ function postNewFacility(facility) {
},

complete: function() {
// Add it into facilities array so it can be selected later
localStorage.setItem("facilities",
JSON.stringify(App.facilities));

Expand Down
2 changes: 1 addition & 1 deletion dokomoforms/static/cache.appcache
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CACHE MANIFEST
# Version 0.0123456789012345670
# Version 0.0123456789012345678

/static/persona.js
/static/lib.js
Expand Down
6 changes: 3 additions & 3 deletions js_tests/emulate_dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ module.exports = (function(url) {
//jsdom.getVirtualConsole(window).sendTo(console);

// leaflet monkeypatches thanks to jieter on github
window.L.Map.prototype.getSize = function () {
return L.point(1024, 1024);
};
//window.L.Map.prototype.getSize = function () {
// return L.point(1024, 1024);
//};

document._ce = document.createElement;
document.createElement = function(elem) {
Expand Down
100 changes: 12 additions & 88 deletions js_tests/widget_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@ describe('Widget creation tests', function(done) {


beforeEach(function(done) {
$.mockjax.clear();
App.facilities = [];
App.unsynced_facilities = {};
App.location = {};
done();
});

afterEach(function(done) {
$.mockjax.clear();
$(".page_nav__next").off('click'); //XXX Find out why events are cached
$(".page_nav__prev").off('click');
$('.content').empty();
$('.bar-footer').empty()
localStorage = {};
App.location = {};
done();
});

Expand Down Expand Up @@ -468,81 +472,7 @@ describe('Widget creation tests', function(done) {

});

it('should render facility widget with default location',
function(done) {
var question = {
question_to_sequence_number: -1,
type_constraint_name: "facility",
logic: {},
answer: [],
question_title: "Good year for flixs though",
sequence_number: 1
};

// Create content div with widget template
var widgetHTML = $('#widget_' + question.type_constraint_name).html();
var widgetTemplate = _.template(widgetHTML);
var compiledHTML = widgetTemplate({question: question, start_loc: {'lat': 40, 'lon': 70}});

$('.content')
.data('index', 1)
.html(compiledHTML)

Widgets[question.type_constraint_name](question, $('.content'));

$('.question__title')
.text()
.should.match("Good year for flixs though");

done();

});

it('should render facility widget with default location and retrieve facilities',
function(done) {
//XXX: Fake Revisit response, make this an actual test
var question = {
question_to_sequence_number: -1,
type_constraint_name: "facility",
logic: {},
answer: [],
question_title: "Good year for flixs though",
sequence_number: 1
};

//XXX: Figure this out
//navigator.onLine = true

// Create content div with widget template
var widgetHTML = $('#widget_' + question.type_constraint_name).html();
var widgetTemplate = _.template(widgetHTML);
var compiledHTML = widgetTemplate({question: question, start_loc: {'lat': 40, 'lon': 70}});

$('.content')
.data('index', 1)
.html(compiledHTML)

var barfootHTML = $('#template_footer').html();
var barfootTemplate = _.template(barfootHTML);
compiledHTML = barfootTemplate({
'other_text': question.logic.other_text
});

$('.bar-footer').html(compiledHTML);
$('.bar-footer').removeClass('bar-footer-extended');
$('.bar-footer').removeClass('bar-footer-super-extended');
$('.bar-footer').css("height", "");

Widgets[question.type_constraint_name](question, $('.content'), $('.bar-footer'))

$('.question__title')
.text()
.should.match("Good year for flixs though");

done();

});

it('should render facility widget with default location and some default facilities',
function(done) {
var question = {
Expand All @@ -553,14 +483,15 @@ describe('Widget creation tests', function(done) {
question_title: "Good year for flixs though+",
sequence_number: 1
};

// Preload some facilities;
App.facilities = require('./fixtures/facilities.json');
App.location = {'lat': 40, 'lon': 70};

// Create content div with widget template
var widgetHTML = $('#widget_' + question.type_constraint_name).html();
var widgetTemplate = _.template(widgetHTML);
var compiledHTML = widgetTemplate({question: question, start_loc: {'lat': 40, 'lon': 70}});
var compiledHTML = widgetTemplate({question: question});

$('.content')
.data('index', 1)
Expand All @@ -583,13 +514,13 @@ describe('Widget creation tests', function(done) {
.text()
.should.match("Good year for flixs though+");

$('#map').find('.leaflet-marker-icon').length.should.be.exactly(68); // basic check to see if markers are rendered

$('.question__radio').length.should.match(10);
done();

});

it('should render facility widget with default location an unsynced facility',
it('should render facility widget with unset location and no facilities',
function(done) {
var question = {
question_to_sequence_number: -1,
Expand All @@ -600,17 +531,10 @@ describe('Widget creation tests', function(done) {
sequence_number: 1
};

// Preload some facilities;
App.unsynced_facilities[1] = {
'name': 'New Facility', 'uuid': 1,
'properties' : {'sector': 'health'},
'coordinates' : [40.01, 70.01]
};

// Create content div with widget template
var widgetHTML = $('#widget_' + question.type_constraint_name).html();
var widgetTemplate = _.template(widgetHTML);
var compiledHTML = widgetTemplate({question: question, start_loc: {'lat': 40, 'lon': 70}});
var compiledHTML = widgetTemplate({question: question});

$('.content')
.data('index', 1)
Expand All @@ -633,8 +557,8 @@ describe('Widget creation tests', function(done) {
.text()
.should.match("That lego movie song sucks");

$('#map').find('.leaflet-marker-icon').length.should.be.exactly(1); // basic check to see if markers are rendered

$('.question__radio').length.should.match(0);
$('.facility__btn')[0].style.display.should.equal('none'); // Should be there but not visible
done();

});
Expand Down

0 comments on commit 5195ab8

Please sign in to comment.