aboutsummaryrefslogtreecommitdiffstats
path: root/97suifangqa/apps/indicator/static/javascripts/new_delete_index.js
blob: e216f09c4269ffba3b8996819d696ec614c010af (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
// track the indexes already added(/followed)
var added_indexes_id = new Array();

var select_letter = '';
var select_index_obj = null;

$(document).ready(function(){
    $(".index_type").each(function(){
        if($(this).hasClass("selected")){
            select_index_obj = $(this);
            return false;
        }
    });
    $(".index_type").hover(
        function(){
            $(this).addClass("selected");
        },
        function(){
            if(!$(this).is(select_index_obj)){
                $(this).removeClass("selected");
            }
        }
    );

    // login control kit {{{
    $(".drop-down-area").bind("click", function(){
        var drop_down_menu = $(".drop-down-menu");
        if(drop_down_menu.hasClass("open")){
            drop_down_menu.removeClass("open");
        }else{
            drop_down_menu.addClass("open");
        }
        return false;
    });
    $("body").bind("click", function(){
        $(".drop-down-menu").removeClass("open");
    });
    // login }}}

    // search -> ajax
    // bind enter key
    $(".search #search_kw").on('keypress', null, function(e) {
        var keycode = (e.keyCode ? e.keyCode : e.which);
        //console.log('keycode: "'+keycode+'"');
        if (keycode == 13) {
            //alert('"Enter" key pressed');
            var kw = $(this).val();
            //console.log('kw: "'+kw+'"');
            search_indicators(kw);
            return false;
        }
    });
    $(".search #search_btn").bind("click", function(){
        var kw = $("#search_kw").val();
        //console.log('kw: "'+kw+'"');
        //window.location.href = '?kw='+kw;
        search_indicators(kw);
        return false;
    });

    // XXX: cannot deal with the dynamically added div's by ajax
    /*
    $(".left>.index_line, .index_letter_container>.index_lines>.index_line").each(function(){
        classHover($(this), "add");
    });
    $(".right>.index_line").each(function(){
        classHover($(this), "minus");
    });
    */
    // updated to work with ajax
    $(".show_indexes").on('mouseenter mouseleave', '.index_lines>.index_line', function(e) {
        //console.log("event_type: "+e.type);
        var line = $(this);
        var lines_div = line.parent();
        if (lines_div.hasClass("to_add")) {
            // left container (unfollowed)
            var cls_name = "add";
        }
        else {
            // right container (followed)
            var cls_name = "minus";
        }
        // event type
        if (e.type === 'mouseenter') {
            $(this).addClass(cls_name);
        }
        else {
            $(this).removeClass(cls_name);
        }
    });

    // save the "index_id's of added (type: string)
    $(".right>.index_line").each(function(){
        var index_id = $(this).attr("index_id");
        added_indexes_id.push(index_id);
    });
    $(".show_indexes").on("click", ".add>.icon", function(){
        var add_icon = $(this);
        var index_id = add_icon.closest(".index_line").attr("index_id");
        var date = new Date();
        var time = date.getTime();
        $.ajax({
            type: 'get',
            url: indicator_url + 'ajax/act_index/',
            data: 'index_id='+index_id + '&act=add' + '&time='+time,
            success: function(data){
                if(data == 'success'){
                    // check if the index exists?
                    if (added_indexes_id.indexOf(index_id) == -1) {
                        var obj = add_icon.parent();
                        var objClone = obj.clone();
                        objClone.removeClass("add")
                        objClone.children(".index_category").remove();
                        $(".right").append(objClone);
                        classHover(objClone, "minus");
                        added_indexes_id.push(index_id);
                    }
                }
            }
        });

        return false;
    });
    $(".show_indexes").on("click", ".minus>.icon", function(){
        var minus_icon = $(this);
        var index_id = minus_icon.closest(".index_line").attr("index_id");
        var date = new Date();
        var time = date.getTime();
        $.ajax({
            type: 'get',
            url: indicator_url + 'ajax/act_index/',
            data: 'index_id='+index_id + '&act=minus' + '&time='+time,
            success: function(data){
                var obj = minus_icon.parent();
                obj.remove();
                rm_index = added_indexes_id.indexOf(index_id);
                added_indexes_id.splice(rm_index, 1);
            }
        });

        return false;
    });
    $(".all_condition>.index_all_letter>div").bind("click", function(){
        $(".letter_selected").removeClass("letter_selected");
        $(this).addClass("letter_selected");
        var container = $(".index_letter_container");
        var letterClass = $(this).text();
        var scrollTo = $("."+letterClass);
        container.scrollTop(scrollTo.offset().top - container.offset().top + container.scrollTop());
        select_letter = letterClass;
        return false;
    });
    $(".all_condition>.index_all_letter>div").hover(
        function(){
            $(this).addClass("letter_selected");
        },
        function(){
            if(select_letter != $(this).text()){
                $(this).removeClass("letter_selected");
            }
        }
    );
    $("#submitIndexBtn").bind("click", function(){
        var commit_index = '';
        $(".right>.index_line").each(function(){
            commit_index += $(this).attr("index_id")+',';
        });
        $("#commit_index").val(commit_index);
        $("#index_form").submit();
        return false;
    });
});


// helper functions
function classHover(obj, c){
    obj.hover(
        function(){
            $(this).addClass(c);
        },
        function(){
            $(this).removeClass(c);
        }
    );
}

// search
function search_indicators(kw) {
    if (is_str_blank(kw)) {
        // 'kw' blank
        return false;
    }
    var time = moment().valueOf();
    $.ajax({
        type: 'get',
        url: indicator_url + 'ajax/search_indicators/',
        data: 'kw='+kw + '&time='+time,
        dataType: 'json',
        success: function(dataJson) {
            // unselect index_type
            $(".index_navigation .selected").removeClass("selected");
            select_index_obj = null;
            // hide & show
            $(".all_condition").hide();
            $(".category_condition").hide();
            $(".search_condition").show();
            // clear existing search div's or create
            if ($(".search_condition > .index_lines").length) {
                $(".search_condition > .index_lines").html('');
            }
            else {
                // add 'index_lines' div
                $(".search_condition").append('<div class="index_lines left to_add"></div>');
            }
            if (dataJson.failed === true) {
                if (dataJson.error_code === 10) {
                    // search keyword blank
                    $(".search_condition > .index_lines").append('<div class="index_search_error"> <div class="index_error">您未输入搜索关键词</div> <div class="icon"></div> <div style="clear:both"></div> </div>');
                }
                if (dataJson.error_code === 20) {
                    // search result empty
                    $(".search_condition > .index_lines").append('<div class="index_search_error"> <div class="index_error">未搜索到符合的结果</div> <div class="icon"></div> <div style="clear:both"></div> </div>');
                }
            }
            else {
                // append search results to page
                var results_html = '';
                for (var i=0; i<dataJson.indicators.length; i++) {
                    var ind = dataJson.indicators[i];
                    results_html += '<div class="index_line" index_id="'+ind.id + '"> <div class="index_name">'+ind.name + '</div> <div class="index_category"><a href="?tab='+ind.categories_id[0] + '">'+ind.categories_name[0] + '</a></div> <div class="icon"></div> <div style="clear:both"></div> </div> \n';
                }
                $(".search_condition > .index_lines").append(results_html);
            }
        }
    });
    return false;
}

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


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