From 1f0a59584b60eef6b4a95ce35566819e3bfe1620 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 18 Dec 2013 14:47:22 -0500 Subject: [PATCH] Revert "Re-apply with fixes: Stop using user agent to detect mobile devices. Use a media query and yepnope to load the appropriate css and customizations." --- .../javascripts/discourse/lib/mobile.js | 17 ++++- app/controllers/application_controller.rb | 5 ++ app/helpers/application_helper.rb | 18 ++++- app/models/site_customization.rb | 18 +---- .../common/_discourse_stylesheet.html.erb | 45 +++-------- app/views/layouts/application.html.erb | 61 +++++++-------- spec/helpers/application_helper_spec.rb | 74 +++++++++++++++++++ spec/models/site_customization_spec.rb | 19 ----- .../assets/javascripts/browser-update.js.erb | 9 +-- 9 files changed, 148 insertions(+), 118 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/mobile.js b/app/assets/javascripts/discourse/lib/mobile.js index 8a0876bf97f..c1b904970fe 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 5e6111101fe..91c2aa2cbe9 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 b4309482ade..3ad0ece5e9b 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 d5d0105956d..f46ea5e592a 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 afdab8627ef..41bff2413d1 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 3e6fafac3cb..0b3a0f7f798 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,5 +1,5 @@ - + <%= content_for?(:title) ? yield(:title) + ' - ' + SiteSetting.title : SiteSetting.title %> @@ -20,55 +20,48 @@ <%= javascript_include_tag "admin"%> <%- end %> - <%= render :partial => "common/discourse_stylesheet" %> <%= render :partial => "common/special_font_face" %> + <%= render :partial => "common/discourse_stylesheet" %> <%= discourse_csrf_tags %> <%= yield :head %> - + - + <%= render_google_analytics_code %>