diff --git a/app/assets/javascripts/discourse.js b/app/assets/javascripts/discourse.js index ebfadc83d3c..7fb9937720b 100644 --- a/app/assets/javascripts/discourse.js +++ b/app/assets/javascripts/discourse.js @@ -143,8 +143,7 @@ Discourse = Ember.Application.createWithMixins(Discourse.Ajax, { bootbox.animate(false); bootbox.backdrop(true); // clicking outside a bootbox modal closes it - Discourse.Session.currentProp('mobileDevice', $html.hasClass('mobile-device')); - Discourse.Session.currentProp('mobileView', $html.hasClass('mobile-view')); + Discourse.Mobile.init(); setInterval(function(){ Discourse.Formatter.updateRelativeAge($('.relative-date')); diff --git a/app/assets/javascripts/discourse/components/mobile.js b/app/assets/javascripts/discourse/components/mobile.js new file mode 100644 index 00000000000..3678ccd2451 --- /dev/null +++ b/app/assets/javascripts/discourse/components/mobile.js @@ -0,0 +1,36 @@ +/** + A class that is responsible for logic related to mobile devices. + + @class Mobile + @namespace Discourse + @module Discourse +**/ +Discourse.Mobile = { + + isMobileDevice: false, + mobileView: false, + + init: function() { + var $html = $('html'); + this.isMobileDevice = $html.hasClass('mobile-device'); + this.mobileView = $html.hasClass('mobile-view'); + + if (localStorage && localStorage.mobileView) { + var savedValue = (localStorage.mobileView === 'true' ? true : false); + if (savedValue !== this.mobileView) { + this.reloadPage(savedValue); + } + } + }, + + toggleMobileView: function() { + if (localStorage) { + localStorage.mobileView = !this.mobileView; + } + this.reloadPage(!this.mobileView); + }, + + reloadPage: function(mobile) { + window.location.assign(window.location.pathname + '?mobile_view=' + (mobile ? '1' : '0')); + } +}; diff --git a/app/assets/javascripts/discourse/controllers/header_controller.js b/app/assets/javascripts/discourse/controllers/header_controller.js index 613c1586dfc..bf52f41a984 100644 --- a/app/assets/javascripts/discourse/controllers/header_controller.js +++ b/app/assets/javascripts/discourse/controllers/header_controller.js @@ -25,15 +25,15 @@ Discourse.HeaderController = Discourse.Controller.extend({ }.property('currentUser', 'topic.isPrivateMessage'), mobileDevice: function() { - return Discourse.Session.currentProp('mobileDevice'); + return Discourse.Mobile.isMobileDevice; }.property(), mobileView: function() { - return Discourse.Session.currentProp('mobileView'); + return Discourse.Mobile.mobileView; }.property(), toggleMobileView: function() { - window.location.assign(window.location.pathname + '?mobile_view=' + (Discourse.Session.currentProp('mobileView') ? '0' : '1')); + Discourse.Mobile.toggleMobileView(); } }); diff --git a/app/assets/javascripts/discourse/views/header_view.js b/app/assets/javascripts/discourse/views/header_view.js index 03abe05a275..458385466c6 100644 --- a/app/assets/javascripts/discourse/views/header_view.js +++ b/app/assets/javascripts/discourse/views/header_view.js @@ -98,7 +98,7 @@ Discourse.HeaderView = Discourse.View.extend({ **/ logoHTML: function() { var result = "