OSDN Git Service

embr
[embrj/master.git] / js / home.js
1 // Global Const
2 var INTERVAL_COOKIE = 'homeInterval';
3 $(function () {
4         formFunc();
5         $("#allTimeline").click(function(e) {
6                 var $this = $(e.target);
7                 var type = $this.attr('class');
8                 switch(type) {
9                         case 'rt_btn':
10                                 e.preventDefault();
11                                 onRT($this);
12                                 break;
13                         case 'retw_btn':
14                                 e.preventDefault();
15                                 onNwRT($this);
16                                 break;
17                         case 'replie_btn':
18                                 e.preventDefault();
19                                 onReplie($this,e);
20                                 break;
21                         case 'favor_btn':
22                                 e.preventDefault();
23                                 onFavor($this);
24                                 break;
25                         case 'unfav_btn':
26                                 e.preventDefault();
27                                 UnFavor($this);
28                                 break;
29                         case 'delete_btn':
30                                 e.preventDefault();
31                                 onDelete($this);
32                                 break;
33                         case 'rt_undo':
34                                 e.preventDefault();
35                                 onUndoRt($this);
36                                 break;
37                 }
38         });
39         markReply($("#allTimeline > li"));
40         $("#submit_btn").click(function (e) {
41                 e.preventDefault();
42                 updateStatus();
43         });
44         $("body").live("click", function (e) {
45                 document.title = document.title.replace(/(\([0-9]+\))/g, "");
46                 $(".new").remove();
47         });
48         setUpdateInterval();
49 });
50 var setUpdateInterval = function () {
51         if (!location.href.split("?")[1] || location.href.split("?")[1] == "p=1") {
52                 var interval = parseFloat($.cookie(INTERVAL_COOKIE));
53                 if (interval === 0.0) {
54                         return false;
55                 }
56                 interval = interval > 0 ? interval : 1;
57                 UPDATE_INTERVAL = setInterval(function () {
58                         update();
59                 }, interval * 1000 * 60);
60         }
61 };
62 function update() {
63         if (PAUSE_UPDATE === true) {
64                 window.setTimeout(update, 5000);
65         } else if (PAUSE_TIMELINE === true) {
66                 return 0;
67         } else {
68                 PAUSE_TIMELINE = true;
69                 updateSentTip('Retrieving new tweets...', 5000, 'ing');
70                 if ($.cookie("intervalChanged") === "true") {
71                         clearInterval(UPDATE_INTERVAL);
72                         $.cookie("intervalChanged", "")
73                         setUpdateInterval();
74                 }
75                 $("ol.timeline li.mine").removeClass("mine").addClass("myTweet");
76                 var since_id = $("ol.timeline li:not(.myTweet):not(#ajax_statuses li):first").find(".status_id").text();
77                 $.ajax({
78                         url: "ajax/updateTimeline.php",
79                         type: "GET",
80                         dataType: "text",
81                         data: "since_id=" + since_id,
82                         success: function (msg) {
83                                 if ($.trim(msg).indexOf("</li>") > 0) {
84                                         var source = $(msg).prependTo($(".timeline"));
85                                         var num = 0;
86                                         if (document.title.match(/\d+/) != null) {
87                                                 num = parseInt(document.title.match(/\d+/));
88                                         }
89                                         document.title = "(" + (num + $(msg).length - 1) + ") " + document.title.replace(/(\([0-9]+\))/g, "");
90                                         markReply($('#allTimeline > li'));
91                                         filterEle();
92                                         embrTweet(source);
93                                         if($("div.new").length == 1) {
94                                                 $("div.new").show().slideDown("fast");
95                                         } else {
96                                                 $("div.new").filter(":first").remove();
97                                                 $("span.tweetcount").filter(":last").text(num + $(msg).length - 1);
98                                         }
99                                         $("span.big-retweet-icon").tipsy({
100                                                 gravity: 's'
101                                         });
102                                         previewMedia(source);
103                                 } else {
104                                         updateSentTip('No new tweets', 3000, 'failure');
105                                 }
106                                 PAUSE_TIMELINE = false;
107                         },
108                         error: function (msg) {
109                                 PAUSE_TIMELINE = false;
110                         }
111                 }); 
112         }
113 }