source: trunk/wtorrent/home/js/javasc.js @ 51

Revision 51, 9.3 KB checked in by royger, 2 years ago (diff)
  • New xmlrpc factory, 2 classes added:
    • torrent.cls.php
    • multicall.cls.php

Every torrent creates a torrent object, which contains the functions for calling all the necessary methods. Also a transparent cache was added.

  • Cleaned most of the classes of unnecessary php code.
  • Command interface finished, most actions are done through AJAX calls using the Commands.cls.php class. Fixes #16.
  • This build is tested to work with Mozilla Firefox 2.0.0.14 and Safari 3.1.1 (5525.18) using Mac OS X. Same results expected (although not tested) under Windows.
Line 
1function resizeInnerTab(hash) {
2    if($('tab' + hash).innerHTML == "")
3    {
4        load('tab' + hash, 'info');
5    } else {
6        Effect.toggle('ttab' + hash, 'blind', {duration:0.5});
7    }
8    resize();
9}
10/* Resize to make shadedborders render the div again */
11function resize() {
12        Main.render('principal');
13}
14/* Check all elements of the specified class */
15function checkAllByClass(styleClass) {
16        var elements = document.getElementsByClassName(styleClass);
17                for (var i = 0; i < elements.length; i++) {
18                        elements[i].checked = true;
19                }
20}
21/* Uncheck all elements of the specified class */
22function uncheckAllByClass(styleClass) {
23        var elements = document.getElementsByClassName(styleClass);
24                for (var i = 0; i < elements.length; i++) {
25                        elements[i].checked = false;
26                }
27}
28/* Invert selection */
29function invertAllByClass(styleClass) {
30        var elements = document.getElementsByClassName(styleClass);
31                for (var i = 0; i < elements.length; i++) {
32                        elements[i].checked = !elements[i].checked;
33                }
34}
35/* Function that does the AJAX loading process */
36/* Possible FRAMES:
37    'content': main div
38    'tab' + hash: every torrent div (tabs in the left not included)
39    'itab' + hash: div with the name of the torrent, erase, start... buttons
40    'ttab' + hash; div that contents 'tab' + hash and every torrent left tabs
41    'messages': messages frame
42*/
43function load(frame, content) {
44        if(frame == 'content') // Actions for frame 'content' (MAIN FRAME)
45        {
46                var get = getContent(content);
47        }
48        else // Actions for torrent frames
49        {
50                var get = getFrame(content, frame);
51        }
52        ajaxCall(frame, get);
53}
54/* Generate showLoad and showresponse */
55function getShowFunctions(frame)
56{
57        var functions = new Array();
58
59        functions[0] = (function (frame) { return function () { loadingContent(frame); } })(frame);
60        functions[1] = (function (frame) { return function (request) { responseContent(request, frame); } })(frame);
61    //functions[2] = (function (frame) { return function () { resizeFrame(frame); } })(frame);
62       
63        return functions;
64}       
65/* Get URL to change the content of the Top tabs */
66function getContent(id)
67{
68        if(id == 'public') {
69                var pars = 'cls=ListT&tpl=ajax&view=public';
70                view = 'public';
71                tab = 1;
72        }
73        if(id == 'private') {
74                var pars = 'cls=ListT&tpl=ajax&view=private';
75                view = 'private';
76                tab = 1;
77        }
78        return pars;
79}
80/* Get URL to change the content of the torrent tabs */
81function getFrame(content, frame)
82{
83        var cls;
84        switch(content)
85        {
86                case 'info':
87                        cls = 'General';
88                        break;
89                case 'files':
90                        cls = 'Files';
91                        break;
92                case 'trackers':       
93                        cls = 'Tracker';
94                        break;
95                default:
96                        break;
97        }
98        var pars = 'cls=' + cls + '&tpl=details&hash=' + frame.substr(3);
99        return pars;
100}
101/* Load LOADING content in the given frame */
102function loadingContent(frame) {
103    switch(frame)
104    {
105        case 'messages':
106                    $(frame).style.display = "block";
107                    var loadingDiv = loadingCommand;
108            $(frame).innerHTML = loadingDiv;
109            break;
110        case 'content':
111                    var loadingDiv = loading;
112            $(frame).innerHTML = loadingDiv;
113            break;
114        default:
115            loadingTab(frame);
116            break;
117    }
118    //contractFrame(frame);
119}
120function loadingTab(frame)
121{
122    contractFrame('t' + frame);
123    var node1 = document.getElementById('node1');
124    var overlayCell = $('loadingCell').cloneNode(true);
125    var positions = findPos($('i' + frame));
126    overlayCell.setAttribute('id', 'l' + frame);
127    overlayCell.style.left = positions[0] + 'px';
128    overlayCell.style.top = positions[1] + 'px';
129    document.body.appendChild(overlayCell);
130    overlayCell.show();
131    new Effect.Opacity(overlayCell, {duration:0.2, from:0, to:0.6});
132    return;
133}
134/* Load AJAX RESPONSE in given content */
135function responseContent(originalRequest, frame) {
136        var newData = originalRequest.responseText;
137    $(frame).innerHTML = newData;
138    if(frame != "content" && frame != "messages") {
139        var onFinish = (function (frame) { return function (obj) { $(frame).remove(); } })('l'+ frame);
140        new Effect.Opacity('l' + frame, {duration:0.2, from:0.6, to:0, afterFinish: onFinish});
141        window.setTimeout("expandFrame('t" + frame + "')", 100);
142    }
143        postAjax();
144        resize();
145}
146function expandFrame(frame) {
147    new Effect.BlindDown(frame, {duration:0.5});
148}
149function contractFrame(frame, onFinish) {
150    new Effect.BlindUp(frame, {duration:0.5, afterFinish: onFinish});
151}
152/* Process commands given through the ajax interface */
153function command(command, param)
154{
155        /* Default frame for wTorrent messages and loading/showing functions */
156        var frame = 'messages';
157        var call = new Array();
158        switch(command)
159        {
160                case 'files':
161                        var call = files(param);
162                        break;
163                case 'info':
164                        var call = info(param);
165                        break;
166                case 'trackers':
167                        var call = trackers(param);
168                        break;
169                case 'batch':
170                        var call = batch();
171                        break;
172                default:
173                        var call = defaultCall(command, param);
174                        break;
175        }
176        // Execute the command through ajax
177        ajaxCall(frame, call[0]);
178        // Update the page (after 500ms to give rtorrent time to process the command (also set the frame to loading)
179        //loadingContent(call[1]);
180        window.setTimeout("load('" + call[1] + "', '" + call[2] + "')", 100);
181}
182
183function ajaxCall(frame, pars)
184{
185        // Get show functions with correct frames
186        var functions = getShowFunctions(frame);
187        var showLoad = functions[0];
188        var showResponse = functions[1];
189    //var resizeDiv = functions[2];
190        // Dummy variable to avoid iexplorer cache
191        pars = pars + "&dummy=" + new Date().getTime();
192        var myAjax = new Ajax.Request( url, {
193                        method: 'get', 
194                        parameters: pars, 
195                        onLoading: showLoad, 
196                        onComplete: showResponse
197        } );
198}
199function getChecked(identifier)
200{
201        var params = new Array();
202        var objects = document.getElementsByClassName(identifier);
203        for (var i = 0; i < objects.length; i++) {
204                if(objects[i].checked === true)
205                        params.push(objects[i].id);
206        }
207        return params;
208}
209function files(param)
210{
211        var call = new Array();
212        var params = getChecked('files' + param);
213        var param2 = params.join('~');     
214        param2 = encodeURIComponent(param2);
215        var param1 = $('sf' + param).options[$('sf' + param).selectedIndex].value;
216        call[0] = 'cls=commands&tpl=commands&command=files&param=' + param + '&param1=' + param1 + '&param2=' + param2;
217        call[1] = 'tab' + param;
218        call[2] = 'files';
219       
220        return call;
221}
222function info(param)
223{
224        var call = new Array();
225        param1 = $('sp' + param).options[$('sp' + param).selectedIndex].value;
226        call[0] = 'cls=commands&tpl=commands&command=info&param=' + param + '&param1=' + param1;
227        call[1] = 'tab' + param;
228        call[2] = 'info';
229       
230        return call;
231}
232function trackers(param)
233{
234        var call = new Array();
235        var params = getChecked('trackers' + param);
236        var param2 = params.join('~');     
237        param2 = encodeURIComponent(param2);
238        var param1 = $('st' + param).options[$('st' + param).selectedIndex].value;
239        call[0] = 'cls=commands&tpl=commands&command=trackers&param=' + param + '&param1=' + param1 + '&param2=' + param2;
240        call[1] = 'tab' + param;
241        call[2] = 'trackers';
242       
243        return call;
244}
245function batch()
246{
247        var call = new Array();
248        var command;
249        var params = getChecked('torrent');
250        var number = $('actions').options[$('actions').selectedIndex].value;
251       
252        switch(number)
253        {
254                case '0':
255                        command = 'stop';
256                        break;
257                case '1':
258                        command = 'start';
259                        break;
260                case '2':
261                        command = 'erase';
262                        break;
263                case '3':
264                        command = 'chash';
265                        break;
266                case '4':
267                        command = 'close';
268                        break;
269        }
270                       
271        var param = params.join('~');     
272        param = encodeURIComponent(param);
273        if(command == 'erase')
274        {
275                if(!confirm(confirmMsg))
276                return;
277        }
278        call[0] = 'cls=commands&tpl=commands&command=' + command + '&param=' + param;
279        call[1] = 'content';
280        call[2] = view;
281       
282        return call;
283}
284function defaultCall(command, param)
285{
286        var call = new Array();
287        if(command == 'erase')
288        {
289                if(!confirm(confirmMsg))
290                        return;
291        }
292        call[0] = 'cls=commands&tpl=commands&command=' + command + '&param=' + param;
293        call[1] = 'content';
294        call[2] = view;
295       
296        return call;
297}
298/* Find position of an element */
299function findPos(obj) {
300        var curleft = curtop = 0;
301        if (obj.offsetParent) {
302                do {
303                        curleft += obj.offsetLeft;
304                        curtop += obj.offsetTop;
305                } while (obj = obj.offsetParent);
306        }
307        return [curleft,curtop];
308}
309function torrentTip(elementId) {
310    var content = $('tipContent' + elementId).cloneNode(true);
311    content.id = elementId + 'copy';
312    content.show();
313    new Tip(
314        'tip' + elementId,                 // the id of your element
315        content,                 // a string or an element
316        { 
317            closeButton: false,    // or true
318            duration: 0.3,         // duration of the effect, if used
319            delay: 0,             // seconds before tooltip appears
320            effect: false,         // false, 'appear' or 'blind'
321            fixed: false,          // follow the mouse if false
322            hideAfter: false,      // hides after seconds of inactivity, not hovering the element or the tooltip
323            hideOn: 'mouseout',     // any other event, false or: { element: 'element|target|tip|closeButton|.close', event: 'click|mouseover|mousemove' }
324            hook: false,           // { target: 'topLeft|topRight|bottomLeft|bottomRight|topMiddle|bottomMiddle|leftMiddle|rightMiddle',tip: 'topLeft|topRight|bottomLeft|bottomRight|topMiddle|bottomMiddle|leftMiddle|rightMiddle' }
325            showOn: 'mousemove',   // or any other event
326            viewport: false         // keep within viewport, false when fixed or hooked
327        }
328    );
329}
Note: See TracBrowser for help on using the repository browser.