aboutsummaryrefslogtreecommitdiffstats
path: root/97suifangqa/apps/recommend/static/javascripts/add_edit_blog_info.js
blob: 1d994e530fb396d3909f4f84e1cf5edb931ca535 (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
//
// js for 'add_edit_blog_info' page
//
// 2013/10/08
//

// a list contains all the configs data objects
var research_configs_list = new Array();

$(document).ready(function() {
    // make a configs list from 'research_configs {{{
    var obj_keys = Object.keys(research_configs);
    for (var i=0; i<obj_keys.length; i++) {
        var key = obj_keys[i];
        var configs = research_configs[key].configs;
        research_configs_list = research_configs_list.concat(configs);
    }
    // }}}

    // categories buttons {{{
    var cate_btns_html = '';
    for (var i=1; i<=rind_num; i++) {
        var btn_id = 'btn_cate_'+i;
        var btn_value = i+'个指标';
        cate_btns_html += '<input type="button" class="unselected" id="'+btn_id + '" value="'+btn_value + '" /> &ensp; ';
    };
    $("#cate_btns").html(cate_btns_html);
    // button actions
    $('#cate_btns input[type="button"]').on("click", document, function() {
        // unselect all buttons
        //console.log(this);
        $('#cate_btns input[type="button"]').removeClass("selected");
        $('#cate_btns input[type="button"]').addClass("unselected");
        $(this).removeClass("unselected");
        $(this).addClass("selected");
        // unselect buttons of combinations
        $('#comb_divs input[type="button"]').removeClass("selected");
        $('#comb_divs input[type="button"]').addClass("unselected");
        // hide configs div's
        $('#conf_divs .conf_comb').hide();
        // display category of combinations
        var cate_id = $(this).attr('id').replace('btn_cate_', '');
        $('#comb_divs .comb').hide();
        $('#div_comb_'+cate_id).show();
    });
    // }}}

    // indicator combinations div's and buttons {{{
    var comb_divs_html = '';
    for (var i=1; i<=rind_num; i++) {
        var id = 'div_comb_'+i;
        comb_divs_html += '<div class="comb" id="'+id + '" style="display: none;">\n';
        // combinations buttons
        var btn_comb_html = '';
        var combs = rind_categories['N'+i];
        for (var j=0; j<combs.length; j++) {
            var btn_id = combs[j].tag;
            // get value for button
            var btn_value = get_comb_btn_value(combs[j].data);
            btn_comb_html += '<input type="button" class="unselected" id="'+btn_id + '" value="'+btn_value + '" /> &ensp; ';
        };
        comb_divs_html += btn_comb_html + '\n</div>\n';
    };
    $("#comb_divs").html(comb_divs_html);
    // button actions
    $('#comb_divs input[type="button"]').on("click", document, function() {
        // unselect all buttons
        $('#comb_divs input[type="button"]').removeClass("selected");
        $('#comb_divs input[type="button"]').addClass("unselected");
        $(this).removeClass("unselected");
        $(this).addClass("selected");
        // display configs of the combination
        var comb_id = $(this).attr('id');
        $('#conf_divs .conf_comb').hide();
        $('#div_conf_'+comb_id).show();
    });
    // }}}

    // config div's & info input {{{
    var conf_divs_html = '';
    for (var i=0; i<rind_combs.length; i++) {
        var comb = rind_combs[i];
        var id = 'div_conf_'+comb.tag;
        conf_divs_html += '<div class="conf_comb" id="'+id + '" style="display: none;">\n';
        // configs input
        var conf_input_html = '<table class="conf">\n';
        // table head
        conf_input_html += '<thead style="display: none;">\n<tr class="head">';
        conf_input_html += '<th class="name"></th> <th class="response"></th> <th class="weight"></th>';
        conf_input_html += '</tr></thead>\n';
        // table body
        conf_input_html += '<tbody>\n';
        var configs = research_configs[comb.tag].configs;
        //console.log(configs);
        for (var j=0; j<configs.length; j++) {
            // odd or even (for table tr style)
            if (j%2 == 0) {
                var odd_even = 'odd';
            }
            else {
                var odd_even = 'even';
            }
            var conf = configs[j];
            var conf_id = conf.tag;
            var conf_tr_html = '<tr class="conf '+odd_even + '" id="'+conf_id + '">';
            // display name column
            conf_tr_html += '<td class="name">' + conf.display + '</td>';
            // treat response column (prompt & select input)
            conf_tr_html += '<td class="response">' + treat_responses_objs.name + ': <select class="treat_response"></select></td>';
            // weight column
            conf_tr_html += '<td class="weight">权重: <input type="text" class="weight" value="" /></td>';
            //
            conf_tr_html += '</tr>\n';
            conf_input_html += conf_tr_html;
        };
        conf_input_html += '</tbody>\n</table>';
        //console.log(conf_input_html);
        conf_divs_html += conf_input_html + '\n</div>\n';
    };
    $("#conf_divs").html(conf_divs_html);
    // }}}

    // treat response select {{{
    $("select.treat_response").each(function() {
        // add options for 'select'
        var select_html = '';
        // add empty value
        select_html += '<option value="" selected="selected">----</option>\n';
        for (var i=0; i<treat_responses_list.length; i++) {
            var tr = treat_responses_list[i];
            select_html += '<option class="response tr'+tr.id+'" value="id'+tr.id + '">' + tr.name + '</option>\n';
        }
        $(this).html(select_html);
    });
    // }}}

    // validate weight {{{
    $("input.weight").on("validate", null, function(e) {
        e.stopPropagation();
        var value = $(this).val();
        var number = parseFloat(value);
        if (value == "") {
            $(this).removeClass("valid invalid");
        }
        else if (isNaN(number)) {
            $(this).removeClass("valid");
            $(this).addClass("invalid");
        }
        else if (number<0.0 || number>10.0) {
            $(this).removeClass("valid");
            $(this).addClass("invalid");
        }
        else {
            $(this).removeClass("invalid");
            $(this).addClass("valid");
        }
    });
    $("input.weight").on("change", null, function() {
        $(this).trigger("validate");
    });
    // }}}

    // validate tr conf {{{
    // if only one of the 'treat_response' or 'weight' has data
    // then 'invalid'
    $("tr.conf").on("validate", null, function(e) {
        e.stopPropagation();
        var weight_jq = $(this).find("input.weight");
        var response_jq = $(this).find("select.treat_response");
        //console.log(weight_jq);
        // NOTES:
        // only trigger 'validate' of '.weight' element, when
        // the event originated on any element apart from '.weight'
        // REF: http://stackoverflow.com/questions/5967923/jquery-trigger-click-gives-too-much-recursion
        if (! $(e.target).is(".weight")) {
            weight_jq.trigger("validate");
        }
        if (weight_jq.hasClass("invalid")) {
            $(this).addClass("invalid");
        }
        // only one of the 'treat_response' or 'weight' has data
        if (weight_jq.val() !== '' && response_jq.val() === '') {
            $(this).addClass("invalid");
        }
        else if (weight_jq.val() === '' && response_jq.val() !== '') {
            $(this).addClass("invalid");
        }
    });
    // }}}

    // fill provided configs data {{{
    $("tr.conf").each(function() {
        var conf_id = $(this).attr('id');
        var conf_obj = get_config_obj(conf_id);
        if (conf_obj.hasOwnProperty('id')) {
            // this config already in database
            var tr_id = conf_obj.treatResponse_id;
            $(this).find('select>option.tr'+tr_id).prop('selected', true);
            $(this).find('.weight').val(conf_obj.weight);
            // validate tr config
            $(this).trigger('validate');
        }
    });
    // }}}


    // back_to_list button {{{
    $("#back_to_list").bind("click", function() {
        var msg = '注意:当前页面未保存的信息将会丢失。是否继续?';
        var ans = confirm(msg);
        if (ans) {
            window.location.href = recommend_index_url;
        }
        else {
            return false;
        }
    });
    // }}}

    // submit info button {{{
    $("#submit_info").on("click", null, function() {
        // validate tr conf data
        $("tr.conf").trigger("validate");
        if ($("tr.conf.invalid").length) {
            alert('存在有错误数据的行,请更正后再提交');
            return false;
        }
        // collect conf data
        var configs_list = new Array();
        $("tr.conf").each(function() {
            var conf_id = $(this).attr('id');
            var conf_obj = get_config_obj(conf_id);
            // get config data
            var weight_jq = $(this).find("input.weight");
            var response_jq = $(this).find("select.treat_response");
            var weight = weight_jq.val();
            var tr_id = response_jq.val().replace('id', '');
            //
            if (conf_obj.hasOwnProperty('id')) {
                if (weight === '' && tr_id === '') {
                    // delete config
                    conf_obj['action'] = 'delete';
                    conf_obj['weight'] = null;
                    conf_obj['treatResponse_id'] = null;
                }
                else {
                    // edit config
                    conf_obj['action'] = 'edit';
                    conf_obj['weight'] = parseFloat(weight);
                    conf_obj['treatResponse_id'] = parseInt(tr_id);
                }
            }
            else {
                if (weight !== '' && tr_id !== '') {
                    // add config
                    conf_obj['action'] = 'add';
                    conf_obj['weight'] = parseFloat(weight);
                    conf_obj['treatResponse_id'] = parseInt(tr_id);
                }
                else {
                    // null config
                    conf_obj['action'] = null;
                    conf_obj['weight'] = null;
                    conf_obj['treatResponse_id'] = null;
                }
            }
            // push config data
            configs_list.push(conf_obj);
        });
        //console.log(configs_list);

        // ajax post configs data {{{
        var time = moment().valueOf();
        $.ajax({
            type: 'post',
            url: recommend_url + 'ajax/add_edit_configs/',
            data: {
                csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value,
                configs_list: JSON.stringify(configs_list),
                blog_id: blog_id,
                time: time
            },
            dataType: 'json',
            success: function(dataJson) {
                if (dataJson.failed == true) {
                    // submit failed
                    alert('Error: submit failed');
                    return false;
                }
                else {
                    alert('submit successful');
                    // reload page (do not use cache)
                    location.reload(true);
                }
            }
        });
        // }}}
    });
    // }}}
});


// generate value for combination button
// sort id list by magnitude
function get_comb_btn_value(rid_list) {
    // sort id list numerically (smallest first)
    rid_list.sort(function(a, b) { return a-b });
    var value = '';
    for (var i=0; i<rid_list.length; i++) {
        var key = 'id'+rid_list[i];
        value += rind_objs[key].indicator_name + ' | ';
    }
    value = value.replace(/\s*\|\s*$/, '');
    return value;
};

// return the config js obj (copy)
// by searching the given 'tag' in 'research_configs_list'
function get_config_obj(tag) {
    var result = $.grep(research_configs_list,
            function(e) { return e.tag == tag });
    if (result.length == 1) {
        return $.extend({}, result[0]);
    }
    else {
        return null;
    }
};

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