mirror of
https://github.com/discourse/discourse.git
synced 2025-05-10 06:13:06 +08:00
Support for linking to static pages with hash URLs like #section
. Also
refactor of static code to be more idomatic.
This commit is contained in:
parent
4d3effa686
commit
ad1a8db956
@ -2,38 +2,12 @@
|
|||||||
This controller supports displaying static content.
|
This controller supports displaying static content.
|
||||||
|
|
||||||
@class StaticController
|
@class StaticController
|
||||||
@extends Discourse.Controller
|
@extends Em.ObjectController
|
||||||
@namespace Discourse
|
@namespace Discourse
|
||||||
@module Discourse
|
@module Discourse
|
||||||
**/
|
**/
|
||||||
Discourse.StaticController = Discourse.Controller.extend({
|
Discourse.StaticController = Em.ObjectController.extend({
|
||||||
needs: ['header'],
|
showLoginButton: Em.computed.equal('path', 'login')
|
||||||
path: null,
|
|
||||||
|
|
||||||
showLoginButton: function() {
|
|
||||||
return this.get('path') === '/login';
|
|
||||||
}.property('path'),
|
|
||||||
|
|
||||||
loadPath: function(path) {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
this.setProperties({
|
|
||||||
path: path,
|
|
||||||
content: null
|
|
||||||
});
|
|
||||||
|
|
||||||
// Load from <noscript> if we have it.
|
|
||||||
var $preloaded = $("noscript[data-path=\"" + path + "\"]");
|
|
||||||
if ($preloaded.length) {
|
|
||||||
var text = $preloaded.text();
|
|
||||||
text = text.match(/<!-- preload-content: -->((?:.|[\n\r])*)<!-- :preload-content -->/)[1];
|
|
||||||
this.set('content', text);
|
|
||||||
} else {
|
|
||||||
return Discourse.ajax(path + ".html", {dataType: 'html'}).then(function (result) {
|
|
||||||
self.set('content', result);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Discourse.StaticController.reopenClass({
|
Discourse.StaticController.reopenClass({
|
||||||
|
@ -241,7 +241,26 @@ Discourse.URL = Em.Object.createWithMixins({
|
|||||||
handleURL: function(path) {
|
handleURL: function(path) {
|
||||||
var router = this.get('router');
|
var router = this.get('router');
|
||||||
router.router.updateURL(path);
|
router.router.updateURL(path);
|
||||||
return router.handleURL(path);
|
|
||||||
|
var split = path.split('#'),
|
||||||
|
elementId;
|
||||||
|
|
||||||
|
if (split.length === 2) {
|
||||||
|
path = split[0];
|
||||||
|
elementId = split[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
var transition = router.handleURL(path);
|
||||||
|
transition.promise.then(function() {
|
||||||
|
if (elementId) {
|
||||||
|
Em.run.next('afterRender', function() {
|
||||||
|
var offset = $('#' + elementId).offset();
|
||||||
|
if (offset && offset.top) {
|
||||||
|
$('html, body').scrollTop(offset.top - $('header').height() - 10);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
30
app/assets/javascripts/discourse/models/static_page.js
Normal file
30
app/assets/javascripts/discourse/models/static_page.js
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/**
|
||||||
|
A model that repreesnts a static page in Discourse
|
||||||
|
|
||||||
|
@class StaticPage
|
||||||
|
@extends Em.Object
|
||||||
|
@namespace Discourse
|
||||||
|
@module Discourse
|
||||||
|
**/
|
||||||
|
Discourse.StaticPage = Em.Object.extend({
|
||||||
|
});
|
||||||
|
|
||||||
|
Discourse.StaticPage.reopenClass({
|
||||||
|
find: function(path) {
|
||||||
|
return new Em.RSVP.Promise(function(resolve) {
|
||||||
|
// Models shouldn't really be doing Ajax request, but this is a huge speed boost if we
|
||||||
|
// preload content.
|
||||||
|
var $preloaded = $("noscript[data-path=\"/" + path + "\"]");
|
||||||
|
if ($preloaded.length) {
|
||||||
|
var text = $preloaded.text();
|
||||||
|
text = text.match(/<!-- preload-content: -->((?:.|[\n\r])*)<!-- :preload-content -->/)[1];
|
||||||
|
resolve(Discourse.StaticPage.create({path: path, html: text}));
|
||||||
|
} else {
|
||||||
|
Discourse.ajax(path + ".html", {dataType: 'html'}).then(function (result) {
|
||||||
|
resolve(Discourse.StaticPage.create({path: path, html: result}));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
@ -6,35 +6,27 @@
|
|||||||
@namespace Discourse
|
@namespace Discourse
|
||||||
@module Discourse
|
@module Discourse
|
||||||
**/
|
**/
|
||||||
_.each(Discourse.StaticController.PAGES, function(page) {
|
Discourse.StaticController.PAGES.forEach(function(page) {
|
||||||
|
|
||||||
Discourse[page.capitalize() + "Route"] = Discourse.Route.extend({
|
Discourse[page.capitalize() + "Route"] = Discourse.Route.extend({
|
||||||
|
|
||||||
renderTemplate: function() {
|
renderTemplate: function() {
|
||||||
this.render('static');
|
this.render('static');
|
||||||
},
|
},
|
||||||
|
|
||||||
setupController: function() {
|
beforeModel: function(transition) {
|
||||||
var config_key = Discourse.StaticController.CONFIGS[page];
|
var configKey = Discourse.StaticController.CONFIGS[page];
|
||||||
if (config_key && Discourse.SiteSettings[config_key].length > 0) {
|
if (configKey && Discourse.SiteSettings[configKey].length > 0) {
|
||||||
Discourse.URL.redirectTo(Discourse.SiteSettings[config_key]);
|
transition.abort();
|
||||||
} else {
|
Discourse.URL.redirectTo(Discourse.SiteSettings[configKey]);
|
||||||
this.controllerFor('static').loadPath("/" + page);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
});
|
model: function() {
|
||||||
|
return Discourse.StaticPage.find(page);
|
||||||
|
},
|
||||||
|
|
||||||
});
|
setupController: function(controller, model) {
|
||||||
|
this.controllerFor('static').set('model', model);
|
||||||
Discourse.LoginRoute.reopen({
|
|
||||||
beforeModel: function() {
|
|
||||||
if (!Discourse.SiteSettings.login_required) {
|
|
||||||
this.transitionTo('discovery.latest').then(function(e) {
|
|
||||||
Ember.run.next(function() {
|
|
||||||
e.send('showLogin');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
@ -1,13 +1,9 @@
|
|||||||
<div class='container'>
|
<div class='container'>
|
||||||
<div class='contents clearfix body-page'>
|
<div class='contents clearfix body-page'>
|
||||||
{{#if content}}
|
{{{html}}}
|
||||||
{{{content}}}
|
|
||||||
|
|
||||||
{{#if showLoginButton}}
|
{{#if showLoginButton}}
|
||||||
<button class="btn btn-primary" {{action showLogin}}>{{i18n log_in}}</button>
|
<button class="btn btn-primary" {{action showLogin}}>{{i18n log_in}}</button>
|
||||||
{{/if}}
|
{{/if}}
|
||||||
{{else}}
|
|
||||||
<div class='spinner'>{{i18n loading}}</div>
|
|
||||||
{{/if}}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user