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:
Robin Ward 2014-04-29 18:01:13 -04:00
parent 4d3effa686
commit ad1a8db956
5 changed files with 70 additions and 59 deletions

View File

@ -2,38 +2,12 @@
This controller supports displaying static content.
@class StaticController
@extends Discourse.Controller
@extends Em.ObjectController
@namespace Discourse
@module Discourse
**/
Discourse.StaticController = Discourse.Controller.extend({
needs: ['header'],
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 = Em.ObjectController.extend({
showLoginButton: Em.computed.equal('path', 'login')
});
Discourse.StaticController.reopenClass({

View File

@ -241,7 +241,26 @@ Discourse.URL = Em.Object.createWithMixins({
handleURL: function(path) {
var router = this.get('router');
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);
}
});
}
});
}
});

View 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}));
});
}
});
}
});

View File

@ -6,35 +6,27 @@
@namespace Discourse
@module Discourse
**/
_.each(Discourse.StaticController.PAGES, function(page) {
Discourse.StaticController.PAGES.forEach(function(page) {
Discourse[page.capitalize() + "Route"] = Discourse.Route.extend({
renderTemplate: function() {
this.render('static');
},
setupController: function() {
var config_key = Discourse.StaticController.CONFIGS[page];
if (config_key && Discourse.SiteSettings[config_key].length > 0) {
Discourse.URL.redirectTo(Discourse.SiteSettings[config_key]);
} else {
this.controllerFor('static').loadPath("/" + page);
beforeModel: function(transition) {
var configKey = Discourse.StaticController.CONFIGS[page];
if (configKey && Discourse.SiteSettings[configKey].length > 0) {
transition.abort();
Discourse.URL.redirectTo(Discourse.SiteSettings[configKey]);
}
}
},
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');
});
});
}
}
});

View File

@ -1,13 +1,9 @@
<div class='container'>
<div class='contents clearfix body-page'>
{{#if content}}
{{{content}}}
{{{html}}}
{{#if showLoginButton}}
<button class="btn btn-primary" {{action showLogin}}>{{i18n log_in}}</button>
{{/if}}
{{else}}
<div class='spinner'>{{i18n loading}}</div>
{{#if showLoginButton}}
<button class="btn btn-primary" {{action showLogin}}>{{i18n log_in}}</button>
{{/if}}
</div>
</div>