Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Updated to latest js-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
nialldonnellyfh committed May 21, 2014
1 parent f326ab2 commit 40294ff
Show file tree
Hide file tree
Showing 8 changed files with 2,351 additions and 2,217 deletions.
187 changes: 34 additions & 153 deletions appForms-backbone.js
Expand Up @@ -1975,9 +1975,9 @@ var FieldView = Backbone.View.extend({
value: function(value) {
var self = this;
if (value && !_.isEmpty(value)) {
this.valuePopulate(value);
self.valuePopulate(value);
}
return this.getValue();
return self.getValue();
},
getValue: function() {
var value = [];
Expand Down Expand Up @@ -2209,148 +2209,6 @@ FieldCameraView = FieldView.extend({
}
});
window.sampleImageNum = -1;
FieldCameraGroupView = FieldCameraView.extend({
initialize: function() {
FieldCameraView.prototype.initialize.call(this);
//Make sure 'this' is bound for setImageData, was incorrect on device!
// pass visible event down to all fields
var parent = this;
this.on('visible', function () {
var subviews = this.subviews;
_(subviews).forEach(function (fieldView) {
// this group is a camera view and contains itself
// we've already triggered visible on the group, so skip
if(parent !== fieldView){
fieldView.trigger('visible');
}
});
});
},

render: function () {
var self = this;
// this view subclasses camera view, so render it for first camera item
FieldCameraView.prototype.render.call(this);
this.options.order = 0;

this.subviews = [this]; // this is the first field i.e. this extends FieldCameraView
this.bind('imageAdded imageRemoved', this.updateFields, this);

// initialilse subsequent camera views from subfields
var options = this.model.get("fieldOptions").definition;
for(var i=1;i<options.maxRepeat;i++){
var subview = new FieldCameraView({
parentEl: self.options.parentEl,
parentView: self.options.parentView,
model: self.model,
order: i + 1,
formView: self.options.formView,
initHidden: self.model.IsRequired === '1' ? false: true // hide camera fields initially if they're not required
});
// bind event handler for whenever image is added/remove from field
subview.bind('imageAdded imageRemoved', self.updateFields, self); // purposely pass in self here as subviews need to be iterated over no matter which field changed
self.subviews.push(subview);
}

//ToDo subviews should probably be added in initialize?
// this.value(this.model.serialize());

// if restoring from a draft, may need to show some additional fields
this._optimiseVisibleFields();
},

updateFields: function () {
this._fillBlanks();
this._optimiseVisibleFields();
},

_fillBlanks: function () {
var groups = this._getGroupedFields();

// move any optional filled fields into empty required field spots
// NOTE: could move all filled fields here,
// but not necessary as required fields that are filled is what we want (and are always visible anyways).
// Only thing is, may have a blank required above a filled required. minor UI preference
_(groups.optFilled).forEach(function (optField, index) {
// get next empty field
var nextEmptyField = groups.empty[0];
// if field exists & is before the field we're trying to move, move it. Otherwise, do nothing
if (nextEmptyField && (nextEmptyField.getOrder() < optField.getOrder())) {
// remove entry from empty list as we'll be filling it here
groups.empty.shift();
// move image data to reqField
nextEmptyField.setImageData(optField.getImageData(), true);
// empty image data from optField
optField.setImageData(null, false);
groups.empty.push(optField); // field is now empty, add to end of empty list
}
});
},

_optimiseVisibleFields: function () {
// get groups again as they may have changed above (optional filled moved to req filled)
var groups = this._getGroupedFields();

// all fields image data in order. See how many optional fields we should show, if any
var amountToShow = groups.reqFilled.length >= groups.req.length ? Math.min(groups.opt.length, Math.max(0, groups.optFilled.length + 1)) : 0;
_(groups.opt).forEach(function (optField, index) {
if (index < amountToShow) {
optField.show();
} else {
optField.hide();
}
});
// this.contentChanged(); //Call contentChanged so all image data is set on the group model
},

// group fields based on required status and whether or not image data is filled
_getGroupedFields: function () {
var groups = {
req: [], // required fields
reqEmpty: [], // required empty fields
reqFilled: [], // required filled fields
opt: [], // optional fields
optEmpty: [], // optional empty fields
optFilled: [], // optional filled fields
empty: [] // empty fields
};

_(this.subviews).forEach(function (subview, index) {
if (subview.isRequired()) { // required field
groups.req.push(subview);
if (subview.hasImageData()) { // filled in
groups.reqFilled.push(subview);
} else { // empty
groups.reqEmpty.push(subview);
groups.empty.push(subview);
}
} else { // optional field
groups.opt.push(subview);
if (subview.hasImageData()) { // filled in
groups.optFilled.push(subview);
} else { // empty
groups.optEmpty.push(subview);
groups.empty.push(subview);
}
}
});
return groups;
},

value: function(value) {
if (value && !_.isEmpty(value)) {
_(this.subviews).forEach(function (subview, index) {
//subview might be the group, so we call value on FieldCameraView
FieldCameraView.prototype.value.call(subview, value);
});
}
value = {};
_(this.subviews).forEach(function (subview, index) {
$.extend(value, FieldCameraView.prototype.value.call(subview));
});
return value;
}
});
FieldCheckboxView = FieldView.extend({
checkboxes: '<div class="fh_appform_field_input <%= repeatingClassName%>"><div class="checkboxes"><%= choices %></div></div>',
choice: '<input data-fieldId="<%= fieldId %>" <%= checked %> data-index="<%= index %>" name="<%= fieldId %>[]" type="checkbox" class="field checkbox" value="<%= value %>" ><label class="choice" ><%= choice %></label><br/>',
Expand Down Expand Up @@ -2395,11 +2253,11 @@ FieldCheckboxView = FieldView.extend({
},
valuePopulateToElement: function(index,value) {
var wrapperObj=this.getWrapper(index);
if (!value || !value.selections || !(value.selections instanceof Array)){
if (!value || !(value instanceof Array)){
return;
}
for (var i=0; i < value.selections.length; i++){
var v=value.selections[i];
for (var i=0; i < value; i++){
var v=value[i];
wrapperObj.find("input[value='"+v+"']").attr("checked","checked");
}
}
Expand Down Expand Up @@ -2523,15 +2381,30 @@ FieldGeoView = FieldView.extend({
},
onElementShow: function(index){
var self = this;
var rmBtn = $(this.renderButton(index, "<i class='fa fa-times-circle'></i>&nbsp;Remove Location", "remove"));
var btnLabel = this.locationUnit === "latlong" ? 'Capture Location (Lat/Lon)' : 'Capture Location (East/North)';
btnLabel = _.template(this.buttonHtml, {"buttonText": btnLabel});
var geoButton = $(this.renderButton(index, btnLabel, "fhgeo"));


this.getWrapper(index).append(geoButton);
this.getWrapper(index).append(rmBtn);

geoButton.on("click", function(e){
self.getLocation(e, index);
});

rmBtn.on("click", function(e){
self.clearLocation(e, index);
rmBtn.hide();
});

rmBtn.hide();
},
clearLocation: function(e, index){
var textInput = this.getWrapper(index).find(".fh_appform_field_input");
textInput.val("");
this.geoValues.splice(index, 1);// Remove the geo value from the field
},
onRender: function() {
var that = this;
Expand All @@ -2552,14 +2425,21 @@ FieldGeoView = FieldView.extend({
renderElement: function(index) {
var location = this.geoValues[index];
var locStr = "";
var textInput = this.getWrapper(index).find(".fh_appform_field_input");
var wrapper = this.getWrapper(index);
var textInput = wrapper.find(".fh_appform_field_input");
if (location) {
if (this.locationUnit === "latlong") {
locStr = '(' + location.lat + ', ' + location["long"] + ')';
wrapper.find(".remove").show();
} else if (this.locationUnit === "eastnorth") {
locStr = '(' + location.zone+' '+location.eastings + ', ' + location.northings + ')';
wrapper.find(".remove").show();
} else {
$fh.forms.log.e("FieldGeo: Invalid location unit: " + locStr);
}
textInput.val(locStr);
} else {
wrapper.find(".remove").hide();
}
textInput.blur();
},
Expand Down Expand Up @@ -2594,7 +2474,9 @@ FieldGeoView = FieldView.extend({
"eastings":locArr[1],
"northings":locArr[2]
};
}
} else {
$fh.forms.log.e("FieldGeo: Invalid location unit: " + locStr);
}
that.renderElement(index);
}, function(msg, err) {
textInput.attr('placeholder', 'Location could not be determined');
Expand Down Expand Up @@ -3259,7 +3141,7 @@ var PageView=BaseView.extend({
var fieldType = field.getType();
if (self.viewMap[fieldType]) {

console.log("*- "+fieldType);
$fh.forms.log.l("*- "+fieldType);

self.fieldViews[field.get('_id')] = new self.viewMap[fieldType]({
parentEl: self.$el,
Expand All @@ -3269,7 +3151,7 @@ var PageView=BaseView.extend({
sectionName: sectionKey
});
} else {
console.warn('FIELD NOT SUPPORTED:' + fieldType);
$fh.forms.log.w('FIELD NOT SUPPORTED:' + fieldType);
}
});
}
Expand All @@ -3281,7 +3163,7 @@ var PageView=BaseView.extend({
var fieldType = field.getType();
if (self.viewMap[fieldType]) {

console.log("*- "+fieldType);
$fh.forms.log.l("*- "+fieldType);

self.fieldViews[field.get('_id')] = new self.viewMap[fieldType]({
parentEl: self.$el,
Expand Down Expand Up @@ -3811,7 +3693,6 @@ var FormView = BaseView.extend({
}
}
var count = tmpObj.length;
submission.reset();
for (i = 0; i < tmpObj.length; i++) {
var item = tmpObj[i];
fieldId = item.id;
Expand Down

0 comments on commit 40294ff

Please sign in to comment.