2015-12-12 04:07:38 +08:00
|
|
|
// An object that is responsible for logic related to mobile devices.
|
2016-02-19 00:53:25 +08:00
|
|
|
const Mobile = {
|
2013-12-19 03:47:22 +08:00
|
|
|
isMobileDevice: false,
|
2013-09-11 04:43:51 +08:00
|
|
|
mobileView: false,
|
|
|
|
|
2016-02-19 00:53:25 +08:00
|
|
|
init() {
|
|
|
|
const $html = $('html');
|
2013-12-19 03:47:22 +08:00
|
|
|
this.isMobileDevice = $html.hasClass('mobile-device');
|
2013-09-11 04:43:51 +08:00
|
|
|
this.mobileView = $html.hasClass('mobile-view');
|
2013-12-19 03:47:22 +08:00
|
|
|
|
2014-06-13 14:42:01 +08:00
|
|
|
try{
|
2015-07-10 10:09:43 +08:00
|
|
|
if (window.location.search.match(/mobile_view=1/)){
|
2014-07-28 09:48:38 +08:00
|
|
|
localStorage.mobileView = true;
|
|
|
|
}
|
2015-07-10 10:09:43 +08:00
|
|
|
if (window.location.search.match(/mobile_view=0/)){
|
2014-07-28 09:48:38 +08:00
|
|
|
localStorage.mobileView = false;
|
|
|
|
}
|
|
|
|
if (localStorage.mobileView) {
|
2014-06-13 14:42:01 +08:00
|
|
|
var savedValue = (localStorage.mobileView === 'true');
|
|
|
|
if (savedValue !== this.mobileView) {
|
|
|
|
this.reloadPage(savedValue);
|
|
|
|
}
|
2013-12-19 03:47:22 +08:00
|
|
|
}
|
2014-06-13 14:42:01 +08:00
|
|
|
} catch(err) {
|
|
|
|
// localStorage may be disabled, just skip this
|
|
|
|
// you get security errors if it is disabled
|
2013-12-19 03:47:22 +08:00
|
|
|
}
|
2013-09-11 04:43:51 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
toggleMobileView: function() {
|
2014-06-13 14:42:01 +08:00
|
|
|
try{
|
|
|
|
if (localStorage) {
|
|
|
|
localStorage.mobileView = !this.mobileView;
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
// localStorage may be disabled, skip
|
2013-09-11 04:43:51 +08:00
|
|
|
}
|
2013-12-19 03:47:22 +08:00
|
|
|
this.reloadPage(!this.mobileView);
|
|
|
|
},
|
2013-12-12 00:17:55 +08:00
|
|
|
|
2013-12-19 03:47:22 +08:00
|
|
|
reloadPage: function(mobile) {
|
|
|
|
window.location.assign(window.location.pathname + '?mobile_view=' + (mobile ? '1' : '0'));
|
|
|
|
}
|
2013-09-11 04:43:51 +08:00
|
|
|
};
|
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;
|