Initial commit
[progetti-gl-como:friendica-messenger.git] / www / js / index.js
1
2
3 function url(endpoint){
4         return "https://" + app.settings.opts.user + ":" + app.settings.opts.pass + "@" + app.settings. opts.host + "/api/" + endpoint + "?callback=?"; 
5 }
6
7 /**
8  * Message List Object
9  */
10 function MessageList(sel_elm){
11         this.elm = $(sel_elm);
12 }
13
14 MessageList.prototype.update = function(clb){
15         $.getJSON(url("direct_messages"), {getText:"plain"}, function(data, status){
16                 //console.log(status);
17                 var senders = [];
18                 var out="";
19                 for (k in data) {
20                         var msg = data[k];
21                         if (senders.indexOf(msg.sender_screen_name)<0){
22                                 senders.push(msg.sender_screen_name);
23                                 out += "<li>";
24                                 out += "<aside class='pack'><img src='"+ msg.sender.profile_image_url + "'></aside>";
25                                 out += "<a href='#message/"+msg.sender_screen_name+"'>";
26                                 out += "<p>" + msg.text + "</p>";
27                                 out += "<p>" + msg.sender_screen_name + " - " + msg.created_at + "</p>";
28                                 out += "</a>";
29                                 out += "</li>";
30                         }
31                 }
32                 
33                 this.elm.html(out);
34                 
35                 if(clb) clb(data);
36                 
37         }.bind(this));
38 }
39
40 function Messages(sel_elm){
41         this.elm = $(sel_elm);
42 }
43
44 Messages.prototype.update = function(username, clb) {
45         $.getJSON(url("direct_messages/all"), {getText:"html", screen_name: username}, function(data, status){
46                 //console.log(data, status);
47                 data.reverse();
48                 var out="";
49                 var pack="";
50                 for (k in data) {
51                         var msg = data[k];
52                         out += "<li>";
53                         pack = (msg.sender_screen_name==username ? "pack": "pack-end"); 
54                         out += "<aside class='"+pack+"'><img src='"+ msg.sender.profile_image_url + "'></aside>";
55                         //out += "<a href='#message/"+msg.sender_screen_name+"'>";
56                         out += "<p>" + msg.text + "</p>";
57                         out += "<p>" + msg.sender_screen_name + " - " + msg.created_at + "</p>";
58                         //out += "</a>";
59                         out += "</li>";
60                 }
61                 
62                 this.elm.html(out);
63                 if(clb) clb(data);
64         }.bind(this));
65         
66 }
67
68 function Settings(sel_elm) {
69         this.elm = $(sel_elm);
70         this.update();
71         
72         
73         this.elm.find(".autoupdate").on("blur", function(e,b){ 
74                 this.set(e.target.id, $(e.target).val());
75         }.bind(this));
76 }
77         
78 Settings.prototype.update = function(clb) {
79         this.opts = JSON.parse( window.localStorage.getItem("opts") );
80         if (this.opts === null) this.opts={};
81
82         for (k in this.opts) {
83                 this.elm.find("#"+k).val(this.opts[k]);
84         }
85         if(clb) clb();
86 }
87         
88 Settings.prototype.is_valid = function() {
89         return this.opts !== null && this.opts.host !== "" && this.opts.user !== "" && this.opts.pass !=="";
90 }
91         
92 Settings.prototype.set = function(name, value) {
93         this.opts[name] = value;
94         window.localStorage.setItem("opts", JSON.stringify(this.opts));
95 }
96
97
98
99 /**
100  * App Object
101  */
102 var app = {
103     // Application Constructor
104     initialize: function() {
105                 app.settings = new Settings("#settingsPage");
106                 app.messagelist = new MessageList("#messageList");
107                 app.messages    = new Messages("#messages");
108                 
109                 routie({
110                         '': app.page_messageList,
111                         'settings' : app.page_settings,
112                         'message/:user': app.page_messageUser
113                 });
114                 
115                 
116         this.bindEvents();
117     },
118         
119         page_messageList : function(){
120                 if (!app.settings.is_valid()) {
121                         alert("Please fill required settings");
122                         routie("settings");
123                         return;
124                 }
125                 app.messagelist.update(function(){
126                         if ($("body > .current").length>0) { 
127                                 $("body > .current").removeClass("current").addClass("right");
128                                 $("#inboxPage").addClass("current");
129                         }
130                 });
131         },
132         
133         page_messageUser : function(user) {
134
135                 app.messages.update(user, function(){
136                         
137                         $("#messagePage .scrollable").scrollTop(
138                                 $("#messages li:last-of-type").offset().top
139                         );
140                         
141                         $("#inboxPage").removeClass("current").addClass("left");
142                         $("#messagePage").removeClass("right").addClass("current");
143                 });
144         },
145         
146         page_settings : function() {
147                 app.settings.update(function(){
148                         $("#inboxPage").removeClass("current").addClass("left");
149                         $("#settingsPage").removeClass("right").addClass("current");
150                 });             
151         },
152         
153     // Bind Event Listeners
154     //
155     // Bind any events that are required on startup. Common events are:
156     // 'load', 'deviceready', 'offline', and 'online'.
157     bindEvents: function() {
158         //document.addEventListener('deviceready', this.onDeviceReady, false);
159     }
160 };
161
162 $("progress").hide();
163 $.ajaxSettings.beforeSend = function() { $("progress").show(); }
164 $.ajaxSettings.complete = function() { $("progress").hide(); }
165
166
167 app.initialize();