aboutsummaryrefslogtreecommitdiffstats
path: root/97suifangqa/apps/indicator/static/javascripts/edit_history_data.js
blob: d4ccdf5913cd10c2b50759ea362fa82f86a6d8d8 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// global var to store the data of record
var record_data = {
    date: null,
    value: null,
    val_min: null,
    val_max: null,
    reason: null
};

$(document).ready(function(){
    //点大叉,关闭弹层页面
    $(".edit_history_data_close").bind("click", function(){
        parent.TB_remove();
        return false;
    });
    // jquery-ui: datepicker
    $("#editing_date_picker").datepicker({
        showOn: "both",
        buttonImage: static_url + "images/calendar.png",
        buttonImageOnly: true,
        maxDate: 0      // 0->today, 1->tomorrow
    });

    // delete button
    $("#delete_btn").bind("click", function(){
        // delete record (ajax)
        var time = moment().valueOf();
        $.ajax({
            type: 'get',
            url: indicator_url + 'ajax/delete_record/',
            data: 'record_id='+record_id + '&time='+time,
            success: function(data) {
                if (data == 'success') {
                    // redraw chart
                    var begin_str = $("#search_begin_date", window.parent.document).val();
                    var end_str = $("#search_end_date", window.parent.document).val();
                    //console.log("begin_str: "+begin_str);
                    //console.log("end_str: "+end_str);
                    var getdata_type = "date";
                    var getdata_num = null;
                    parent.detail_chart_getdata_draw(
                            parent.detail_chart_str,
                            parent.detail_chart_options_str,
                            getdata_type, getdata_num,
                            begin_str, end_str
                    );
                    // close popup window
                    parent.TB_remove();
                }
            }
        });
        return false;
    });

    // edit button
    $("#edit_btn").bind("click", function(){
        var this_edit_data_div = $(this).closest(".edit_data");
        var this_editing_data_div = this_edit_data_div.siblings(".editing_data");
        this_editing_data_div.show();
        this_edit_data_div.hide();
        return false;
    });

    // save botton {{{
    $("#save_btn").bind("click", function(){
        // force to trigger 'validate' before save
        $(".editing_data .to_validate").trigger('validate');
        if ($(".editing_data .invalid").length) {
            // TODO: tooltip/popup
            return false;
        }

        // modified data validated
        var cur_mm = moment();
        var created_at_str = cur_mm.toISOString();
        var time = cur_mm.valueOf();
        $.ajax({
            type: 'post',
            url: indicator_url + 'ajax/modify_record/',
            data: {
                csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value,
                record_id: record_id,
                date: record_data.date,
                value: record_data.value,
                val_min: record_data.val_min,
                val_max: record_data.val_max,
                reason: record_data.reason,
                created_at: created_at_str,
                time: time
            },
            dataType: 'json',
            success: function(dataJson) {
                if (dataJson.failed == true) {
                    // tooltip
                }
                else {
                    // redraw chart
                    var begin_str = $("#search_begin_date", window.parent.document).val();
                    var end_str = $("#search_end_date", window.parent.document).val();
                    var end_mm = moment(end_str);
                    var record_date_mm = moment(record_data.date);
                    if (record_date_mm.isAfter(end_mm)) {
                        end_str = record_date_mm.format('YYYY-MM-DD');
                    }
                    //
                    var getdata_type = "date";
                    var getdata_num = null;
                    parent.detail_chart_getdata_draw(
                            parent.detail_chart_str,
                            parent.detail_chart_options_str,
                            getdata_type, getdata_num,
                            begin_str, end_str
                    );
                    // successfully modified
                    parent.TB_remove();
                }
            }
        });
        return false;
    });
    // }}}

    // initialize
    // set datepicker 'date_input' value
    var date_init = $.datepicker.parseDate('yy-mm-dd',
            $(".date_input").attr('value'));
    $(".date_input").datepicker("setDate", date_init);
    // select radio button according to the original value
    if ($(".editing_data .radio_input").length) {
        $(".radio_input input:radio").prop("checked", false);
    }
    if (record.value === '-') {
        $(".radio_input #minus_r").prop("checked", true);
    }
    else {
        $(".radio_input #plus_r").prop("checked", true);
    }

    // record data validate {{{
    // date {{{
    // date_input tooltip
    var dateinput_help = '<p>日期格式:YYYY-MM-DD; 如:2013-08-26</p><p>日期不能晚于<strong>今天</strong></p>';
    $(".date_input").qtip({
        id: 'dateinput',        // -> '#qtip-dateinput'
        prerender: false,
        content: {
            text: dateinput_help
        },
        position: {
            my: 'bottom left',
            at: 'top right'
        },
        show: {
            event: 'mouseenter'
        },
        hide: {
            event: 'mouseleave unfocus'
        }
    });
    $(".date_input").focus(function() {
        $(this).removeClass("valid invalid");
        // show help tooltip
        var qtip_content = dateinput_help;
        $(this).qtip('api').set('content.text', qtip_content);
        $(this).qtip('api').show();
    });
    $(".date_input").on('validate', null, function() {
        var date_str = $(this).val();
        var date_mm = moment(date_str, 'YYYY-MM-DD');
        var today_mm = moment();
        //console.log('date_str: ', date_str);
        // date cannot beyond today
        if (date_mm.isValid() && !date_mm.isAfter(today_mm)) {
            $(this).removeClass("invalid");
            $(this).addClass("valid");
            var qtip_content = dateinput_help;
            $(this).qtip('api').set('content.text', qtip_content);
            $(this).qtip('api').hide();
            // update data
            record_data.date = date_str;
        }
        else {
            // date invalid
            $(this).removeClass("valid");
            $(this).addClass("invalid");
            var qtip_content = '<p>请检查输入日期的格式,并且日期不能晚于<strong>今天</strong></p>';
            $(this).qtip('api').set('content.text', qtip_content);
            $(this).qtip('api').show();
        }
    });
    $(".date_input").on('change blur', null, function() {
        $(this).trigger('validate');
    });
    // }}}

    // validate data
    if (data_type == DATA_TYPES_JS.INTEGER_TYPE) {
        // INTEGER_TYPE
        // TODO
    }
    else if (data_type == DATA_TYPES_JS.FLOAT_TYPE) {          // {{{
        // FLOAT_TYPE
        var datainput_help = '<p>定值型</p><p>数据格式示例:123.7; 1.23e4; 3.5e-2</p>';
        // tooltip
        $(".data_input").qtip({
            id: 'datainput',
            prerender: false,
            content: {
                text: datainput_help
            },
            position: {
                my: 'bottom left',
                at: 'top right'
            },
            show: {
                event: 'mouseenter'
            },
            hide: {
                event: 'mouseleave unfocus'
            }
        });
        $(".data_input").focus(function() {
            $(this).removeClass("valid invalid");
            // show help tooltip
            var qtip_content = datainput_help;
            $(this).qtip('api').set('content.text', qtip_content);
            $(this).qtip('api').show();
        });
        // validate
        $(".data_input").on('validate', null, function() {
            var value_str = $(this).val();
            var value = is_float(value_str);
            if (value === false) {
                // format invalid
                $(this).removeClass("valid");
                $(this).addClass("invalid");
                var qtip_content = '<p>数据格式不符合要求,请检查后重新输入</p>';
                $(this).qtip('api').set('content.text', qtip_content);
                $(this).qtip('api').show();
            }
            else {
                // format valid
                // check confine
                if (value >= confine.math_min &&
                        value <= confine.math_max) {
                    // confine valid
                    $(this).removeClass("invalid");
                    $(this).addClass("valid");
                    var qtip_content = datainput_help;
                    $(this).qtip('api').set('content.text',
                            qtip_content);
                    $(this).qtip('api').hide();
                    // update data
                    record_data.value = value;
                }
                else {
                    // confine tooltip
                    $(this).removeClass("valid");
                    $(this).addClass("invalid");
                    var qtip_content = '<p>数值超出范围</p><p>允许数据范围:'+confine.math_range_html+' </p>';
                    $(this).qtip('api').set('content.text',
                            qtip_content);
                    $(this).qtip('api').show();
                }
            }
        });
        $(".data_input").on('blur', null, function() {
            $(this).trigger('validate');
        });
    } // }}}
    else if (data_type == DATA_TYPES_JS.RANGE_TYPE) {          // {{{
        // RANGE_TYPE
        var datainput_help = '<p>范围型</p><p>数据格式示例:&bullet; <123.7; &bullet; >1.3e3; &bullet; 1.5e3 ~ 3.7e3</p>';
        // tooltip
        $(".data_input").qtip({
            id: 'datainput',
            prerender: false,
            content: {
                text: datainput_help
            },
            position: {
                my: 'bottom left',
                at: 'top right'
            },
            show: {
                event: 'mouseenter'
            },
            hide: {
                event: 'mouseleave unfocus'
            }
        });
        $(".data_input").focus(function() {
            $(this).removeClass("valid invalid");
            // show help tooltip
            var qtip_content = datainput_help;
            $(this).qtip('api').set('content.text', qtip_content);
            $(this).qtip('api').show();
        });
        // validate
        $(".data_input").on('validate', null, function() {
            var value_str = $(this).val();
            var value = is_range(value_str);
            if (is_range(value_str) === false) {
                // format invalid
                $(this).removeClass("valid");
                $(this).addClass("invalid");
                var qtip_content = '<p>数据格式不符合要求,请检查后重新输入</p>';
                $(this).qtip('api').set('content.text', qtip_content);
                $(this).qtip('api').show();
            }
            else {
                // range format valid
                // check if within 'confine'
                var confine_valid = false;
                var val_min = null;
                var val_max = null;

                var operator = value.operator;
                if (operator === '<') {
                    val_max = value.value;
                    if (val_max > confine.math_min &&
                            val_max <= confine.math_max) {
                        // valid
                        confine_valid = true;
                        val_min = confine.math_min;
                    }
                }
                else if (operator === '>') {
                    val_min = value.value;
                    if (val_min >= confine.math_min &&
                            val_min < confine.math_max) {
                        // valid
                        confine_valid = true;
                        val_max = confine.math_max;
                    }
                }
                else if (operator === '~') {
                    val_min = value.val_min;
                    val_max = value.val_max;
                    if (val_min >= confine.math_min &&
                            val_max < confine.math_max) {
                        // valid
                        confine_valid = true;
                    }
                }
                else {
                    confine_valid = false;
                }

                if (confine_valid === true) {
                    $(this).removeClass("invalid");
                    $(this).addClass("valid");
                    var qtip_content = datainput_help;
                    $(this).qtip('api').set('content.text',
                            qtip_content);
                    $(this).qtip('api').hide();
                    // update data
                    record_data.val_min = val_min;
                    record_data.val_max = val_max;
                }
                else {
                    // data not within range
                    $(this).removeClass("valid");
                    $(this).addClass("invalid");
                    var qtip_content = '<p>数值超出范围</p><p>允许数据范围:'+confine.math_range_html+' </p>';
                    $(this).qtip('api').set('content.text',
                            qtip_content);
                    $(this).qtip('api').show();
                }
            }
        });
        $(".data_input").on('blur', null, function() {
            $(this).trigger('validate');
        });
    } // RANGE_TYPE }}}
    else if (data_type == DATA_TYPES_JS.FLOAT_RANGE_TYPE) {
        // TODO
    }
    else if (data_type == DATA_TYPES_JS.PM_TYPE) {             // {{{
        // TODO
        var radioinput_help = '<p>请直接点击选择</p>';
        // tooltip
        $(".radio_input").qtip({
            id: 'radioinput',
            prerender: false,
            content: {
                text: radioinput_help
            },
            position: {
                my: 'bottom left',
                at: 'top right'
            },
            show: {
                event: 'mouseenter'
            },
            hide: {
                event: 'mouseleave unfocus'
            }
        });
        // validate
        $(".radio_input").on('validate', null, function() {
            if ($(".radio_input input:radio:checked").length != 1) {
                var qtip_content = '<p>请选择化验结果</p>';
                $(this).qtip('api').set('content.text',
                    qtip_content);
                $(this).qtip('api').show();
            }
            else {
                // valid
                var qtip_content = radioinput_help;
                $(this).qtip('api').set('content.text',
                    qtip_content);
                $(this).qtip('api').hide();
                // update data
                record_data.value = $(".radio_input input:radio:checked").val();
            }
        });
    } // }}}
    else {
        // unknown
        return false;
    }

    // validate reason {{{
    var reasoninput_help = '<p>解释修改记录的原因</p><p><strong>必填</strong></p>';
    $(".reason_input").qtip({
        id: 'reasoninput',
        prerender: false,
        content: {
            text: reasoninput_help
        },
        position: {
            my: 'bottom left',
            at: 'top right'
        },
        show: {
            event: 'mouseenter'
        },
        hide: {
            event: 'mouseleave unfocus'
        }
    });
    $(".reason_input").focus(function() {
        $(this).removeClass("valid invalid");
        // show help tooltip
        var qtip_content = dateinput_help;
        $(this).qtip('api').set('content.text', qtip_content);
        $(this).qtip('api').show();
    });
    $(".reason_input").on('validate', null, function() {
        var reason_str = $(this).val();
        if (is_str_blank(reason_str)) {
            // reason not given or blank
            $(this).removeClass("valid");
            $(this).addClass("invalid");
            var qtip_content = '<p>未输入内容,或为空白内容</p><p>请重新输入</p>';
            $(this).qtip('api').set('content.text', qtip_content);
            $(this).qtip('api').show();
        }
        else {
            // valid
            $(this).removeClass("invalid");
            $(this).addClass("valid");
            var qtip_content = reasoninput_help;
            $(this).qtip('api').set('content.text', qtip_content);
            $(this).qtip('api').hide();
            // update data
            record_data.reason = reason_str;
        }
    });
    $(".reason_input").on('blur', null, function() {
        $(this).trigger('validate');
    });
    // }}}
    // }}}
});

// help functions
// check if a string is empty, null or undefined
function is_str_empty(str) {
    return (!str || 0 === str.length);
}

// check if a string is blank, null or undefined
function is_str_blank(str) {
    return (!str || /^\s*$/.test(str));
}

// check if a string is float number                        // {{{
function is_float(str) {
    // regex for fixed notation float number
    var fix_regex = /^([+-]?)(\d+|\d+(\.\d*)?|\d*\.\d+)$/;
    // regex for exponential notation float number
    var exp_regex = /^([+-]?)(\d+|\d+(\.\d*)?|\d*\.\d+)[eE]([+-]?)\d+$/;;

    str = str || "";
    var str_orig = str;
    //console.log('str_orig: "'+str_orig+'"');
    // remove the blank on the head and tail
    str = str.replace(/(^\s*|\s*$)/g, '');
    // remove blank between sign and number
    str = str.replace(/^([+-]?)\s*/, '$1');
    //console.log('str: "'+str+'"');
    // valid str can only contains '[\d.]', '[+-]?', and '[eE]?'
    if (fix_regex.test(str) || exp_regex.test(str)) {
        // true
        return parseFloat(str);
    }
    else {
        return false;
    }
}
// }}}

// check if a string is valid range
// a) '< num'; b) '> num'; c) 'low ~ high' (range_symbol)   // {{{
function is_range(str) {
    if (typeof range_symbol === 'undefined') {
        range_symbol = '~';
    }
    str = str || "";
    var str_orig = str;

    str = str.replace(/(^\s*|\s*$)/g, '');
    var operator = str.charAt(0);
    if (operator === '<' || operator === '>') {
        // strip the first char
        str = str.replace(/^./, '');
        var value = is_float(str);
        if (value === false) {
            return false;
        }
        else {
            return {'operator': operator, 'value': value};
        }
    }
    else {
        // not case 'a)' & 'b)'
        var str_splited = str.split(range_symbol);
        if (str_splited.length == 2) {
            var val_min = is_float(str_splited[0]);
            var val_max = is_float(str_splited[1]);
            if (val_min !== false && val_max !== false
                    && val_min < val_max) {
                // valid
                operator = range_symbol;
                return {'operator': operator,
                        'val_min': val_min,
                        'val_max': val_max };
            }
            else {
                return false;
            }
        }
        else {
            // invalid
            return false
        }
    }
}
// }}}

// vim: set ts=4 sw=4 tw=0 fenc=utf-8 ft=javascript: //