discourse/app/assets/javascripts/discourse/lib/mobile.js.es6

55 lines
1.4 KiB
Plaintext
Raw Normal View History

// An object that is responsible for logic related to mobile devices.
2016-02-19 00:53:25 +08:00
const Mobile = {
isMobileDevice: false,
mobileView: false,
2016-02-19 00:53:25 +08:00
init() {
const $html = $('html');
this.isMobileDevice = $html.hasClass('mobile-device');
this.mobileView = $html.hasClass('mobile-view');
try{
if (window.location.search.match(/mobile_view=1/)){
localStorage.mobileView = true;
}
if (window.location.search.match(/mobile_view=0/)){
localStorage.mobileView = false;
}
if (localStorage.mobileView) {
var savedValue = (localStorage.mobileView === 'true');
if (savedValue !== this.mobileView) {
this.reloadPage(savedValue);
}
}
} catch(err) {
// localStorage may be disabled, just skip this
// you get security errors if it is disabled
}
},
toggleMobileView: function() {
try{
if (localStorage) {
localStorage.mobileView = !this.mobileView;
}
} catch(err) {
// localStorage may be disabled, skip
}
this.reloadPage(!this.mobileView);
},
reloadPage: function(mobile) {
window.location.assign(window.location.pathname + '?mobile_view=' + (mobile ? '1' : '0'));
}
};
2016-02-19 00:53:25 +08:00
// Backwards compatibiltity, deprecated
Object.defineProperty(Discourse, 'Mobile', {
get: function() {
Ember.warn("DEPRECATION: `Discourse.Mobile` is deprecated, use `this.site.mobileView` instead");
return Mobile;
}
});
export default Mobile;