aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/square.js
blob: e5a75f52f7dec61798f39938564d54eea18bd1aa (plain) (blame)
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
var square = new function(){
    var that = this;
    var j_index_page;
    var j_catelist;
    var j_indexlist;

    var square_node = new vus.node('square'); 
    var user_node = new vus.node('user');
    var index_node = new vus.node('index');

    function box_set(j_box,id,logo,title,start_time,end_time,intro,active){
        var j_oper;

        j_box.attr('boxid',id);

        j_box.find('div.logo').css('background-image','url(\'' + logo + '\')');
        j_box.find('h5.title').text(title);
        j_box.find('p.intro').text(intro);

        if(start_time != null){
            j_box.find('div.start').text('┌─' + com.get_timestring(start_time));
        }
        if(end_time != null){
            j_box.find('div.end').text('└→' + com.get_timestring(end_time));
        }

        j_oper = j_box.find('div.oper');
        j_oper.empty();
        if(active == null){
            j_oper.append('<button class="btn btn-primary join" data-loading-text="處理中">加入</button><a class="btn open">開啓</a>');
            j_oper.find('button.join').on('click',function(e){
                $(this).button('loading');

                com.call_backend('core/square/','join_square',function(result){
                    var data = result.data;

                    if(com.is_callerr(result)){
                        if(data == 'Ereject'){
                            index.add_alert('alert-error','拒絕','加入請求被拒絕');
                        }else if(data == 'Eno_such_sqid'){
                            index.add_alert('alert-error','錯誤','方塊不存在');
                        }else{
                            index.add_alert('alert-error','錯誤','操作方塊發生錯誤');
                        }
                    }else{
                        box_update(id,logo,title,start_time,end_time,intro,data.active);
                    }
                },id); 
            });
        }else{
            if(active== true){
                j_oper.append('<button class="btn btn-success quit" data-loading-text="處理中">退出</button><a class="btn open">開啓</a>');
            }else{
                j_oper.append('<button class="btn btn-warning quit" data-loading-text="處理中">取消申請</button><a class="btn open">開啓</a>');
            }

            j_oper.find('button.quit').on('click',function(e){
                $(this).button('loading');

                com.call_backend('core/square/','quit_square',function(result){
                    var data = result.data;

                    if(com.is_callerr(result)){
                        if(data == 'Eno_such_sqid'){
                            index.add_alert('alert-error','錯誤','方塊不存在');
                        }else{
                            index.add_alert('alert-error','錯誤','操作方塊發生錯誤');
                        }
                    }else{
                        box_update(id,logo,title,start_time,end_time,intro,null);
                    }
                },id); 
            });
        }

        j_oper.find('a.open').attr('href','/toj/sq/' + id + '/');
    }
    function box_update(id,logo,title,start_time,end_time,intro,active){
        var i;
        var boxs;

        boxs = j_indexlist.find('[boxid="' + id + '"]');
        for(i = 0;i < boxs.length;i++){
            box_set($(boxs[i]),id,logo,title,start_time,end_time,intro,active);
        }
    }
    function box_create(id,logo,title,start_time,end_time,intro,active){
        var j_box = $('<div class="span5 box"><div class="logo"></div><h5 class="title"></h5><div class="time start"></div><div class="time end"></div><p class="intro"></p><div class="btn-group oper"></div></div>');

        box_set(j_box,id,logo,title,start_time,end_time,intro,active);

        return j_box;
    }
    function catebox_set(j_box,cateid,catename){
        j_box.attr('cateid',cateid);
        j_box.find('h3.catename').text(catename);
    }
    function catebox_create(cateid,catename){
        var j_box = $('<div class="catebox"><h3 class="catename"></h3><h4 class="run">進行中</h4><div class="clearfix boxlist run"></div><h4 class="pend">等待中</h4><div class="clearfix boxlist pend"></div><h4 class="past">已結束</h4><div class="clearfix boxlist past"></div>');

        catebox_set(j_box,cateid,catename);

        return j_box;
    }   

    function catelist_update(){
        var defer = $.Deferred();

        com.call_backend('core/square/','list_category',function(result){
            var data = result.data;        

            if(com.is_callerr(result)){
                index.add_alert('','警告','方塊目錄發生錯誤');
                defer.reject();
            }else{
                defer.resolve(data);
            }
        });

        return defer.promise();
    }
    function indexlist_update(catelist,joined){
        var i; 
        var j_catebox;
        var cateo;
        
        j_catelist.empty();
        j_indexlist.empty();
        for(i = 0;i < catelist.length;i++){
            cateo = catelist[i];

            com.call_backend('core/square/','list_square',function(cateo){return function(result){
                var i;
                var data = result.data;
                var show_list;
                var sqo;
                var logo;
                var start_time;
                var end_time;
                var j_cate;
                var j_box;
                var j_run;
                var j_pend;
                var j_past;
                var j_a;

                if(com.is_callerr(result)){
                    index.add_alert('','警告','方塊目錄發生錯誤');
                }else{
                    show_list = new Array();
                    for(i = 0;i < data.length;i++){
                        sqo = data[i];
                        if((joined == false || sqo.active != null) && sqo.hidden == false){
                            show_list.push(sqo);
                        }
                    }
                    if(show_list.length == 0){
                        return;
                    }

                    j_cate = $('<li><a href=""></a></li>');
                    j_a = j_cate.find('a');
                    j_a.text(cateo.catename);
                    j_a.on('click',function(e){
                        $(window).scrollTop(j_indexlist.find('[cateid="' + cateo.cateid + '"]').offset().top - 66);
                        return false;
                    });
                    j_catelist.append(j_cate);

                    j_catebox = catebox_create(cateo.cateid,cateo.catename);       
                    j_indexlist.append(j_catebox);

                    j_run = j_catebox.find('div.run');
                    j_pend = j_catebox.find('div.pend');
                    j_past = j_catebox.find('div.past');

                    for(i = 0;i < show_list.length;i++){
                        sqo = show_list[i];
                        if((joined == true && sqo.active == null) || sqo.hidden == true){
                            continue;
                        }

                        if((logo = sqo.logo) == ''){
                            logo = com.get_defaultimg(sqo.sqid);
                        }
                        
                        j_box = box_create(sqo.sqid,logo,sqo.title,sqo.start_time,sqo.end_time,sqo.intro,sqo.active);

                        if(sqo.status == 1){
                            j_pend.append(j_box);
                        }else if(sqo.status == 2){
                            j_run.append(j_box);
                        }else if(sqo.status == 3){
                            j_past.append(j_box);
                        }
                    } 

                    if(j_pend.children().length > 0){
                        j_catebox.find('h4.pend').show();
                        j_pend.show();
                    }
                    if(j_run.children().length > 0){
                        j_catebox.find('h4.run').show();
                        j_run.show();
                    }
                    if(j_past.children().length > 0){
                        j_catebox.find('h4.past').show();
                        j_past.show();
                    }
                }
            }}(cateo),cateo.cateid);
        }
    }
    function update(joined){
        catelist_update().done(function(catelist){
            indexlist_update(catelist,joined);
        });
    }

    that.ready = function(){
        var user_tabnav;
        var index_tabnav;

        j_index_page = $('#index_page');

        square_node.url_chg = function(direct,url_upart,url_dpart,param){
            if(direct == 'in'){
                index.set_menu('方塊');
                index.set_title('');
                index.clear_tabnav();

                if(user.uid != null){
                    square_node.child_delayset('user');
                }
                square_node.child_delayset('index');

                com.loadpage('/toj/html/square.html','/toj/css/square.css').done(function(){
                    j_catelist = j_index_page.find('ul.catelist');
                    j_indexlist = j_index_page.find('div.indexlist');

                    if(user.uid != null){
                        user_tabnav = index.add_tabnav('已加入','/toj/square/user/');
                        square_node.child_set(user_node);
                    }
                    index_tabnav = index.add_tabnav('目錄','/toj/square/index/');
                    square_node.child_set(index_node);
                });

                if(url_dpart.length == 0){
                    if(user.uid == null){
                        com.url_update('/toj/square/index/');
                    }else{
                        com.url_update('/toj/square/user/');
                    }
                }
            }else if(direct == 'out'){
                square_node.child_del(user_node);
                square_node.child_del(index_node);
            }

            return 'cont';
        };
        com.vus_root.child_set(square_node);

        user_node.url_chg = function(direct,url_upart,url_dpart,param){
            if(direct == 'in'){
                user_tabnav.active();

                update(true);
            } 

            return 'cont';
        }
        
        index_node.url_chg = function(direct,url_upart,url_dpart,param){
            if(direct == 'in'){
                index_tabnav.active();

                update(false);
            } 

            return 'cont';
        }
    };
};