Skip to content

Commit

Permalink
Add ability to customize css and header for mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
nlalonde committed Sep 16, 2013
1 parent c9ebf23 commit 13f17b2
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 72 deletions.
6 changes: 4 additions & 2 deletions app/assets/javascripts/admin/models/site_customization.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@module Discourse
**/
Discourse.SiteCustomization = Discourse.Model.extend({
trackedProperties: ['enabled', 'name', 'stylesheet', 'header', 'override_default_style'],
trackedProperties: ['enabled', 'name', 'stylesheet', 'header', 'mobile_stylesheet', 'mobile_header', 'override_default_style'],

init: function() {
this._super();
Expand All @@ -33,7 +33,7 @@ Discourse.SiteCustomization = Discourse.Model.extend({

return changed;

}.property('override_default_style', 'enabled', 'name', 'stylesheet', 'header', 'originals'),
}.property('override_default_style', 'enabled', 'name', 'stylesheet', 'header', 'mobile_stylesheet', 'mobile_header', 'originals'),

startTrackingChanges: function() {
var _this = this;
Expand Down Expand Up @@ -62,6 +62,8 @@ Discourse.SiteCustomization = Discourse.Model.extend({
enabled: this.enabled,
stylesheet: this.stylesheet,
header: this.header,
mobile_stylesheet: this.mobile_stylesheet,
mobile_header: this.mobile_header,
override_default_style: this.override_default_style
};

Expand Down
35 changes: 24 additions & 11 deletions app/assets/javascripts/admin/templates/customize.js.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,38 @@

{{#if selectedItem}}
<div class='current-style'>
<div class='admin-controls'>
<ul class="nav nav-pills">
<li {{bindAttr class="view.stylesheetActive:active"}}>
<a {{action selectStylesheet href="true" target="view"}}>{{i18n admin.customize.css}}</a>
</li>
<li {{bindAttr class="view.headerActive:active"}}>
<a {{action selectHeader href="true" target="view"}}>{{i18n admin.customize.header}}</a>
</li>
</ul>
</div>

{{#with selectedItem}}
{{textField class="style-name" value=name}}

<div class='admin-controls'>
<ul class="nav nav-pills">
<li {{bindAttr class="view.stylesheetActive:active"}}>
<a {{action selectStylesheet href="true" target="view"}}>{{i18n admin.customize.css}}</a>
</li>
<li {{bindAttr class="view.headerActive:active"}}>
<a {{action selectHeader href="true" target="view"}}>{{i18n admin.customize.header}}</a>
</li>
<li {{bindAttr class="view.mobileStylesheetActive:active"}}>
<a {{action selectMobileStylesheet href="true" target="view"}}>{{i18n admin.customize.mobile_css}}</a>
</li>
<li {{bindAttr class="view.mobileHeaderActive:active"}}>
<a {{action selectMobileHeader href="true" target="view"}}>{{i18n admin.customize.mobile_header}}</a>
</li>
</ul>
</div>

{{#if view.headerActive}}
{{aceEditor content=header mode="html"}}
{{/if}}
{{#if view.stylesheetActive}}
{{aceEditor content=stylesheet mode="scss"}}
{{/if}}
{{#if view.mobileHeaderActive}}
{{aceEditor content=mobile_header mode="html"}}
{{/if}}
{{#if view.mobileStylesheetActive}}
{{aceEditor content=mobile_stylesheet mode="scss"}}
{{/if}}
{{/with}}
<br>
<div class='status-actions'>
Expand Down
20 changes: 12 additions & 8 deletions app/assets/javascripts/admin/views/admin_customize_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,16 @@
Discourse.AdminCustomizeView = Discourse.View.extend({
templateName: 'admin/templates/customize',
classNames: ['customize'],
headerActive: Ember.computed.equal('selected', 'header'),
stylesheetActive: Ember.computed.equal('selected', 'stylesheet'),
mobileHeaderActive: Ember.computed.equal('selected', 'mobileHeader'),
mobileStylesheetActive: Ember.computed.equal('selected', 'mobileStylesheet'),

init: function() {
this._super();
this.set('selected', 'stylesheet');
},

headerActive: (function() {
return this.get('selected') === 'header';
}).property('selected'),

stylesheetActive: (function() {
return this.get('selected') === 'stylesheet';
}).property('selected'),

selectHeader: function() {
this.set('selected', 'header');
},
Expand All @@ -33,6 +29,14 @@ Discourse.AdminCustomizeView = Discourse.View.extend({
this.set('selected', 'stylesheet');
},

selectMobileHeader: function() {
this.set('selected', 'mobileHeader');
},

selectMobileStylesheet: function() {
this.set('selected', 'mobileStylesheet');
},

didInsertElement: function() {
var controller = this.get('controller');
return Mousetrap.bindGlobal(['meta+s', 'ctrl+s'], function() {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/site_customizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def destroy
private

def site_customization_params
params.require(:site_customization).permit(:name, :stylesheet, :header, :position, :enabled, :key, :override_default_style, :stylesheet_baked)
params.require(:site_customization).permit(:name, :stylesheet, :header, :mobile_stylesheet, :mobile_header, :position, :enabled, :key, :override_default_style, :stylesheet_baked)
end

def log_site_customization_change(old_record, new_params)
Expand Down
93 changes: 55 additions & 38 deletions app/models/site_customization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,35 @@ class SiteCustomization < ActiveRecord::Base
end

before_save do
if stylesheet_changed?
begin
self.stylesheet_baked = Sass.compile stylesheet
rescue Sass::SyntaxError => e
error = e.sass_backtrace_str("custom stylesheet")
error.gsub!("\n", '\A ')
error.gsub!("'", '\27 ')

self.stylesheet_baked =
"#main {display: none;}
footer {white-space: pre; margin-left: 100px;}
footer:after{ content: '#{error}' }"
['stylesheet', 'mobile_stylesheet'].each do |stylesheet_attr|
if self.send("#{stylesheet_attr}_changed?")
begin
self.send("#{stylesheet_attr}_baked=", Sass.compile(self.send(stylesheet_attr)))
rescue Sass::SyntaxError => e
error = e.sass_backtrace_str("custom stylesheet")
error.gsub!("\n", '\A ')
error.gsub!("'", '\27 ')

self.send("#{stylesheet_attr}_baked=",
"#main {display: none;}
footer {white-space: pre; margin-left: 100px;}
footer:after{ content: '#{error}' }")
end
end
end
end

after_save do
if stylesheet_changed?
if File.exists?(stylesheet_fullpath)
File.delete stylesheet_fullpath
end
File.delete(stylesheet_fullpath) if File.exists?(stylesheet_fullpath)
end
if mobile_stylesheet_changed?
File.delete(stylesheet_fullpath(:mobile)) if File.exists?(stylesheet_fullpath(:mobile))
end
remove_from_cache!
if stylesheet_changed?
ensure_stylesheet_on_disk!
if stylesheet_changed? or mobile_stylesheet_changed?
ensure_stylesheets_on_disk!
# TODO: this is broken now because there's mobile stuff too
MessageBus.publish "/file-change/#{key}", stylesheet_hash
end
MessageBus.publish "/header-change/#{key}", header if header_changed?
Expand All @@ -47,6 +51,9 @@ class SiteCustomization < ActiveRecord::Base
if File.exists?(stylesheet_fullpath)
File.delete stylesheet_fullpath
end
if File.exists?(stylesheet_fullpath(:mobile))
File.delete stylesheet_fullpath(:mobile)
end
self.remove_from_cache!
end

Expand All @@ -71,17 +78,17 @@ def self.enabled_style_key
end
end

def self.custom_stylesheet(preview_style)
def self.custom_stylesheet(preview_style, target=:desktop)
preview_style ||= enabled_style_key
style = lookup_style(preview_style)
style.stylesheet_link_tag.html_safe if style
style.stylesheet_link_tag(target).html_safe if style
end

def self.custom_header(preview_style)
def self.custom_header(preview_style, target=:desktop)
preview_style ||= enabled_style_key
style = lookup_style(preview_style)
if style && style.header
style.header.html_safe
if style && ((target == :mobile && style.mobile_header) || style.header)
target == :mobile ? style.mobile_header.html_safe : style.header.html_safe
else
""
end
Expand All @@ -104,7 +111,7 @@ def self.lookup_style(key)

@lock.synchronize do
style = where(key: key).first
style.ensure_stylesheet_on_disk! if style
style.ensure_stylesheets_on_disk! if style
@cache[key] = style
end
end
Expand Down Expand Up @@ -135,39 +142,49 @@ def remove_from_cache!
self.class.remove_from_cache!(key)
end

def stylesheet_hash
Digest::MD5.hexdigest(stylesheet)
def stylesheet_hash(target=:desktop)
Digest::MD5.hexdigest( target == :mobile ? mobile_stylesheet : stylesheet )
end

def cache_fullpath
"#{Rails.root}/public/#{CACHE_PATH}"
end

def ensure_stylesheet_on_disk!
path = stylesheet_fullpath
dir = cache_fullpath
FileUtils.mkdir_p(dir)
unless File.exists?(path)
File.open(path, "w") do |f|
f.puts stylesheet_baked
def ensure_stylesheets_on_disk!
[[:desktop, 'stylesheet_baked'], [:mobile, 'mobile_stylesheet_baked']].each do |target, baked_attr|
path = stylesheet_fullpath(target)
dir = cache_fullpath
FileUtils.mkdir_p(dir)
unless File.exists?(path)
File.open(path, "w") do |f|
f.puts self.send(baked_attr)
end
end
end
end

def stylesheet_filename
"/#{self.key}.css"
def stylesheet_filename(target=:desktop)
target == :desktop ? "/#{self.key}.css" : "/#{target}_#{self.key}.css"
end

def stylesheet_fullpath
"#{cache_fullpath}#{stylesheet_filename}"
def stylesheet_fullpath(target=:desktop)
"#{cache_fullpath}#{stylesheet_filename(target)}"
end

def stylesheet_link_tag
def stylesheet_link_tag(target=:desktop)
return mobile_stylesheet_link_tag if target == :mobile
return "" unless stylesheet.present?
return @stylesheet_link_tag if @stylesheet_link_tag
ensure_stylesheet_on_disk!
ensure_stylesheets_on_disk!
@stylesheet_link_tag = "<link class=\"custom-css\" rel=\"stylesheet\" href=\"/#{CACHE_PATH}#{stylesheet_filename}?#{stylesheet_hash}\" type=\"text/css\" media=\"screen\">"
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 = "<link class=\"custom-css\" rel=\"stylesheet\" href=\"/#{CACHE_PATH}#{stylesheet_filename(:mobile)}?#{stylesheet_hash(:mobile)}\" type=\"text/css\" media=\"screen\">"
end
end

# == Schema Information
Expand Down
4 changes: 1 addition & 3 deletions app/views/common/_discourse_stylesheet.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@
<%= stylesheet_link_tag "admin"%>
<%-end%>

<% unless mobile_view? %>
<%= SiteCustomization.custom_stylesheet(session[:preview_style]) %>
<% end %>
<%= SiteCustomization.custom_stylesheet(session[:preview_style], mobile_view? ? :mobile : :desktop) %>
4 changes: 1 addition & 3 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
<body>
<!--[if IE 9]><script type="text/javascript">ie = "new";</script><![endif]-->

<% unless mobile_view? %>
<%= SiteCustomization.custom_header(session[:preview_style]) %>
<% end %>
<%= SiteCustomization.custom_header(session[:preview_style], mobile_view? ? :mobile : :desktop) %>
<section id='main'>
</section>

Expand Down
2 changes: 2 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,8 @@ en:
long_title: "Site Customizations"
header: "Header"
css: "Stylesheet"
mobile_header: "Mobile Header"
mobile_css: "Mobile Stylesheet"
override_default: "Do not include standard style sheet"
enabled: "Enabled?"
preview: "preview"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddMobileToSiteCustomizations < ActiveRecord::Migration
def change
add_column :site_customizations, :mobile_stylesheet, :text
add_column :site_customizations, :mobile_header, :text
add_column :site_customizations, :mobile_stylesheet_baked, :text
end
end
Loading

0 comments on commit 13f17b2

Please sign in to comment.