diff options
author | vjeantet <valere.jeantet@gmail.com> | 2020-09-25 11:01:01 +0300 |
---|---|---|
committer | vjeantet <valere.jeantet@gmail.com> | 2020-09-25 11:01:01 +0300 |
commit | e2fd5749ff2086b88e51a7954c99580275b058df (patch) | |
tree | cff4a1e9da0d0e09b51629a6439209b329a631b2 | |
parent | a97ab9a9a71d02dd6a04ca04b4a0bdff611e84c7 (diff) |
checklist: condition are an DOM element attribute
-rw-r--r-- | layouts/shortcodes/c/show.html | 8 | ||||
-rw-r--r-- | static/js/checklist.js | 28 |
2 files changed, 15 insertions, 21 deletions
diff --git a/layouts/shortcodes/c/show.html b/layouts/shortcodes/c/show.html index 637f430..830c784 100644 --- a/layouts/shortcodes/c/show.html +++ b/layouts/shortcodes/c/show.html @@ -1,17 +1,15 @@ -{{ $formID := "6d2ab01b-fad4-4e70-83a8-b910ca848fec" }} -{{ $random := delimit (shuffle (split (md5 $formID) "" )) "" }} +{{ $random := delimit (shuffle (split (md5 .Page.File.UniqueID) "" )) "" }} {{ $id := printf "dp-show-%s" $random}} {{ $condition := trim (.Get 0) " "}} -{{ $regFormValue := printf "formValue('%s','$1')" $formID}} +{{ $regFormValue := printf "formValue('$1')"}} {{ $conditionFix := replaceRE `{(\w+)}` $regFormValue $condition }} {{ $conditionFix = replace $conditionFix ` and ` ` && ` }} {{ $conditionFix = replace $conditionFix ` or ` ` || ` }} {{ $conditionFix = replaceRE `[^)]+has\s+('\w+')` ".includes($1)" $conditionFix }} -<div id="{{$id}}" class="baz" style="display:none"> +<div id="{{$id}}" class="baz" style="display:none" cond="{{$conditionFix | safeHTMLAttr}}"> {{with .Get 1}} <input type="hidden" value="true" name="{{.}}"> {{end}} {{.Inner | markdownify}}</div> -<script type="text/javascript">$(registerShowIf('{{$id}}','{{$conditionFix}}'))</script> diff --git a/static/js/checklist.js b/static/js/checklist.js index efe5b8d..a0011ca 100644 --- a/static/js/checklist.js +++ b/static/js/checklist.js @@ -8,18 +8,11 @@ $( document ).ready(function() { $(this).parent().attr('id','toc-'+id) $(this).before('<input type="checkbox" disabled readonly>'); }); - // $.each(formConditions, function(i) { - // console.log(this.id, this.condition) - // }) }); - -function registerShowIf(elmID, condition) { - formConditions.unshift({id:elmID, condition:condition}); -} - - -function formValue(formID, fieldName) { +function formValue(fieldName) { + var formID = "6d2ab01b-fad4-4e70-83a8-b910ca848fec" + // var formElm = $('[name='+fieldName+']').parents('form'); var val = "" var field = $("#"+formID +' [name='+fieldName+']') if (field.attr("multiple") !== undefined) { @@ -148,7 +141,13 @@ function listenFormChange(id){ newWrap = wrap(this, newWrap) } }); - + + // Register Conditions + $.each($("#"+id+" div[cond]"), function(i) { + formConditions.unshift({id:$(this).attr('id'), condition:$(this).attr('cond')}); + }); + + // on form change update display $("#"+id).bind('change', function(e) { formChange(id) @@ -158,10 +157,6 @@ function listenFormChange(id){ formChange(id) } -function formData(name){ - return $("#"+name).serializeArray() -} - function slugify(a) { return a.toLowerCase().replace(/ /g, '-').replace(/[^\w-]+/g, ''); } @@ -173,7 +168,8 @@ function downloadFile(formID){ at: new Date().toISOString(), ref: "TODO", url: document.location.protocol+"//"+document.location.host+document.location.pathname, - data: formData(formID)}; + data: $("#"+id).serializeArray() + }; var filename = fileName+".checklist.json"; var blob = new Blob([JSON.stringify(obj)], {type: 'application/json'}); if (window.navigator && window.navigator.msSaveOrOpenBlob) { |