diff --git a/app/assets/javascripts/discourse/lib/mobile.js b/app/assets/javascripts/discourse/lib/mobile.js index 8a0876bf9..c1b904970 100644 --- a/app/assets/javascripts/discourse/lib/mobile.js +++ b/app/assets/javascripts/discourse/lib/mobile.js @@ -5,19 +5,30 @@ @module Mobile **/ 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; } - window.location.reload(); - } + this.reloadPage(!this.mobileView); + }, + reloadPage: function(mobile) { + window.location.assign(window.location.pathname + '?mobile_view=' + (mobile ? '1' : '0')); + } }; diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5e6111101..91c2aa2cb 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -26,6 +26,7 @@ class ApplicationController < ActionController::Base end before_filter :set_locale + before_filter :set_mobile_view before_filter :inject_preview_style before_filter :disable_customization before_filter :block_if_maintenance_mode @@ -119,6 +120,10 @@ class ApplicationController < ActionController::Base end end + def set_mobile_view + session[:mobile_view] = params[:mobile_view] if params.has_key?(:mobile_view) + end + def inject_preview_style style = request['preview-style'] session[:preview_style] = style if style diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index b4309482a..3ad0ece5e 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -19,6 +19,10 @@ module ApplicationHelper end end + def html_classes + "#{mobile_view? ? 'mobile-view' : 'desktop-view'} #{mobile_device? ? 'mobile-device' : 'not-mobile-device'}" + end + def escape_unicode(javascript) if javascript javascript = javascript.dup.force_encoding("utf-8") @@ -111,8 +115,18 @@ module ApplicationHelper "#{Discourse::base_uri}/login" end - def stylesheet_filenames(target=:desktop) - [asset_path("#{target}.css"), customization_disabled? ? nil : SiteCustomization.custom_stylesheet_path(session[:preview_style], target)].compact + def mobile_view? + return false unless SiteSetting.enable_mobile_theme + if session[:mobile_view] + session[:mobile_view] == '1' + else + mobile_device? + end + end + + def mobile_device? + # TODO: this is dumb. user agent matching is a doomed approach. a better solution is coming. + request.user_agent =~ /Mobile|webOS|Nexus 7/ and !(request.user_agent =~ /iPad/) end def customization_disabled? diff --git a/app/models/site_customization.rb b/app/models/site_customization.rb index d5d010595..f46ea5e59 100644 --- a/app/models/site_customization.rb +++ b/app/models/site_customization.rb @@ -83,16 +83,6 @@ class SiteCustomization < ActiveRecord::Base style.stylesheet_link_tag(target).html_safe if style end - def self.custom_stylesheet_path(preview_style, target=:desktop) - preview_style ||= enabled_style_key - style = lookup_style(preview_style) - if style && ((target != :mobile && style.stylesheet) || (target == :mobile && style.mobile_stylesheet)) - style.stylesheet_relative_path(target) - else - nil - end - end - def self.custom_header(preview_style, target=:desktop) preview_style ||= enabled_style_key style = lookup_style(preview_style) @@ -185,18 +175,14 @@ class SiteCustomization < ActiveRecord::Base return "" unless stylesheet.present? return @stylesheet_link_tag if @stylesheet_link_tag ensure_stylesheets_on_disk! - @stylesheet_link_tag = "" + @stylesheet_link_tag = "" end def mobile_stylesheet_link_tag return "" unless mobile_stylesheet.present? return @mobile_stylesheet_link_tag if @mobile_stylesheet_link_tag ensure_stylesheets_on_disk! - @mobile_stylesheet_link_tag = "" - end - - def stylesheet_relative_path(target=:desktop) - "/#{CACHE_PATH}#{stylesheet_filename(target)}?#{stylesheet_hash(target)}" + @mobile_stylesheet_link_tag = "" end end diff --git a/app/views/common/_discourse_stylesheet.html.erb b/app/views/common/_discourse_stylesheet.html.erb index afdab8627..41bff2413 100644 --- a/app/views/common/_discourse_stylesheet.html.erb +++ b/app/views/common/_discourse_stylesheet.html.erb @@ -1,42 +1,15 @@ <%- unless SiteCustomization.override_default_style(session[:preview_style]) %> - - - + <% if mobile_view? %> + <%= stylesheet_link_tag "mobile" %> + <% else %> + <%= stylesheet_link_tag "desktop" %> + <% end %> <%- end %> - - <%- if staff? %> <%= stylesheet_link_tag "admin"%> <%-end%> + +<%- unless customization_disabled? %> + <%= SiteCustomization.custom_stylesheet(session[:preview_style], mobile_view? ? :mobile : :desktop) %> +<%- end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 3e6fafac3..0b3a0f7f7 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,5 +1,5 @@ - +