Welcome to mirror list, hosted at ThFree Co, Russian Federation.

checklist.js « js « static - github.com/vjeantet/hugo-theme-docport.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0466559e789aaa5d8eca6e2fa57b8560494830f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272

var formConditions = [];

$( document ).ready(function() {
    $.each($("#TableOfContents ul li a"), function(i) {
        id = $(this).attr("href")
        id = id.substring(1);
        $(this).parent().attr('id','toc-'+id)
        $(this).before('<input type="checkbox" disabled readonly>');
    });
});

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) {
		//multiple returns array
		val = []
		$.each(field.serializeArray(), function() {
			if (this.name == fieldName) {
				val.push(this.value)
			}
		});
	}else{
		//single return one scalar value
		$.each($("#"+formID).serializeArray(), function() {
			if (this.name == fieldName) {
				val = this.value
			}
		});
	}
	
	return val
}

function formChange(id){
    $.each(formConditions, function(i) {
        conditionEvaluation = eval(this.condition)
        var showDiv = $('#'+this.id)
        var showDivFields = showDiv.find('input, select, textarea')
        if (conditionEvaluation) {
          showDiv.show()
          showDivFields.prop("disabled", false);
        }else{
          showDiv.hide()
          showDivFields.prop("disabled", true);
          showDivFields.prop("checked",false)
        }
    });
    updateTOC(id)
}

function updateTOC(formID) {
    $.each($("#"+formID).children(), function(i) {
        var zone = $(this).attr("ref")
        var countNames = 0
        var started = 0
        if (zone !== undefined){
            // Selectionne les input/select/textarea d'une zone
            var lastName = ""
            $.each($(this).find('input:not([disabled]), select:not([disabled]), textarea:not([disabled])'), function(j){
                var name = $(this).attr("name")
                if ( name != lastName ){
                    lastName = name
                    countNames++
                    if  (this.tagName == "INPUT" && ($(this).attr("type") == "radio" || $(this).attr("type") == "checkbox") ){
                        if ($("#"+formID+" input[name="+name+"]:checked").length > 0){
                            started++
                        }
                        return
                   }
                } else {
                   return
                }

                var tagName =  this.tagName
                if (tagName == 'INPUT'){
                    var type = $(this).attr("type")
                    if (type == "text"){
                        if ($(this).val() != ''){
                            started++
                        }    
                    }else if (type == "radio" || type == "checkbox") {
                        if ($(this).prop("checked")){
                            started++
                        }
                    }
                }else if (tagName == 'SELECT'){
                    if ($(this).attr("multiple")!==undefined){
                        if ($(this).val().length > 0){
                            started++
                        }
                    }else{
                        if ($(this).val() != ''){
                            started++
                        }    
                    }
                }else if (tagName == 'TEXTAREA'){
                    if ($(this).val() != ''){
                        started++
                    }
                }else{

                }
            });
            if (countNames > 0 ) {
                var status = "ready"
                if (started == countNames) {
                    status = "done"
                    $("#TableOfContents #toc-"+zone+" input").attr('checked',true)
                    $("#TableOfContents #toc-"+zone+" input").prop("indeterminate", false)
                }else if (started >0){
                    status = "doing"
                    $("#TableOfContents #toc-"+zone+" input").attr('checked',false)
                    $("#TableOfContents #toc-"+zone+" input").prop("indeterminate", true)
                }else{
                    status = "ready"
                    $("#TableOfContents #toc-"+zone+" input").attr('checked',false)
                    $("#TableOfContents #toc-"+zone+" input").prop("indeterminate", false)
                }
                $("#TableOfContents #toc-"+zone+"").removeClass('cl-ready cl-doing cl-done').addClass('cl-'+status);
            }
        }
    });
}


function listenFormChange(id){
    // Wrap each Zone with a div
    var formChildren = $("#"+id).children()
    var formChildrenLen = formChildren.length;
    $.each(formChildren, function(i) {
        if (this.tagName == "H2" || i+1 == formChildrenLen) {
            this.parentNode.appendChild(newWrap);
            // console.log("zone",newWrap)
            // this.parentNode.appendChild(this); // decommenter si on veut sortir les h1
            newWrap = wrap(this) 
        }else{
            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)
	});

    // initial update display
    formChange(id)
}

function slugify(a) { 
    return a.toLowerCase().replace(/ /g, '-').replace(/[^\w-]+/g, ''); 
} 


function downloadFile(formID){
	var fileName = slugify($("#"+formID).attr("ref"))
    var obj = {
    	at: new Date().toISOString(),
    	ref: "TODO",
    	url: document.location.protocol+"//"+document.location.host+document.location.pathname,
    	data: $("#"+formID).serializeArray()
    };
    var filename = fileName+".checklist.json";
    var blob = new Blob([JSON.stringify(obj)], {type: 'application/json'});
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(blob, filename);
    } else{
        var e = document.createEvent('MouseEvents'),
        a = document.createElement('a');
        a.download = filename;
        a.href = window.URL.createObjectURL(blob);
        a.dataset.downloadurl = ['application/json', a.download, a.href].join(':');
        e.initEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        a.dispatchEvent(e);
    }
}

function onChooseFile(event, onLoadFileHandler) {
    if (typeof window.FileReader !== 'function')
        throw ("The file API isn't supported on this browser.");
    let input = event.target;
    if (!input)
        throw ("The browser does not properly implement the event object");
    if (!input.files)
        throw ("This browser does not support the `files` property of the file input.");
    if (!input.files[0])
        return undefined;
    let file = input.files[0];
    let fr = new FileReader();
    fr.onload = onLoadFileHandler;
    fr.readAsText(file);
}




function fillForm(formID){
    return function (e,o){
        jsonData = e.target.result
        let obj = JSON.parse(jsonData)

        currentUrl = document.location.protocol+"//"+document.location.host+document.location.pathname
        if (obj.url != currentUrl) {
            alert("Warning : this answer file was saved from a different page")
        }
        // check url with 

        $("#"+formID).deserialize(obj.data);
        formChange(formID)
    }
}


jQuery.fn.deserialize = function (data) {
    var f = this,
        map = {},
        find = function (selector) { return f.is("form") ? f.find(selector) : f.filter(selector); };

    jQuery.each(data, function () {
            n = this.name
            v = this.value
        if (!(n in map)) {
            map[n] = [];
        }
        map[n].push(v);
    })
    //Set values for all form elements in the data
    jQuery.each(map, function (n, v) {
        find("[name='" + n + "']").val(v);
    })
    //Clear all form elements not in form data
    find("input:text,select,textarea").each(function () {
        if (!(jQuery(this).attr("name") in map)) {
            jQuery(this).val("");
        }
    })
    find("input:checkbox:checked,input:radio:checked").each(function () {
        if (!(jQuery(this).attr("name") in map)) {
            this.checked = false;
        }
    })
    return this;
};


        
        var wrap = function (toWrap, wrapper) {
            wrapper = wrapper || document.createElement('div');
            if (toWrap.tagName==='H2'){
                $(wrapper).attr("ref",$(toWrap).attr("ref"))
                $(wrapper).attr("todo","10")
                $(wrapper).attr("raf","9") // reste à faire
                $(wrapper).attr("raf_req","2") // reste à faire obligatoire
                $(wrapper).attr("id","zone-"+$(toWrap).attr("ref"))
                $(wrapper).attr("class","checklist-zone")
            }
            
            wrapper.appendChild(toWrap);    // mettre else si on veut sortir les h1
            return wrapper
        };

        var newWrap