instrumentation: add next-share/
[cs-p2p-next.git] / instrumentation / next-share / BaseLib / WebUI / javascript / tribe.interface.js
1 /*
2  *      Written by Riccardo Petrocco
3  *      see LICENSE.txt for license information
4  * 
5  */
6
7
8 function TribeInterface( controller )
9 {
10         this.initialize( controller );
11         return this;
12 }
13
14 TribeInterface.prototype =
15 {
16         /*
17          * Constructor
18          */
19         initialize: function(controller) {
20                 this._controller = controller;
21                 this._error = '';
22                 this._token = '';
23                 this._report  = true;
24         },
25
26         /*
27          * Error handle
28          */
29         ajaxError: function(request, error_string, exception, ajaxObject) {
30                 var token;
31                 remote = this;
32
33                 remote._error = request.responseText
34                                         ? request.responseText.trim().replace(/(<([^>]+)>)/ig,"")
35                                         : "";
36                 if( !remote._error.length )
37                         remote._error = 'Server not responding';
38                 
39         if (remote._report) {
40                 alert(remote._error);
41                 
42                     this._controller._BPClosed = true;
43                     remote._report = false
44             }
45                     
46         },
47
48         sendRequest: function( data, success, async ) {
49
50                 remote = this;
51                 if( typeof async != 'boolean' )
52                   async = true;
53
54                 var ajaxSettings = {
55                         url: 'webUI',
56                         type: 'GET',
57                         contentType: 'json',
58                         dataType: 'json',
59                         cache: false,
60                         data: $.toJSON(data),
61                         error: function(request, error_string, exception){ remote.ajaxError(request, error_string, exception, ajaxSettings); },
62                         success: success,
63                         async: async
64                 };
65
66                 $.ajax( ajaxSettings );
67         },
68
69     // TODO not used now
70         loadStats: function( callback, async ) {
71                 var tr = this._controller;
72                 var o = { method: 'stats' };
73                 this.sendRequest( o, callback, async );
74         },
75
76         getInitialDataFor: function(dl_ids, callback) {
77                 var o = {
78                         method: "get_all_downloads"
79                 };
80
81                 if(dl_ids)
82                         o.arguments.ids = dl_ids;
83
84         //this.sendRequest( o, function(data){ alert( data.downloads )} );
85                 this.sendRequest( o, function(data){ callback(data.downloads)} );
86         },
87         
88         pauseAll: function() {
89
90         var tribeif = this;
91         
92             var o = {
93                 method: "pause_all"
94             };
95             
96             // Send the request and report a message if some problems occurred
97             this.sendRequest( o, function(data){ if ( !data.success ) alert("Errors occurred while trying to pause the downloads"); } );
98             
99     },
100
101     pauseDownload: function( id ) {
102
103         var tribeif = this;
104         
105             var o = {
106                 method: "pause_dl",
107                 arguments: {"id" : id}
108             };
109             
110             // Send the request and report a message if some problems occurred
111             this.sendRequest( o, function(data){ if ( !data.success ) alert("Errors occurred while trying to pause the download"); } );
112             
113     },
114
115
116         resumeAll: function() {
117
118         var tribeif = this;
119         
120             var o = {
121                 method: "resume_all"
122             };
123             
124             // Send the request and report a message if some problems occurred
125             this.sendRequest( o, function(data){ if ( !data.success ) alert("Errors occurred while trying to resume the downloads"); } );
126             
127     },
128
129
130     resumeDownload: function( id ) {
131
132         var tribeif = this;
133         
134             var o = {
135                 method: "resume_dl",
136                 arguments: {"id" : id}
137             };
138             
139             // Send the request and report a message if some problems occurred
140             this.sendRequest( o, function(data){ if ( !data.success ) alert("Errors occurred while trying to resume the download"); } );
141             
142     },
143
144
145     removeAll: function() {
146
147         var tribeif = this;
148         
149             var o = {
150                 method: "remove_all"
151             };
152             
153             // Send the request and report a message if some problems occurred
154             this.sendRequest( o, function(data){ if ( !data.success ) alert("Errors occurred while trying to remove the downloads"); } );
155             
156     },
157
158     removeDownload: function( id ) {
159
160         var tribeif = this;
161         
162             var o = {
163                 method: "remove_dl",
164                 arguments: {"id" : id}
165             };
166             
167             // Send the request and report a message if some problems occurred
168             this.sendRequest( o, function(data){ if ( !data.success ) alert("Errors occurred while trying to remove the download"); } );
169             
170     }
171
172
173     
174 };
175