-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.html
974 lines (762 loc) · 34.4 KB
/
index.html
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta charset="utf-8" />
<title>Twitter-Mobile</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" >
// make app zoomable
$(document).bind("mobileinit", function(){
$.mobile.metaViewportContent = "width=device-width, minimum-scale=1, maximum-scale=2";
});
</script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.js"></script>
<script src="http://platform.twitter.com/anywhere.js?id=C4vv3E46DAprDT8m7pO0sw&v=1" type="text/javascript"></script>
</head>
<body>
<style>
/* customize jquerymobile css for this application */
.ui-li .ui-btn-inner { padding: .7em 50px .7em 15px; }
.ui-li-has-thumb .ui-btn-inner { min-height: 45px; padding: .7em 45px .7em 55px; }
.ui-li-heading { margin: 0em 0 0.6em 0; }
.ui-li-desc { margin: -.5em 0 .6em; text-overflow: ellipsis; overflow: visible; white-space: pre-line; }
.ui-li-thumb, .ui-li-icon { top: 13px; }
.ui-input-search { width: 50% !important;}
.ui-btn-icon-right .ui-icon { position: absolute; top: 30%; margin-top: -9px; }
</style>
<script type="text/javascript">
/*
Copyright (c) 2010 Richard Sepulveda
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Twitter-Mobile is a Twitter client for mobile devices using jQuery Mobile and the twitter @anywhere API
My goal was to create a standalone webpage that self generates its pages based on interaction
with twitter.com. (no further interaction is required with the originating server)
See the README for installation instructions, etc.
*/
phistory = []; // stack of pages
myCurrentUser = null; // the current logged in user data
// user pressed the backbutton on a page
function backButton()
{
// we pop the top page unless we are already on the bottom
if( phistory.length == 1)
return;
phistory.length = phistory.length - 1;
current = phistory[phistory.length - 1];
// build the page 'id' for this jquery mobile 'page' (we can't use @ or # in the name so we change the name)
var newtitle = title = current;
if( title[0] == '@')
newtitle = "AT_" + title.substr( 1, title.length - 1);
if( title[0] == '#')
newtitle = "LB_" + title.substr( 1, title.length - 1);
stitle = '#' + newtitle;
// now tell jquerymobile to display the top-of-stack page
$.mobile.changePage($(stitle));
}
// display the specified tweet data (timeline, @user, #subject)
function fetchPage(current)
{
if( phistory[phistory.length - 1] == current) // don't launch the same page
return;
displayPageLoadingIndicator(); // page loading animation
if( current == "Timeline"){ // user's home timeline
twttr.anywhere(function (T) {
T.User.current().homeTimeline({count:50, success:function(statuses){
$.mobile.pageLoading(true);
updateStatusPage(T, statuses.array, current);
}});
});
}
else if( current[0] == '@'){ // grab specified user's tweets
twttr.anywhere(function (T) {
T.User.find( encodeURIComponent(current.substr(1,current.length - 1))).timeline({count:50, success:function(statuses){
$.mobile.pageLoading(true);
updateStatusPage(T, statuses.array, current);
}});
});
}
else if( current[0] == '#'){ // grab tweets containing specified string
var url = 'http://search.twitter.com/search.json?q=' + encodeURIComponent(current);
$.getJSONP({
url:url,
timeout: 30000,
data: {rpp:50},
error: function(xhr, status, e) {
alert( "An error occurred while fetching search results");
},
success: function(json) {
$.mobile.pageLoading(true);
if (json.error) {
alert( "An error occurred while fetching search results");
return;
}
twttr.anywhere(function (T) {
updateStatusPage(T, json.results, current);
});
}
});
}
return false;
}
// fn to handle jsonp with timeouts and errors
// hat tip to Ricardo Tomasi for the timeout logic
$.getJSONP = function(s) {
s.dataType = 'jsonp';
$.ajax(s);
// figure out what the callback fn is
var $script = $(document.getElementsByTagName('head')[0].firstChild);
var url = $script.attr('src') || '';
var cb = (url.match(/callback=(\w+)/)||[])[1];
if (!cb)
return; // bail
var t = 0, cbFn = window[cb];
$script[0].onerror = function(e) {
$script.remove();
handleError(s, {}, "error", e);
clearTimeout(t);
};
if (!s.timeout)
return;
window[cb] = function(json) {
clearTimeout(t);
cbFn(json);
cbFn = null;
};
t = setTimeout(function() {
$script.remove();
handleError(s, {}, "timeout");
if (cbFn)
window[cb] = function(){};
}, s.timeout);
function handleError(s, o, msg, e) {
// support jquery versions before and after 1.4.3
($.ajax.handleError || $.handleError)(s, o, msg, e);
}
};
// this is called when we want to send a message( tweet, reply, direct message)
function sendMessage(title,id,callback,msg)
{
// set title of message page
$('.tweetTitle').html(title);
// set the textarea element if specified in 'msg'
$('.tweetText').attr("value", msg||"");
// display the page
phistory[phistory.length] = "message_page";
$.mobile.changePage($('#message_page'));
// if the user presses send, we grab the message and call the callback to complete the send
$('.sendButton').click( function(){
msg = $(".tweetText").attr("value");
if( msg.length <= 0){
alert( "You can't send an empty message");
return false;
}
// this is a poor guess of max message length. i could be more accurate by actually searching the
// message for http: regexp pattern's and replacing that length with a shortened url length such
// as http://t.co/KU7nTOX (19 characters)
if( msg.length >= 340){
alert( "Your message exceeded 340 characters. The twitter limit is 240 characters but " +
"your html links may be shortened. Please reduce your message length.");
return false;
}
// i haven't found a list of valid characters that twitter accepts, but this type of regexp can be
// used to filter bad characters
// msg = msg.replace (/[^0-9A-Z\s\-\'\"]/gi,""); // "'
// in terms of escaping dangerous database characters, i am going to assume that twitter.com will
// escape any data that they are storing in their databases.
callback(id,msg);
return false;
});
}
// gets a user's twitter profile and display's it
function profile(user)
{
twttr.anywhere(function (T) {
// the only way that i have found to get User data is to look in the tweet data that the user has sent
T.User.find( encodeURIComponent(user.substr(1,user.length - 1))).timeline({ success:function(statuses){
var userData=statuses.array[0].user; // just get the user's first tweet and extract the User structure
var str="";
str += '<p style="padding:5px;">';
str += "Screen Name: " + userData.screenName + "<br />";
str += "Biography: " + (userData.description || " ") + "<br />";
str += "Statuses: " + userData.statusesCount + "<br />";
str += "Followers: " + userData.followersCount + "<br />";
str += "Following: " + userData.friendsCount + "<br />";
str += "Favourites: " + userData.favouritesCount + "<br />";
str += "Location: " + (userData.location || " ") + "<br />";
str += "Website: " + (userData.url || " ") + "<br />";
str += "</p>";
$("#profile_page .pbox").html(str);
$('#profile_page .rtitle').html( user + " Profile");
// push the page on the stack and display it
phistory[phistory.length] = user;
$.mobile.changePage($('#profile_page'));
}});
});
}
var TimelineMode = 0;
var UserMode = 1;
var SearchMode = 2;
var DMMode = 3;
// this builds the tweet page (home timeline, @user, or #subject)
function updateStatusPage(T, statuses, title, displayPage)
{
if( displayPage == undefined) // do we also want to display the page after it is built?
displayPage = true;
var mode = TimelineMode;
if( title == 'DirectMessages')
mode = DMMode;
else if( title[0] == '@')
mode = UserMode;
else if( title[0] == '#')
mode = SearchMode;
// build the title for this page
var rtitle = title;
if( title == "Timeline"){
screenName = myCurrentUser.data('screen_name');
rtitle = "@" + screenName + " Timeline";
}
// build the page 'id' for this jquery mobile 'page' (we can't use @ or # in the name so we change the name)
var newtitle = title;
if( title[0] == '@')
newtitle = "AT_" + title.substr( 1, title.length - 1);
if( title[0] == '#')
newtitle = "LB_" + title.substr( 1, title.length - 1);
// build the jquery selector for this 'page'
var stitle = '#' + newtitle;
// is this page already in the DOM? If not, we clone the template then modify it
if( $(stitle).length <= 0)
{
var $clone = $("#default_page").clone();
$clone.attr("id",newtitle); // set the id of the 'page'
$("#anywhere").after($clone); // put the clone in the DOM
// store some internal data for the 'page'
jQuery.data( $(stitle)[0], "page_data", { title: title, id: stitle });
var pagesel = stitle;
// this attempts to workaround a problem with JQM. I was sometimes getting strange
// behavior with the fixed header and footer. They would sometimes hang in the middle
// of the page or disappear altogether. After some digging in the JQM sources, I found
// the $.fixedToolbars function that appears to control these. The show command will force
// them to display, so i tied that to the window.scroll() event. One other problem popped up
// where the header was showing below the normal 'content'. The fixed header/footer are set
// to z-index:1000 in the JQM CSS but for some reason that seemed to change?? so i also force
// the z-index to 1000.
$(window).scroll( function(){
$(pagesel).find("[data-role=header],[data-role=footer]").css("z-index","1000");
$.fixedToolbars.show(true);
});
// set the page title and back button visible attribute
$( stitle + ' .rtitle').html(rtitle);
$( stitle + ' .myheader').attr( 'data-nobackbtn', (phistory.length<=1)?'true':'false');
// if this page is for an @user, we display the twitter follow user button
if( title[0] == '@'){
twttr.anywhere(function (T) {
$(stitle + " .follow-twitterapi").html("");
T(stitle + " .follow-twitterapi").followButton(title.substr(1,title.length - 1));
});
}
// workaround for strange bug cloning a page with search included
$( stitle + ' .content').prepend(
' <form action="#" class="searchForm">'+
' <div data-role="fieldcontain" style="padding:0; margin:0 0 20px 0;">'+
//' <label for="search">Search:</label>'+
' <input type="search" name="search" class="search" value="" style="font-size:13px" />'+
' </div>'+
' </form>');
$( stitle + ' .searchForm').page(); // tell jQueryMobile to compile this block of HTML
// setup the buttons on the footer
$(stitle + " .action-buttons").attr("status_id", title);
// we hide the reply button and profile button except on User pages
if( mode != UserMode){
var selector = stitle + " a.publicReplyButton," + stitle + " a.profileButton";
$(selector).css("visibility","hidden");
}
// setup the search field callback
$(stitle + " .searchForm").submit( function(){
// get the search string and call twitter search, then display the status results
var searchVal = $(stitle + " .search").attr("value");
var url = 'http://search.twitter.com/search.json?q=' + encodeURIComponent( searchVal);
$.getJSONP({
url:url,
timeout: 30000,
data: {rpp:50},
error: function(xhr, status, e) {
alert( "An error occurred while fetching search results");
},
success: function(json) {
if (json.error) {
alert( "An error occurred while fetching search results");
return;
}
twttr.anywhere(function (T) {
updateStatusPage(T, json.results, searchVal);
});
}
});
return false;
});
installButtonHandlers(stitle,title);
}
var str = buildTweetTable( statuses);
// clear out the tweet section of our page and insert the fresh data
$( stitle + ' .tweets').html("");
$( stitle + ' .tweets').append(str);
$( stitle + " ul").page(); // now get jquerymobile to add its special markup to our html
// install retweet button for each tweet
if( mode != DMMode){
$(stitle + " span.ui-icon-arrow-r").before('<span style="right:10px; top:70%%;" class="ui-icon ui-icon-refresh"></span>');
$(stitle + " span.ui-icon-refresh").unbind();
$(stitle + " span.ui-icon-refresh").click( function(){
var id = $(this).closest("li.tweet-li").attr("data-tweet-id"); // get this tweet's id
displayDialog( "Retweet", "Alert", "Are you sure that you want to send this retweet?", ["Yes","No"], id,
function(buttonIndex,id){
if( buttonIndex == '0'){ // send retweet
twttr.anywhere(function (T) {
T.Status.retweet(id);
});
} else { // cancel retweet
}
});
return false;
});
}
installLinkHandlers(stitle,mode);
// if we want to display the page, we push our hash history and tell jqm to change the page
if( displayPage){
phistory[phistory.length] = title;
$.mobile.changePage(stitle);
}
} // end updateStatusPage()
// installs the handlers for the links in each tweet (thumbnail, @user, #subject, reply button, retweet button)
function installLinkHandlers(stitle,mode)
{
// convert the embedded span tags into <a> tags. we did this because jqm manipulates <a> tags in unwanted ways
$(stitle + " span.atag").each( function(){
$(this).after( "<a class='aref' href='" + $(this).attr("href") + "'>" + $(this).attr("href") + "</a>");
});
$(stitle + " span.btag").each( function(){
$(this).after( "<a class='bref' href='" + $(this).attr("href") + "'>" + $(this).attr("href") + "</a>");
});
// user pressed a link embedded in the tweet
$(stitle + " a.aref").click( function(){
var href = $(this).attr("href");
window.location = href;
return false;
});
// user pressed a @user or #subject embedded in the tweet
$(stitle + " a.bref").click( function(){
var href = $(this).attr("href");
fetchPage( href); // display the page associated with this @user or #subject
return false;
});
// user pressed the thumbnail
$(stitle + " p," + stitle + " img," + stitle + " a.status_reply").unbind();
$(stitle + " p," + stitle + " a.status_reply").click( function(){ return false});
$(stitle + " img").click( function(){
var href = $(this).siblings().find("a.status_reply").attr("status_id");
if( href != ('@' + myCurrentUser.data('screen_name')))
fetchPage( href); // display the page associated with this @user or #subject
return false;
});
// button at the right of tweet (reply to tweet)
$(stitle + " span.ui-icon-arrow-r").unbind();
$(stitle + " span..ui-icon-arrow-r").click( function(){
// find the owner of this tweet (stored in the $("a.status_reply) tag in attribute "status_id"
var id = $(this).siblings().find("a.status_reply").attr("status_id");
if( mode == DMMode){
if( id == ('@' + myCurrentUser.data('screen_name'))) // can't send a DM to yourself
return false;
sendMessage("Direct Message to " + id, id, function(id,msg){
twttr.anywhere(function (T) {
T.DirectMessage.send( id.substr(1,id.length - 1), msg, {success: function(){
backButton();
}, error:function(){
alert("Error sending message to " + id);
backButton();
}});
});
return false;
});
return false;
}
else {
sendMessage("Reply to " + id, id, function(id,msg){
twttr.anywhere(function (T) {
T.Status.reply( 0, msg, {success: function(){
backButton();
}, error:function(){
alert("Error sending message to " + id);
backButton();
}});
});
return false;
}, id + " ");
return false;
}
});
}
// installs the handlers for the buttons in the footer
function installButtonHandlers(stitle,title)
{
// the direct message to @user callback
$(stitle + " .directMsgButton").click( function(){
if( title == 'DirectMessages') // if we are already displaying direct messages, ignore this
return;
if( title == 'Timeline'){ // Display all direct messages sent to and from current user
twttr.anywhere(function (T) {
displayPageLoadingIndicator();
T.User.current().directMessages( {count:50, success:function(receiveDMs){
T.User.current().sentMessages( {count:50, success:function(sentDMs){
$.mobile.pageLoading(true);
var myReceiveDMs = receiveDMs.array;
var newArray = myReceiveDMs.concat(sentDMs.array);
var nowDate = new Date();
newArray.sort( function (a,b) {
var createdDateA = new Date(a.createdAt);
var createdDateB = new Date(b.createdAt);
var diffA = nowDate - createdDateA;
var diffB = nowDate - createdDateB;
return diffA - diffB;
});
updateStatusPage(T, newArray, "DirectMessages");
}});
}});
});
return false;
}
else { // send a Direct message to this user
sendMessage("Direct Message to " + $(this).attr("status_id"), $(this).attr("status_id"), function(id,msg){
twttr.anywhere(function (T) {
T.DirectMessage.send( id.substr(1,id.length - 1), msg, {success: function(){
backButton();
}, error:function(){
alert("Error sending message to " + id);
backButton();
}});
});
return false;
});
return false;
}
});
// the public reply to @user callback
$(stitle + " .publicReplyButton").click( function(){
sendMessage("Reply to " + $(this).attr("status_id"), $(this).attr("status_id"), function(id,msg){
twttr.anywhere(function (T) {
T.Status.reply( 0, msg, {success: function(){
backButton();
}, error:function(){
alert("Error sending message to " + id);
backButton();
}});
});
return false;
}, $(this).attr("status_id") + " ");
return false;
});
// the user's profile callback
$(stitle + " .profileButton").click( function(){
profile($(this).attr("status_id"));
return false;
});
// the tweet button callback
$(stitle + " .sendTweetButton").click( function(){
sendMessage("Send tweet", $(this).attr("status_id"), function(id,msg){
twttr.anywhere(function (T) {
T.Status.update( msg, {success: function(){
backButton();
}, error:function(){
alert("Error sending tweet");
backButton();
}});
});
return false;
});
return false;
});
// logout button callback
$(stitle + " .logoutButton").click( function(){
twttr.anywhere.signOut();
return false;
});
}
// build the UL/LI table containing tweets for this page
function buildTweetTable(statuses)
{
// build the ul/li markup for the statuses(tweets)
var str = "";
str += '<ul data-role="listview">';
for( var i=0; i<statuses.length; ++i)
{
var status = statuses[i];
// convert any characters that may be confused as being html to their html escaped equivalents
status.text = htmlEntities(status.text);
// we find all linkable items in the tweet, http:, @user, #subject and embed them in <span> tags
// we install the real handlers for these a-tags later
var text = status.text.replace(/(http[s]?:\/\/[a-zA-Z0-9\.\-\_\/\?\%\#\&\=]+)/g, "<span class='atag' href='$&'></span>");
text = text.replace(/(#[^:\s]+)/g, "<span class='btag' href='$&'></span>");
text = text.replace(/(@[a-zA-Z0-9\_]+)/g, "<span class='btag' href='$&'></span>");
// calculate the age of the tweet
var createdAt = status.createdAt || status.created_at;
var createdDate = new Date(createdAt);
var nowDate = new Date();
var diff = nowDate - createdDate;
var milliseconds=Math.floor(diff % 1000);
diff=diff/1000;
var seconds=Math.floor(diff % 60);
diff=diff/60;
var minutes=Math.floor(diff % 60);
diff=diff/60;
var hours=Math.floor(diff % 24);
diff=diff/24;
var days=Math.floor(diff);
if( hours <= 0){
createdAt = minutes + ((minutes == 1)?" minute ago":" minutes ago");
}
else if( hours < 24) {
createdAt = hours + ((hours == 1)?" hour ago":" hours ago");
}
else {
createdAt = days + ((days == 1)?" day ago":" days ago");
}
// build the li for this tweet
// we look at different 'status' structure names depending on the where we got the statuses
if( status.sender){
str +=
"<li class='tweet-li' data-tweet-id='" + status.idStr + "'>"+
" <img src='" + status.sender.profileImageUrl + "' />"+
" <h4><a href='#' class='status_reply' status_id='@" + status.sender.screenName + "'>" +
status.sender.screenName + " -> " + status.recipientScreenName +
"<span style='font-weight:normal;font-size:90%;'> <br />(" +
(createdDate.getMonth() + 1) + "/" + createdDate.getDate() + "/" + createdDate.getFullYear() + " " +
createdDate.getHours() + ":" + createdDate.getMinutes() + ")</span>" +
" </a></h4>"+
" <p>" + text + "</p>"+
"</li>";
}
else if( status.user)
{
str +=
"<li class='tweet-li' data-tweet-id='" + status.idStr + "'>"+
" <img src='" + status.user.profileImageUrl + "' />"+
" <h4><a href='#' class='status_reply' status_id='@" + status.user.screenName + "'>" +
status.user.name + "<span style='font-weight:normal;'> (" + createdAt + ")</span>" +
" </a></h4>"+
" <p>" + text + "</p>"+
"</li>";
}
else
{
str +=
"<li class='tweet-li' data-tweet-id='" + status.idStr + "'>"+
" <img src='" + status.profile_image_url + "' />"+
" <h4><a href='#' class='status_reply' status_id=''>" +
status.from_user + "<span style='font-weight:normal;'> (" + createdAt + ")</span>"+
" </a></h4>"+
" <p>" + text + "</p>"+
"</li>";
}
}
str += '</ul>';
return str;
}
// this is called on startup to see if we are already logged into twitter, if not, we display
// a twitter connection button otherwise we display the home timeline statuses
twttr.anywhere(function (T) {
if (T.isConnected()) {
fetchPage("Timeline");
myCurrentUser = T.currentUser;
} else {
$("#twitter-connect-placeholder").html("");
T("#twitter-connect-placeholder").connectButton({
authComplete: function(user) {
fetchPage("Timeline");
myCurrentUser = T.currentUser;
},
signOut: function() {
// triggered when user logs out
}
});
};
});
// this gets called anytime we login or logout of twitter
twttr.anywhere(function (T) {
T.bind("authComplete", function (e, user) { // triggered when auth completed successfully
});
T.bind("signOut", function (e) {
phistory.length = 0; // clear the history hash
$("#twitter-connect-placeholder").html("");
$.mobile.changePage($("#login_page")); // display login page
T("#twitter-connect-placeholder").connectButton({
authComplete: function(user) {
fetchPage("Timeline");
myCurrentUser = T.currentUser;
},
signOut: function() {
// triggered when user logs out
}
});
});
});
// autorefresh the home timeline
autorefreshTime = 10; // in minutes
refreshTimeline();
function refreshTimeline()
{
if( $("#Timeline").length){
twttr.anywhere(function (T) {
displayPageLoadingIndicator();
T.User.current().homeTimeline({count:50, success:function(statuses){
$.mobile.pageLoading(true);
updateStatusPage(T, statuses.array, "Timeline", false);
}});
});
}
setTimeout(function(){
refreshTimeline()
}, autorefreshTime * 60 * 1000);
}
// display the page loading animation and automatically dismisses the indicator just in case
// the ajax call never completes (default timeout 10 seconds)
function displayPageLoadingIndicator()
{
$.mobile.pageLoading();
setTimeout(function(){
$.mobile.pageLoading(true);
}, 10 * 1000);
}
// this routine displays a dialog with the given title's, description and button titles
// it calls the callback function when the user selects a button, returning the private data to complete
// processing
function displayDialog( pageTitle, dialogTitle, dialogDescription, buttonTitles, data, callback)
{
if( callback == undefined)
return;
$("#dialog_page h1.page-title").html(pageTitle||"");
$("#dialog_page div.dialog-title").html(dialogTitle||"");
$("#dialog_page div.dialog-description").html(dialogDescription||"");
// add the buttons to the page
$("#dialog_page div.dialog-buttons").html("");
for( i=0; i<buttonTitles.length; ++i){
$("#dialog_page div.dialog-buttons").append("<button class='dialog_button' style='width:100%%; height: 44px; " +
" background:white; border-radius: 10px; font-size:110%%; margin-bottom: 10px;' href='#' name='" + i + "'>" + buttonTitles[i] + "</button><br />");
}
// setup a handler to catch button click events
$("#dialog_page .dialog_button").click( function(){
backButton(); // pop the dialog from the screen
callback( $(this).attr("name"), data); // call the user's callback with button pressed and private data
return false;
});
// this just pushes the dialog to the screen
phistory[phistory.length] = "dialog_page";
$.mobile.changePage($("#dialog_page"));
}
// replace any html delimiters with their html escaped equivalents
function htmlEntities(str)
{
// return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); //"
// removed '&' since that may interfere with capturing a URL and wrapping it with <a> tags.
return String(str).replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); //"
}
</script>
<div id="login_page" data-role="page" class="foo">
<div class="myheader" data-role="header" data-position="fixed" data-nobackbtn="true">
<!-- <a class="back-btn" href="#" data-icon="arrow-l" onclick="backButton();">Back</a> -->
<h1 class="rtitle">Twitter Login</h1>
</div>
<div class="content" data-role="content" style="">
<div id="twitter-connect-placeholder"></div>
</div>
<div data-role="footer" data-position="fixed">
</div>
</div>
<div id="default_page" data-role="page" class="foo">
<div class="myheader" data-role="header" data-position="fixed" data-nobackbtn="true">
<a class="back-btn" href="#" data-icon="arrow-l" onclick="backButton();">Back</a>
<h1 class="rtitle"></h1>
<a class="sendTweetButton" status_id="" href="#" class="ui-btn-right" style="">Tweet</a>
<span class="follow-twitterapi"></span>
</div>
<div class="content" data-role="content" style="padding:5px 20px 20px 20px;">
<div class="tweets"></div>
</div>
<div class="footer" data-role="footer" data-position="fixed" >
<a href="#" data-role="button" class="logoutButton">Logout</a>
<a href="#" data-role="button" class="action-buttons directMsgButton" status_id="">Direct Msg</a>
<a href="#" data-role="button" class="action-buttons publicReplyButton" status_id="">Reply</a>
<a href="#" data-role="button" class="action-buttons profileButton" status_id="">Profile</a>
</div>
</div>
<div id="message_page" data-role="page">
<div class="myheader" data-role="header" data-position="fixed">
<a class="back-btn" href="#" data-icon="arrow-l" onclick="backButton();">Back</a>
<h1 class="rtitle">Tweet</h1>
</div>
<div class="content" data-role="content" style="">
<div class="tbox" style="">
<div style="padding-left:5px; width:300px;">
<form class="sendTweet" action="#">
<p style="font-size:110%;">
<span class="tweetTitle">@rsepulveda99</span>
</p>
<textarea class="tweetText" style="height:85px; width:270px;"></textarea>
<button style="float:right; height:44px; font-weight: bold; font-size: 115%%; ' +
margin-bottom: 5px; margin-right: 5px;" class="sendButton" type="button">Send</button>
</form>
</div>
</div>
</div>
<div class="footer" data-role="footer" data-position="fixed" >
<a href="#" data-role="button" class="logoutButton">Logout</a>
<a href="#" data-role="button" class="action-buttons profileButton" status_id="">Profile</a>
</div>
</div>
<div id="profile_page" data-role="page">
<div class="myheader" data-role="header" data-position="fixed">
<a class="back-btn" href="#" data-icon="arrow-l" onclick="backButton();">Back</a>
<h1 class="rtitle">Profile</h1>
</div>
<div class="content" data-role="content" style="">
<div class="pbox" style=""></div>
</div>
<div class="footer" data-role="footer" data-position="fixed" >
<a href="#" data-role="button" class="logoutButton">Logout</a>
<a href="#" data-role="button" class="action-buttons directMsgButton" status_id="">Direct Msg</a>
<a href="#" data-role="button" class="action-buttons publicReplyButton" status_id="">Reply</a>
</div>
</div>
<div id="dialog_page" data-role="page" style="background:gray;">
<div class="myheader" data-role="header" data-position="fixed">
<a class="back-btn" href="#" data-icon="arrow-l" onclick="backButton();">Back</a>
<h1 class="page-title"></h1>
</div>
<div class="content" data-role="content" style="">
<div class="ui-body-c" style="padding:10px; border-radius: 10px;">
<div class="dialog-title" style="font-size: 105%; font-weight:bold; margin-bottom:10px; text-align:center;"></div>
<div class="dialog-description" style="width:100%; margin-bottom:20px; text-align:center;"></div>
<div class="dialog-buttons" style="width:100%;"></div>
</div>
</div>
<div class="footer" data-role="footer" data-position="fixed" >
<a href="#" data-role="button" class="logoutButton">Logout</a>
</div>
</div>
<div id="anywhere"></div>
</body>
</html>