-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsync.js
More file actions
189 lines (140 loc) · 5.48 KB
/
sync.js
File metadata and controls
189 lines (140 loc) · 5.48 KB
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
/**
* @class monetary.sync
* @static
*
* Handles syncing the user data between tabs and windows.
*/
money.sync = (function(){
return {
/**
* Called every page load to make sure we are storing the most up to date data value, or things get messed up.
*/
init: function(){
if(!Modernizr.localstorage){
return;
}
yootil.storage.set("monetary_data_sync_" + yootil.user.id(), money.data(yootil.user.id()).get.data(), true, true);
var self = this;
// Delay the binding, IE fires it too quickly, grrr.
setTimeout(function(){
$(window).bind("storage", $.proxy(self.handle_syncing, self));
}, 100);
},
/**
* Registers this module with the money object.
* @return {Object} Returns the sync object to be registered.
*/
register: function(){
money.modules.push(this);
return this;
},
/**
* Checks for the original storage event and makes sure we are using the correct key.
*/
handle_syncing: function(evt){
if(evt && evt.originalEvent && evt.originalEvent.key == ("monetary_data_sync_" + yootil.user.id())){
var self = this;
// Again, IE causing us to add in extra steps.
// We are trying to prevent the trigger caller from
// getting synced, there is no need, as it acts as the
// master of the data.
if(money.trigger_caller){
money.trigger_caller = false;
return;
}
self.sync_data(evt.originalEvent);
}
},
/**
* Here we sync the data object with the new data, and we also handle visual stuff, we don't have too but it's nice.
*
* We do a straight swap for now, I see no reason not too.
*
* This only is called when an update to the data has happened.
*/
sync_data: function(evt){
var old_data = evt.oldValue;
var new_data = evt.newValue;
// Stop here if there is no changes, the data is stringified (2 strings)
// This prevents changing the DOM for no reason
if(old_data == new_data){
return;
}
if(new_data && yootil.is_json(new_data)){
new_data = JSON.parse(new_data);
} else {
return;
}
// Straight up swap of new data, we trust it.
//money.data = new_data;
money.data(yootil.user.id()).set.data(new_data, true);
// Handle gifts
if(location.href.match(/\?monetarygift=.+?$/i)){
var code = money.gift.get_gift_code();
var gift = money.gift.valid_code(code);
if(!gift || money.gift.has_received(code) || !money.gift.allowed_gift(gift)){
$(".monetary-gift-notice-content-top").css("opacity", .3);
$(".monetary-gift-notice-content-accept").html("You have accepted this gift in another tab / window.");
}
}
// Handle donations, make sure the user isn't trying to
// accept the same donation multiple times
if(location.href.match(/\?monetarydonation&view=3&id=([\d\.]+)/i)){
var don_id = RegExp.$1;
var the_donation = money.donation.fetch_donation(don_id);
if(!the_donation){
clearInterval(money.donation.interval);
$(".monetary-donation-form").css("opacity", .3);
$(".monetary-donation-button").hide();
pb.window.alert("An Error Has Occurred", "This " + money.donation.settings.text.donation.toLowerCase() + " no longer exists.");
}
}
// Format new money changes
var user_money = money.data(yootil.user.id()).get.money(true);
var user_bank_money = money.data(yootil.user.id()).get.bank(true);
var user_donations_sent = money.data(yootil.user.id()).get.total_sent_donations(true);
var user_donations_received = money.data(yootil.user.id()).get.total_received_donations(true);
// Now lets see where we are, and attempt to update visuals
var location_check = (yootil.location.search_results() || yootil.location.message_thread() || yootil.location.thread() || yootil.location.recent_posts() || yootil.location.profile_home() || yootil.location.members());
if(location_check){
var user_id = yootil.user.id();
$(".pd_money_amount_" + user_id).text(yootil.number_format(user_money));
$(".pd_bank_amount_" + user_id).text(yootil.number_format(user_bank_money));
$(".pd_donations_sent_amount_" + user_id).text(yootil.number_format(user_donations_sent));
$(".pd_donations_received_amount_" + user_id).text(yootil.number_format(user_donations_received));
}
// See if there is a wallet about
var wallet = $("#pd_money_wallet_amount");
if(wallet.length){
wallet.text(yootil.number_format(user_money));
}
var other_wallet = $(".money_wallet_amount");
if(other_wallet.length){
other_wallet.html(money.settings.text.wallet + money.settings.money_separator + money.settings.money_symbol + yootil.html_encode(user_money));
}
// Lets see if it's the bank, if so update the balance.
// Don't bother with transactions, it's in the data, but
// no need to visually update it, for now.
if(yootil.location.forum() && location.href.match(/\/?bank\/?/i)){
$("#pd_money_bank_balance").text(yootil.number_format(user_bank_money));
}
// Update stock list
if(yootil.location.forum() && location.href.match(/\/?stockmarket\/?/i)){
if(money.stock_market.settings.enabled){
money.stock_market.check_for_data();
money.stock_market.current_investment_list();
}
}
},
/**
* Sometimes we need to trigger the sync (i.e edit money dialog) manually.
*/
trigger: function(){
if(!Modernizr.localstorage){
return;
}
money.trigger_caller = true;
yootil.storage.set("monetary_data_sync_" + yootil.user.id(), money.data(yootil.user.id()).get.data(), true, true);
}
};
})().register();