diff --git a/app/assets/javascripts/discourse/models/topic.js.es6 b/app/assets/javascripts/discourse/models/topic.js.es6 index f47342d7665..d52ffafacd6 100644 --- a/app/assets/javascripts/discourse/models/topic.js.es6 +++ b/app/assets/javascripts/discourse/models/topic.js.es6 @@ -134,6 +134,10 @@ const Topic = RestModel.extend({ return this.get('url') + (user ? '?u=' + user.get('username_lower') : ''); }.property('url'), + printUrl: function(){ + return this.get('url') + '/print'; + }.property('url'), + url: function() { let slug = this.get('slug') || ''; if (slug.trim().length === 0) { diff --git a/app/assets/javascripts/discourse/views/print-button.js.es6 b/app/assets/javascripts/discourse/views/print-button.js.es6 new file mode 100644 index 00000000000..6a64845f806 --- /dev/null +++ b/app/assets/javascripts/discourse/views/print-button.js.es6 @@ -0,0 +1,16 @@ +import ButtonView from 'discourse/views/button'; +import { iconHTML } from 'discourse/helpers/fa-icon'; + +export default ButtonView.extend({ + classNames: ['print'], + textKey: 'topic.print.title', + helpKey: 'topic.print.help', + + renderIcon(buffer) { + buffer.push(iconHTML("print")); + }, + + click() { + window.open(this.get('controller.model.printUrl'), '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,width=600,height=315'); + } +}); diff --git a/app/assets/javascripts/discourse/views/topic-footer-main-buttons.js.es6 b/app/assets/javascripts/discourse/views/topic-footer-main-buttons.js.es6 index b28b1e9c34c..3f2748a5e8e 100644 --- a/app/assets/javascripts/discourse/views/topic-footer-main-buttons.js.es6 +++ b/app/assets/javascripts/discourse/views/topic-footer-main-buttons.js.es6 @@ -32,6 +32,8 @@ export default ContainerView.extend({ this.attachViewClass('invite-reply-button'); } + this.attachViewClass('print-button'); + if (topic.get('isPrivateMessage')) { this.attachViewClass('archive-button'); } diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ed040589e28..293121957a0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -53,7 +53,7 @@ class ApplicationController < ActionController::Base end def use_crawler_layout? - @use_crawler_layout ||= (has_escaped_fragment? || CrawlerDetection.crawler?(request.user_agent)) + @use_crawler_layout ||= (has_escaped_fragment? || CrawlerDetection.crawler?(request.user_agent) || params.key?("print")) end def add_readonly_header diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index d890410469a..88bd54d758c 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -58,6 +58,7 @@ class TopicsController < ApplicationController username_filters = opts[:username_filters] opts[:slow_platform] = true if slow_platform? + opts[:print] = true if params[:print].present? opts[:username_filters] = username_filters.split(',') if username_filters.is_a?(String) # Special case: a slug with a number in front should look by slug first before looking diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index cb25065a0fd..9d1b89442d2 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1459,6 +1459,10 @@ en: title: 'Share' help: 'share a link to this topic' + print: + title: 'Print' + help: 'Open a printer friendly version of this topic' + flag_topic: title: 'Flag' help: 'privately flag this topic for attention or send a private notification about it' diff --git a/config/routes.rb b/config/routes.rb index af169723dd0..f0dca891665 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -554,6 +554,7 @@ Discourse::Application.routes.draw do # Topic routes get "t/id_for/:slug" => "topics#id_for_slug" + get "t/:slug/:topic_id/print" => "topics#show", format: :html, print: true, constraints: {topic_id: /\d+/} get "t/:slug/:topic_id/wordpress" => "topics#wordpress", constraints: {topic_id: /\d+/} get "t/:topic_id/wordpress" => "topics#wordpress", constraints: {topic_id: /\d+/} get "t/:slug/:topic_id/moderator-liked" => "topics#moderator_liked", constraints: {topic_id: /\d+/} diff --git a/lib/topic_view.rb b/lib/topic_view.rb index f69d3e47167..d854c09ac7e 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -12,6 +12,10 @@ class TopicView 10 end + def self.print_chunk_size + 1000 + end + def self.chunk_size 20 end @@ -44,7 +48,11 @@ class TopicView end @page = 1 if (!@page || @page.zero?) - @chunk_size = options[:slow_platform] ? TopicView.slow_chunk_size : TopicView.chunk_size + @chunk_size = case + when options[:slow_platform] then TopicView.slow_chunk_size + when options[:print] then 1000 + else TopicView.chunk_size + end @limit ||= @chunk_size setup_filtered_posts