Skip to content

Commit

Permalink
Merge pull request kobotoolbox#968 from kobotoolbox/formpack-content-…
Browse files Browse the repository at this point in the history
…updates

Formpack content updates
  • Loading branch information
jnm authored Oct 17, 2016
2 parents 063ce08 + 25518fa commit f713230
Show file tree
Hide file tree
Showing 50 changed files with 1,749 additions and 518 deletions.
7 changes: 6 additions & 1 deletion helper/webpack-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module.exports = function (options) {
hot: false,
https: false,
optimize: false,
// if webpack is building too slowly, set "browserTests" to false
browserTests: true,
outputDir: path.resolve(__dirname, '../jsapp/compiled/'),
outputHash: true,
port: 3000,
Expand All @@ -31,8 +33,11 @@ module.exports = function (options) {
options = merge(defaultOptions, options || {});

var entry = {
app: options.entry || path.join(__dirname, '../jsapp/js/main.es6'),
app: options.entry || path.join(__dirname, '..', 'jsapp', 'js', 'main.es6'),
};
if (options.browserTests) {
entry.tests = path.resolve(__dirname, '..', 'test', 'index');
}

var scssIncludePaths = ['',
'~',
Expand Down
1 change: 0 additions & 1 deletion jsapp/js/editorMixins/existingForm.es6
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
} from '../utils';

import stores from '../stores';
import dkobo_xlform from '../../xlform/src/_xlform.init';

export default {
editorState: 'existing',
Expand Down
9 changes: 9 additions & 0 deletions jsapp/xlform/src/csv.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ module.exports = do ->
# * Delimiters
# * quoted fields
# * and standard fields

###
this regexp needs to be compiled with coffee-script 1.6.3
or, escape characters need to be escaped (see csv._objPattern
below this comment)
csv._objPattern = ///
(
\ #{strDelimiter}
Expand Down Expand Up @@ -214,6 +220,9 @@ module.exports = do ->
)
)
///gi
###

csv._objPattern = `new RegExp('(\\,|\\r?\\n|\\r|^)(?:"((?:(?:[^\\\\]|\\\\\\\\|[\\\\(?=")]"|[\\\\(?!")])*?))"|([^"\\,\\r\\n]*))', 'gi')`

while arrMatches = csv._objPattern.exec(strData)
strMatchedDelimiter = arrMatches[1]
Expand Down
6 changes: 5 additions & 1 deletion jsapp/xlform/src/model.aliases.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ module.exports = do ->
score: [
"begin score",
"end score",
"begin_score",
"end_score",
]
rank: [
"begin_rank",
"end_rank",
"begin rank",
"end rank",
]
Expand Down Expand Up @@ -63,7 +67,7 @@ module.exports = do ->
['today'],
['simserial'],
['subscriberid'],
['phonenumber'],
['phonenumber', 'phone_number'],
]

aliases.custom = q
Expand Down
2 changes: 1 addition & 1 deletion jsapp/xlform/src/model.configs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = do ->
end_time:
name: "end"
label: "end time"
description: "tecords when the survey was marked as completed"
description: "records when the survey was marked as completed"
default: true
asJson:
type: "end"
Expand Down
32 changes: 30 additions & 2 deletions jsapp/xlform/src/model.inputParser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,21 @@ module.exports = do ->
return true
inputParser.hasBeenParsed = hasBeenParsed

parseArr = (type='survey', sArr)->
flatten_translated_fields = (item, translations)->
for key, val of item
if _.isArray(val)
delete item[key]
_.map(translations, (_t, i)->
_translated_val = val[i]
if _t and _t isnt translations.preferred_translation
lang_str = "#{key}::#{_t}"
else
lang_str = key
item[lang_str] = _translated_val
)
item

parseArr = (type='survey', sArr, translations=false)->
counts = {
open: {}
close: {}
Expand Down Expand Up @@ -59,6 +73,10 @@ module.exports = do ->

for item in sArr
_groupAtts = $aliases.q.testGroupable(item.type)

if translations and translations.length > 0
item = flatten_translated_fields(item, translations)

if _groupAtts
if _groupAtts.begin
_pushGrp(_groupAtts.type, item)
Expand All @@ -77,9 +95,19 @@ module.exports = do ->

inputParser.parseArr = parseArr
inputParser.parse = (o)->
translations = o.translations or [null]
if translations and '#null_translation' of o
translations.preferred_translation = o['#null_translation']
if o['#null_translation'] isnt null and null in translations
translations[translations.indexOf(null)] = 'UNNAMED'

# sorts groups and repeats into groups and repeats (recreates the structure)
if o.survey
o.survey = parseArr('survey', o.survey)
o.survey = parseArr('survey', o.survey, translations)

if o.choices
o.choices = parseArr('choices', o.choices, translations)

# settings is sometimes packaged as an array length=1
if o.settings and _.isArray(o.settings) and o.settings.length is 1
o.settings = o.settings[0]
Expand Down
15 changes: 8 additions & 7 deletions jsapp/xlform/src/model.row.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ module.exports = do ->
outObj = {}
for [key, val] in @attributesArray()
if key is 'type' and val.get('typeId') in ['select_one', 'select_multiple']
result = {}
result[val.get('typeId')] = val.get('listName')
outObj['type'] = val.get('typeId')
outObj['select_from_list_name'] = val.get('listName')
continue
else
result = @getValue(key)
unless @hidden
Expand Down Expand Up @@ -117,9 +118,9 @@ module.exports = do ->
rr._afterIterator = (cb, ctxt)->
obj =
export_relevant_values: (surv, addl)->
surv.push(type: "end #{rr._beginEndKey()}")
surv.push(type: "end_#{rr._beginEndKey()}")
toJSON: ()->
type: "end #{rr._beginEndKey()}"
type: "end_#{rr._beginEndKey()}"
cb(obj) if ctxt.includeGroupEnds

_toJSON = rr.toJSON
Expand Down Expand Up @@ -161,7 +162,7 @@ module.exports = do ->

rr.toJSON = ()->
out = _toJSON.call(rr)
out.type = "begin #{rr._beginEndKey()}"
out.type = "begin_#{rr._beginEndKey()}"
if typeof @_additionalJson is 'function'
_.extend(out, @_additionalJson())
out
Expand Down Expand Up @@ -210,7 +211,7 @@ module.exports = do ->
if @_rankLevels
additionalSheets['choices'].add(@_rankLevels)
begin_xlsformrow = _.clone(@toJSON2())
begin_xlsformrow.type = "begin rank"
begin_xlsformrow.type = "begin_rank"
begin_xlsformrow['kobo--rank-items'] = @getList().get('name')
survey_arr.push(begin_xlsformrow)
``
Expand Down Expand Up @@ -257,7 +258,7 @@ module.exports = do ->
if score_list
additionalSheets['choices'].add(score_list)
output = _.clone(@toJSON2())
output.type = "begin score"
output.type = "begin_score"
output['kobo--score-choices'] = @getList().get('name')
survey_arr.push(output)
``
Expand Down
1 change: 0 additions & 1 deletion jsapp/xlform/src/model.rowDetails.skipLogic.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class rowDetailsSkipLogic.SkipLogicCriterion extends Backbone.Model
# old_question_type = @neverDefined?.increment_coffee_ref()
# old_question_type = (if `this._get_question()` then this._get_question().get_type()) or { name: null }
old_question_type = @_get_question()?.get_type?() or { name: null }
console.log('old_question_type: ', old_question_type, ' via ', @_get_question())
@set('question_cid', cid)
question_type = @_get_question().get_type()

Expand Down
38 changes: 38 additions & 0 deletions jsapp/xlform/src/model.survey.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ module.exports = do ->
@surveyDetails = new $surveyDetail.SurveyDetails([], _parent: @).loadSchema(options.surveyDetailsSchema || $configs.surveyDetailSchema)
@choices = new $choices.ChoiceLists([], _parent: @)
$inputParser.loadChoiceLists(options.choices || [], @choices)

@translations = options.translations or [null]
if @translations
# if 'preferred_translation' is set, then any reference to a null translation
# is actually a reference to the preferred translation.
if @translations.preferred_translation
_pt_index = @translations.indexOf(@translations.preferred_translation)
if _pt_index is -1
throw new Error("Translation #{@translations.preferred_translation} "
"not found in list: #{@translations.join(', ')}")
if not @translations.secondary_translation
# the secondary_translation is the next available translation in the list
@translations.secondary_translation = @translations[if _pt_index is 0 then 1 else 0]
else if -1 is @translations.indexOf(null)
# if no null translation exists, set the first two translations as the
# preferred and secondary
@translations.preferred_translation = @translations[0]
@translations.secondary_translation = @translations[1]
else
# if a null translation exists (e.g. with a column "label" in a survey with
# other "label::lang" columns) then it is the "preferred_translation" by default
_null_index = @translations.indexOf(null)
@translations.preferred_translation = @translations[_null_index]
_next_translation_index = (_null_index + 1) % @translations.length
@translations.secondary_translation = @translations[_next_translation_index]

if options.survey
if !$inputParser.hasBeenParsed(options)
options.survey = $inputParser.parseArr(options.survey)
Expand Down Expand Up @@ -128,6 +154,18 @@ module.exports = do ->
addlSheets =
choices: new $choices.ChoiceLists()


# pass interface parameters back to the server
if @translations and @translations.preferred_translation isnt null
obj['#null_translation'] = @translations.preferred_translation

# in case we had to rename the null translation to "UNNAMED" in order
# to "focus" the form builder on a named translation
if @translations and 'UNNAMED' in @translations
obj['#replace_with_null_translation'] = 'UNNAMED'

obj.translations = [].concat(@translations)

obj.survey = do =>
out = []
fn = (r)->
Expand Down
9 changes: 6 additions & 3 deletions jsapp/xlform/src/model.surveyFragment.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ $row = require './model.row'
$aliases = require './model.aliases'
$utils = require './model.utils'
$configs = require './model.configs'
$surveyDetail = require './model.surveyDetail'

module.exports = do ->

Expand Down Expand Up @@ -240,14 +241,14 @@ module.exports = do ->
for k, val of group.attributes
if k isnt '_isRepeat'
out[k] = val.getValue()
out.type = "begin #{group._groupOrRepeatKey()}"
out.type = "begin_#{group._groupOrRepeatKey()}"
out
groupEnd: ->
group = @
export_relevant_values: (surv, shts)-> surv.push(@toJSON())
toJSON: ()-> type: "end #{group._groupOrRepeatKey()}"
toJSON: ()-> type: "end_#{group._groupOrRepeatKey()}"

INVALID_TYPES_AT_THIS_STAGE = ['begin group', 'end group', 'begin repeat', 'end repeat']
INVALID_TYPES_AT_THIS_STAGE = ['begin_group', 'end_group', 'begin_repeat', 'end_repeat']
_determineConstructorByParams = (obj)->
formSettingsTypes = do ->
for key, val of $configs.defaultSurveyDetails
Expand All @@ -258,6 +259,8 @@ module.exports = do ->
throw new Error("Invalid type at this stage: #{type}")

if type in formSettingsTypes
# e.g. "today"
throw new Error("#{type} is not properly handled as a SurveyDetail")
$surveyDetail.SurveyDetail
else if type is 'score'
$row.Row
Expand Down
11 changes: 11 additions & 0 deletions jsapp/xlform/src/view.choices.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ module.exports = do ->
@d.append(@t)
@d.append(@c)
@$el.html(@d)
try
_tt = @model.getSurvey().getSurvey().translations
catch err
_tt = false

if _tt and _tt.secondary_translation
_t_opt = @model.get("label::#{_tt.secondary_translation}")
$("<small>").html("""
<span>🌐&nbsp;-&nbsp;</span>
<span class="translated-text">#{_t_opt}</span>
""").appendTo(@$el)
@
keyupinput: (evt)->
ifield = @$("input.inplace_field")
Expand Down
10 changes: 5 additions & 5 deletions jsapp/xlform/src/view.row.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ module.exports = do ->
# only render the row details which are necessary for the initial view (ie 'label')
@make_label_editable new $viewRowDetail.DetailView(model: @model.get('label'), rowView: @).render().insertInDOM(@)



@already_rendered = true
@

Expand All @@ -165,8 +163,9 @@ module.exports = do ->
@$header.after($viewTemplates.row.groupSettingsView())
@cardSettingsWrap = @$('.card__settings').eq(0)
@defaultRowDetailParent = @cardSettingsWrap.find('.card__settings__fields--active').eq(0)
for [key, val] in @model.attributesArray() when key in ["name", "_isRepeat", "appearance", "relevant"]
new $viewRowDetail.DetailView(model: val, rowView: @).render().insertInDOM(@)
for [key, val] in @model.attributesArray()
if key in ["name", "_isRepeat", "appearance", "relevant"] or key.match(/^label::/)
new $viewRowDetail.DetailView(model: val, rowView: @).render().insertInDOM(@)

if @hasNestedGroups()
@$('.xlf-dv-appearance').hide()
Expand Down Expand Up @@ -206,7 +205,8 @@ module.exports = do ->
@cardSettingsWrap = @$('.card__settings').eq(0)
@defaultRowDetailParent = @cardSettingsWrap.find('.card__settings__fields--question-options').eq(0)

for [key, val] in @model.attributesArray() when key isnt "label" and key isnt "type"
# don't display columns that start with a $
for [key, val] in @model.attributesArray() when !key.match(/^\$/) and key not in ["label", "type", "select_from_list_name"]
new $viewRowDetail.DetailView(model: val, rowView: @).render().insertInDOM(@)
@

Expand Down
2 changes: 1 addition & 1 deletion jsapp/xlform/src/view.surveyApp.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ module.exports = do ->
@warnings = options.warnings || []
@__rowViews = new Backbone.Model()
@ngScope = options.ngScope
@surveyStateStore = options.stateStore || {trigger:$.noop}
@surveyStateStore = options.stateStore || {trigger:$.noop, setState:$.noop}

$(document).on 'click', @deselect_rows

Expand Down
12 changes: 11 additions & 1 deletion jsapp/xlform/src/view.surveyApp.templates.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ module.exports = do ->
for warning in surveyApp.warnings
warnings_html += """<p class="survey-warnings__warning">#{warning}</p>"""
warnings_html += """<button class="survey-warnings__close-button js-close-warning">x</button></div>"""
if survey.translations
t0 = survey.translations.preferred_translation
t1 = survey.translations.secondary_translation
print_translation = (tx)-> if tx is null then "Unnamed translation" else tx
translations_content = "#{print_translation(t0)}"
if t1
translations_content += " [<small>#{print_translation(t1)}</small>]"
else
translations_content = "1&times;🌐"

"""
<div class="sub-header-bar">
Expand Down Expand Up @@ -95,10 +104,11 @@ module.exports = do ->
<p class="survey-header__description" hidden>
<hgroup class="survey-header__inner container">
<h1 class="survey-header__title">
<span class="form-title">#{survey.settings.get("form_title")}</span>
<span class="form-title">#{survey.settings.get("form_title") || _t("untitled")}</span>
</h1>
</hgroup>
</p>
<p>#{translations_content}</p>
</header>
#{warnings_html}
<div class="survey-editor form-editor-wrap container">
Expand Down
7 changes: 0 additions & 7 deletions kobo/apps/reports/constants.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
UNCHARTABLE_TYPES = [
'begin group',
'end group',
'begin repeat',
'end repeat',
'note',
]

CHARTABLE_TYPES = [
'select_one',
Expand Down
1 change: 1 addition & 0 deletions kobo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'jsapp'),
os.path.join(BASE_DIR, 'static'),
('mocha', os.path.join(BASE_DIR, 'node_modules', 'mocha'),),
)

from cachebuster.detectors import git
Expand Down
Loading

0 comments on commit f713230

Please sign in to comment.