]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/js/ostatus.js
1fc44b21b614d5320bc5c870dfff3d9d1fd26aa9
[quix0rs-gnu-social.git] / plugins / OStatus / js / ostatus.js
1 /*
2  * StatusNet - a distributed open-source microblogging tool
3  * Copyright (C) 2010, StatusNet, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @category  OStatus UI interaction
19  * @package   StatusNet
20  * @author    Sarven Capadisli <csarven@status.net>
21  * @copyright 2010 StatusNet, Inc.
22  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
23  * @link      http://status.net/
24  * @note      Everything in here should eventually migrate over to /js/util.js's SN.
25  */
26
27 SN.C.S.StatusNetInstance = 'StatusNetInstance';
28
29 SN.U.StatusNetInstance = {
30     Set: function(value) {
31         $.cookie(
32             SN.C.S.StatusNetInstance,
33             JSON.stringify(value),
34             {
35                 path: '/',
36                 expires: SN.U.GetFullYear(2029, 0, 1)
37             });
38     },
39
40     Get: function() {
41         var cookieValue = $.cookie(SN.C.S.StatusNetInstance);
42         if (cookieValue !== null) {
43             return JSON.parse(cookieValue);
44         }
45         return null;
46     },
47
48     Delete: function() {
49         $.cookie(SN.C.S.StatusNetInstance, null);
50     }
51 };
52
53 SN.Init.OStatusCookie = function() {
54     if (SN.U.StatusNetInstance.Get() === null) {
55         SN.U.StatusNetInstance.Set({profile: null});
56     }
57 };
58
59 SN.U.DialogBox = {
60     Subscribe: function(a) {
61         var f = a.parent().find('.form_settings');
62         if (f.length > 0) {
63             f.show();
64         }
65         else {
66             $.ajax({
67                 type: 'GET',
68                 dataType: 'xml',
69                 url: a[0].href + ((a[0].href.match(/[\\?]/) === null)?'?':'&') + 'ajax=1',
70                 beforeSend: function(formData) {
71                     a.addClass('processing');
72                 },
73                 error: function (xhr, textStatus, errorThrown) {
74                     alert(errorThrown || textStatus);
75                 },
76                 success: function(data, textStatus, xhr) {
77                     if (typeof($('form', data)[0]) != 'undefined') {
78                         a.after(document._importNode($('form', data)[0], true));
79
80                         var form = a.parent().find('.form_settings');
81
82                         form
83                             .addClass('dialogbox')
84                             .append('<button class="close">&#215;</button>');
85
86                         form
87                             .find('.submit')
88                                 .addClass('submit_dialogbox')
89                                 .removeClass('submit')
90                                 .bind('click', function() {
91                                     form.addClass('processing');
92                                 });
93
94                         form.find('button.close').click(function(){
95                             form.hide();
96
97                             return false;
98                         });
99
100                         form.find('#profile').focus();
101
102                         if (form.attr('id') == 'form_ostatus_connect') {
103                             SN.Init.OStatusCookie();
104                             form.find('#profile').val(SN.U.StatusNetInstance.Get().profile);
105
106                             form.find("[type=submit]").bind('click', function() {
107                                 SN.U.StatusNetInstance.Set({profile: form.find('#profile').val()});
108                                 return true;
109                             });
110                         }
111                     }
112
113                     a.removeClass('processing');
114                 }
115             });
116         }
117     }
118 };
119
120 SN.Init.Subscribe = function() {
121     $('.entity_subscribe .entity_remote_subscribe').live('click', function() { SN.U.DialogBox.Subscribe($(this)); return false; });
122 };
123
124 $(document).ready(function() {
125     SN.Init.Subscribe();
126 });