Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ojsxc.js « js « build - github.com/nextcloud/jsxc.nextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cae66f5600fd6f323660ca5bf7887961b1f9b4e4 (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
/*!
 * ojsxc v3.0.2+appstore - 2016-12-27
 * 
 * Copyright (c) 2016 Klaus Herberth <klaus@jsxc.org> <br>
 * Released under the MIT license
 * 
 * Please see http://www.jsxc.org/
 * 
 * @author Klaus Herberth <klaus@jsxc.org>
 * @version 3.0.2+appstore
 * @license MIT
 */

/* global jsxc, oc_appswebroots, OC, $, oc_requesttoken, dijit, oc_config */
/* jshint latedef: nofunc */

/**
 * Make room for the roster inside the owncloud template.
 *
 * @param {type} event
 * @param {type} state State in which the roster is
 * @param {type} duration Time the roster needs to move
 * @returns {undefined}
 */
function onRosterToggle(event, state, duration) {
   "use strict";
   var wrapper = $('#content-wrapper');

   var roster_width = (state === 'shown') ? $('#jsxc_roster').outerWidth() : 0;
   var toggle_width = $('#jsxc_toggleRoster').width();

   if ($(window).width() < 768) {
      // Do not resize elements on extra small devices (bootstrap definition)
      return;
   }

   wrapper.animate({
      paddingRight: (roster_width + toggle_width) + 'px'
   }, duration);

   // update webodf
   if (typeof dijit !== 'undefined') {
      $('#mainContainer, #odf-toolbar').animate({
         right: (roster_width + toggle_width) + 'px'
      }, {
         progress: function() {
            dijit.byId("mainContainer").resize();
         }
      });
   }

   // update app sidebar
   if ($('#app-sidebar').length > 0) {
      $('#app-sidebar').animate({
         right: (roster_width + toggle_width) + 'px'
      });
   }
}

/**
 * Init owncloud template for roster.
 *
 * @returns {undefined}
 */
function onRosterReady() {
   "use strict";
   var roster_width, navigation_width, roster_right, toggle_width;

   if (typeof $('#jsxc_roster').outerWidth() !== 'number') {
      setTimeout(onRosterReady, 200);
      return;
   }

   var div = $('<div/>');

   div.addClass('jsxc_chatIcon');
   div.click(function(){
      jsxc.gui.roster.toggle();
   });

   $('#settings').after(div);

   if ($(window).width() < 768) {
      // Do not resize elements on extra small devices (bootstrap definition)
      return;
   }

   getValues();

   $('#content-wrapper').css('paddingRight', roster_width + roster_right + toggle_width);

   // update webodf
   var contentbg = $('#content-wrapper').css('background-color');
   $(window).on('hashchange', function() {
      $('#content-wrapper').css('background-color', contentbg);

      if (window.location.pathname.match(/\/documents\/$/)) {
         var docNo = window.location.hash.replace(/^#/, '');

         if (docNo.match(/[0-9]+/) && typeof dijit !== 'undefined') {
            getValues();

            $('#content-wrapper').css('background-color', $('#mainContainer').css('background-color'));

            $('#mainContainer, #odf-toolbar').css('right', roster_width + roster_right + toggle_width);
            dijit.byId("mainContainer").resize();
         }
      }
   });

   setTimeout(function(){
      // update app sidebar
      if ($('#app-sidebar').length > 0) {
         $('#app-sidebar').css('right', (roster_width + roster_right + toggle_width) + 'px');
      }
   }, 500);

   function getValues() {
      roster_width = $('#jsxc_roster').outerWidth();
      navigation_width = $('#navigation').width();
      roster_right = parseFloat($('#jsxc_roster').css('right'));
      toggle_width = $('#jsxc_toggleRoster').width();
   }
}

// initialization
$(function() {
   "use strict";

   if (location.pathname.substring(location.pathname.lastIndexOf("/") + 1) === 'public.php') {
      // abort on shares
      return;
   }

   if (typeof jsxc === 'undefined') {
      // abort if core or dependencies threw an error
      return;
   }

   $(document).one('ready.roster.jsxc', onRosterReady);
   $(document).on('toggle.roster.jsxc', onRosterToggle);

   $(document).on('connected.jsxc', function() {
      // reset default avatar cache
      jsxc.storage.removeUserItem('defaultAvatars');
   });

   $(document).on('status.contacts.count status.contact.updated', function() {
      if (jsxc.restoreCompleted) {
         setTimeout(function() {
            jsxc.gui.detectEmail($('table#contactlist'));
         }, 500);
      } else {
         $(document).on('restoreCompleted.jsxc', function() {
            jsxc.gui.detectEmail($('table#contactlist'));
         });
      }
   });

   jsxc.init({
      app_name: 'Nextcloud',
      loginForm: {
         form: '#body-login form',
         jid: '#user',
         pass: '#password',
         ifFound: 'force',
         onConnecting: (oc_config.version.match(/^([8-9]|[0-9]{2,})+\./))? 'quiet' : 'dialog'
      },
      logoutElement: $('#logout'),
      rosterAppend: 'body',
      root: oc_appswebroots.ojsxc + '/js/jsxc',
      RTCPeerConfig: {
         url: OC.filePath('ojsxc', 'ajax', 'getTurnCredentials.php')
      },
      displayRosterMinimized: function() {
         return OC.currentUser != null;
      },
      defaultAvatar: function(jid) {
         var cache = jsxc.storage.getUserItem('defaultAvatars') || {};
         var user = Strophe.unescapeNode(jid.replace(/@[^@]+$/, ''));

         $(this).each(function() {

            var $div = $(this).find('.jsxc_avatar');
            var size = $div.width();
            var key = user + '@' + size;

            var handleResponse = function(result) {
               if (typeof (result) === 'object') {
                  if (result.data && result.data.displayname) {
                     $div.imageplaceholder(user, result.data.displayname);
                  } else {
                     $div.imageplaceholder(user);
                  }
               } else {
                  $div.css('backgroundImage', 'url('+result+')');
               }
            };

            if (typeof cache[key] === 'undefined' || cache[key] === null) {
               var url;

               url = OC.generateUrl('/avatar/' + encodeURIComponent(user) + '/' + size + '?requesttoken={requesttoken}', {
                  user: user,
                  size: size,
                  requesttoken: oc_requesttoken
               });

               $.get(url, function(result) {

                  var val = (typeof result === 'object') ? result : url;
                  handleResponse(val);

                  jsxc.storage.updateItem('defaultAvatars', key, val, true);
               });

            } else {
               handleResponse(cache[key]);
            }
         });
      },
      loadSettings: function(username, password, cb) {
         $.ajax({
            type: 'POST',
            url: OC.filePath('ojsxc', 'ajax', 'getSettings.php'),
            data: {
               username: username,
               password: password
            },
            success: function(d) {
               if (d.result === 'success' && d.data && d.data.serverType !== 'internal' && d.data.xmpp.url !== '' && d.data.xmpp.url !== null) {
                  cb(d.data);
               } else if (d.data && d.data.serverType === 'internal') {
                  // fake successful connection
                  jsxc.bid = username + '@' + window.location.host;

                  jsxc.storage.setItem('jid', jsxc.bid + '/internal');
                  jsxc.storage.setItem('sid', 'internal');
                  jsxc.storage.setItem('rid', '123456');

                  jsxc.options.set('xmpp', {
                     url: OC.generateUrl('apps/ojsxc/http-bind')
                  });
                  if (d.data.loginForm) {
                     jsxc.options.set('loginForm', {
                        startMinimized: d.data.loginForm.startMinimized
                     });
                  }

                  cb(false);
               } else {
                  cb(false);
               }
            },
            error: function() {
               jsxc.error('XHR error on getSettings.php');

               cb(false);
            }
         });
      },
      saveSettinsPermanent: function(data, cb) {
         $.ajax({
            type: 'POST',
            url: OC.filePath('ojsxc', 'ajax', 'setUserSettings.php'),
            data: data,
            success: function(data) {
               cb(data.trim() === 'true');
            },
            error: function() {
               cb(false);
            }
         });
      },
      getUsers: function(search, cb) {
         $.ajax({
            type: 'GET',
            url: OC.filePath('ojsxc', 'ajax', 'getUsers.php'),
            data: {
               search: search
            },
            success: cb,
            error: function() {
               jsxc.error('XHR error on getUsers.php');
            }
         });
      },
      viewport: {
         getSize: function() {
            var w = $(window).width() - $('#jsxc_windowListSB').width();
            var h = $(window).height() - $('#header').height() - 10;

            if (jsxc.storage.getUserItem('roster') === 'shown') {
               w -= $('#jsxc_roster').outerWidth(true);
            }

            return {
               width: w,
               height: h
            };
         }
      }
   });

   // Add submit link without chat functionality
   if (jsxc.el_exists(jsxc.options.loginForm.form) && jsxc.el_exists(jsxc.options.loginForm.jid) && jsxc.el_exists(jsxc.options.loginForm.pass)) {

      var link = $('<a/>').text($.t('Log_in_without_chat')).attr('href', '#').click(function() {
         jsxc.submitLoginForm();
      });

      var alt = $('<p id="jsxc_alt"/>').append(link);
      $('#body-login form:eq(0) fieldset').append(alt);
   }
});