3 function url(endpoint){
4 return "https://" + app.settings.opts.user + ":" + app.settings.opts.pass + "@" + app.settings. opts.host + "/api/" + endpoint + "?callback=?";
10 function MessageList(sel_elm){
11 this.elm = $(sel_elm);
15 MessageList.prototype.update = function(clb){
16 $.getJSON(url("direct_messages"), {getText:"plain"}, function(data, status){
17 //console.log(status);
22 if (senders.indexOf(msg.sender_screen_name)<0){
23 senders.push(msg.sender_screen_name);
25 out += "<aside class='pack'><img src='"+ msg.sender.profile_image_url + "'></aside>";
26 out += "<a href='#message/"+msg.sender_screen_name+"'>";
27 out += "<p>" + msg.text + "</p>";
28 out += "<p>" + msg.sender_screen_name + " - " + msg.created_at + "</p>";
43 * Conversation with a user
45 function Messages(sel_elm){
46 this.elm = $(sel_elm);
50 Messages.prototype.update = function(username, clb) {
52 this.username = username;
54 username = this.username;
57 if (!username) return;
59 $("#contact_screen_name").html(username);
61 $.getJSON(url("direct_messages/all"), {getText:"html", screen_name: username}, function(data, status){
62 //console.log(data, status);
66 var reply_id; // id of the message to reply to. should be the last.
70 pack = (msg.sender_screen_name==username ? "pack": "pack-end");
71 out += "<aside class='"+pack+"'><img src='"+ msg.sender.profile_image_url + "'></aside>";
72 //out += "<a href='#message/"+msg.sender_screen_name+"'>";
73 out += "<p>" + msg.text + "</p>";
74 out += "<p>" + msg.sender_screen_name + " - " + msg.created_at + "</p>";
81 $(".message_text").val("");
82 $(".message_to").val(username);
83 $(".message_reply").val(reply_id);
94 function Compose(sel_elm){
95 this.elm = $(sel_elm);
96 this.contact_photo = $(sel_elm).find(".contact_photo");
97 this.contact_photo_empty = this.contact_photo.attr("src");
102 this.elm.find(".message_to").on('input', function(e){
103 var f=$(e.target).val();
104 if (this.friends[f] !== undefined) {
105 this.contact_photo.attr("src",this.friends[f].profile_image_url);
110 Compose.prototype.update = function(clb){
111 if (this.last_update > (new Date())-60000) {
112 if (clb) clb(this.friends)
115 var dl = this.elm.find("#contacts");
116 $.getJSON(url("statuses/friends"), {}, function(data, status) {
120 var friend = data[k];
121 if (friend.network == "dfrn")
122 this.friends[friend.screen_name] = friend;
123 out += "<option value='"+friend.screen_name+"'>"+friend.name+" ("+friend.screen_name+")</option>";
125 this.last_update = (new Date()) - 0;
132 Compose.prototype.send = function(clb){
134 var screen_name = $(".message_to").val();
135 var replyto = $(".message_reply").val();
136 var text = $(".message_body").val();
138 text = $(".message_text").val();
141 alert("Please enter the message body");
144 if (screen_name=="" || this.friends[screen_name]==undefined){
145 alert("Please enter a valid recipient");
152 Friendica doesn't set CORS headers (it should.. open a feature request!)
153 We can't send post requests via ajax on different domains without it
154 (we can get with jsonp, wich is another hack, but standard...)
155 We create an iframe, append it to body and create a form wich post data to
157 We listen for 'load' event to know when the request returned data, so we
158 can update the message list.
159 We sould remove it when finished.
160 We could read the content of the iframe before removeit as it would be json
165 var f = $("<iframe>").appendTo($("body")).hide();
167 f.on('load', function() {
170 screen_name:screen_name,
176 // we need a small timeout before we can write to iframe content
177 setTimeout( function() {
178 var doc = f[0].contentWindow.document;
180 var form = $("<form>").attr('action', url("direct_messages/new")).attr('method','POST').append(
181 $("<input>").attr('name','screen_name').val(screen_name)
183 $("<input>").attr('name','text').val(text)
185 if (replyto!=="") form.append(
186 $("<input>").attr('name','replyto').val(replyto)
188 $('body',doc).append(form);
197 function Settings(sel_elm) {
198 this.elm = $(sel_elm);
202 this.elm.find(".autoupdate").on("blur", function(e,b){
203 this.set(e.target.id, $(e.target).val());
207 Settings.prototype.update = function(clb) {
208 this.opts = JSON.parse( window.localStorage.getItem("opts") );
209 if (this.opts === null) this.opts={};
211 for (k in this.opts) {
212 this.elm.find("#"+k).val(this.opts[k]);
217 Settings.prototype.is_valid = function() {
218 return this.opts !== null &&
219 this.opts.host !== undefined && this.opts.host !== "" &&
220 this.opts.user !== undefined && this.opts.user !== "" &&
221 this.opts.pass !== undefined && this.opts.pass !=="";
224 Settings.prototype.set = function(name, value) {
225 this.opts[name] = value;
226 window.localStorage.setItem("opts", JSON.stringify(this.opts));
235 // Application Constructor
236 initialize: function() {
237 app.settings = new Settings("#settingsPage");
238 app.messagelist = new MessageList("#messageList");
239 app.messages = new Messages("#messages");
240 app.compose = new Compose("#newMessagePage");
243 '': app.page_messageList,
244 'settings' : app.page_settings,
245 'compose' : app.page_compose,
246 'message/:user': app.page_messageUser
253 page_messageList : function(){
254 if (!app.settings.is_valid()) {
255 /* alert("Please fill required settings"); */
259 app.messagelist.update(function(){
260 if ($("body > .current").length>0) {
261 $("body > .current").removeClass("current").addClass("right");
262 $("#inboxPage").addClass("current");
267 page_messageUser : function(user) {
269 app.messages.update(user, function(){
271 var ofs = $("#messages li:last-of-type").offset()
273 $("#messagePage .scrollable").scrollTop(ofs.top);
275 $("#inboxPage").removeClass("current").addClass("left");
276 $("#messagePage").removeClass("right").addClass("current");
280 page_settings : function() {
281 app.settings.update(function(){
282 $("#inboxPage").removeClass("current").addClass("left");
283 $("#settingsPage").removeClass("right").addClass("current");
287 page_compose : function() {
288 $(".message_body").val("");
289 $(".message_to").val("");
290 $(".message_reply").val("");
291 app.compose.contact_photo.attr("src", app.compose.contact_photo_empty);
293 app.compose.update(function(){
294 $("#inboxPage").removeClass("current").addClass("left");
295 $("#newMessagePage").removeClass("right").addClass("current");
299 bindEvents: function() {
303 $("#reply_send").on("click", function(e){
305 app.compose.send(function(){
306 app.messages.update();
310 /* send new message */
311 $("#message_send").on("click", function(e){
313 app.compose.send(function(data){
314 // dovrebbe andare a message/data.screen_name
315 // ma per come รจ fatto al momento non funziona...
316 // torniamo alla lista messaggi
325 $("progress").hide();
326 $.ajaxSettings.beforeSend = function() { $("progress").show(); }
327 $.ajaxSettings.complete = function() { $("progress").hide(); }