Remove `Discourse.Mobile` constants

This commit is contained in:
Robin Ward 2016-02-18 11:53:25 -05:00
parent ef75f87c8b
commit 849c99f83d
31 changed files with 205 additions and 185 deletions

View File

@ -12,8 +12,8 @@ export default Ember.Component.extend({
return !c.get('parentCategory');
}),
hidden: function(){
return Discourse.Mobile.mobileView && !this.get('category');
hidden: function() {
return this.site.mobileView && !this.get('category');
}.property('category'),
firstCategory: function() {

View File

@ -18,7 +18,7 @@ export default Ember.Component.extend({
@on('init')
_setupPreview() {
const val = (Discourse.Mobile.mobileView ? false : (this.keyValueStore.get('composer.showPreview') || 'true'));
const val = (this.site.mobileView ? false : (this.keyValueStore.get('composer.showPreview') || 'true'));
this.set('showPreview', val === 'true');
},
@ -218,7 +218,7 @@ export default Ember.Component.extend({
}
});
if (Discourse.Mobile.mobileView) {
if (this.site.mobileView) {
this.$(".mobile-file-upload").on("click.uploader", function () {
// redirect the click on the hidden file input
$("#mobile-uploader").click();

View File

@ -25,7 +25,9 @@ const OP = {
const _createCallbacks = [];
function Toolbar() {
class Toolbar {
constructor(site) {
this.shortcuts = {};
this.groups = [
@ -106,7 +108,7 @@ function Toolbar() {
perform: e => e.addText("\n\n----------\n")
});
if (Discourse.Mobile.mobileView) {
if (site.mobileView) {
this.groups.push({group: 'mobileExtras', buttons: []});
this.addButton({
@ -119,9 +121,9 @@ function Toolbar() {
}
this.groups[this.groups.length-1].lastGroup = true;
};
}
Toolbar.prototype.addButton = function(button) {
addButton(button) {
const g = this.groups.findProperty('group', button.group);
if (!g) {
throw `Couldn't find toolbar group ${button.group}`;
@ -171,7 +173,8 @@ Toolbar.prototype.addButton = function(button) {
} else {
g.buttons.push(createdButton);
}
};
}
}
export function onToolbarCreate(func) {
_createCallbacks.push(func);
@ -237,7 +240,7 @@ export default Ember.Component.extend({
@computed
toolbar() {
const toolbar = new Toolbar();
const toolbar = new Toolbar(this.site);
_createCallbacks.forEach(cb => cb(toolbar));
this.sendAction('extraButtons', toolbar);
return toolbar;

View File

@ -10,17 +10,17 @@ export default Ember.Component.extend({
@computed()
showKeyboardShortcuts() {
return !Discourse.Mobile.mobileView && !this.capabilities.touch;
return !this.site.mobileView && !this.capabilities.touch;
},
@computed()
showMobileToggle() {
return Discourse.Mobile.mobileView || (this.siteSettings.enable_mobile_theme && this.capabilities.touch);
return this.site.mobileView || (this.siteSettings.enable_mobile_theme && this.capabilities.touch);
},
@computed()
mobileViewLinkTextKey() {
return Discourse.Mobile.mobileView ? "desktop_view" : "mobile_view";
return this.site.mobileView ? "desktop_view" : "mobile_view";
},
@computed()
@ -68,7 +68,7 @@ export default Ember.Component.extend({
this.sendAction('showKeyboardAction');
},
toggleMobileView() {
Discourse.Mobile.toggleMobileView();
this.site.toggleMobileView();
}
}
});

View File

@ -17,7 +17,7 @@ export default Ember.Component.extend({
if (this.siteSettings.login_required && !this.currentUser) {
this.sendAction('loginAction');
} else {
if (Discourse.Mobile.mobileView && this.get('mobileAction')) {
if (this.site.mobileView && this.get('mobileAction')) {
this.sendAction('mobileAction');
return;
}

View File

@ -14,11 +14,11 @@ export default Ember.Component.extend({
}.property('targetUrl'),
showSmallLogo: function() {
return !Discourse.Mobile.mobileView && this.get("minimized");
return !this.site.mobileView && this.get("minimized");
}.property("minimized"),
showMobileLogo: function() {
return Discourse.Mobile.mobileView && !Ember.isBlank(this.get('mobileBigLogoUrl'));
return this.site.mobileView && !Ember.isBlank(this.get('mobileBigLogoUrl'));
}.property(),
smallLogoUrl: setting('logo_small_url'),

View File

@ -120,17 +120,17 @@ export default Ember.Component.extend({
@computed()
showKeyboardShortcuts() {
return !Discourse.Mobile.mobileView && !this.capabilities.touch;
return !this.site.mobileView && !this.capabilities.touch;
},
@computed()
showMobileToggle() {
return Discourse.Mobile.mobileView || (this.siteSettings.enable_mobile_theme && this.capabilities.touch);
return this.site.mobileView || (this.siteSettings.enable_mobile_theme && this.capabilities.touch);
},
@computed()
mobileViewLinkTextKey() {
return Discourse.Mobile.mobileView ? "desktop_view" : "mobile_view";
return this.site.mobileView ? "desktop_view" : "mobile_view";
},
@computed()

View File

@ -19,7 +19,7 @@ export default Ember.Component.extend({
}.property(),
skipHeader: function() {
return Discourse.Mobile.mobileView;
return this.site.mobileView;
}.property(),
showLikes: function(){

View File

@ -10,7 +10,7 @@ export default Ember.Controller.extend(ModalFunctionality, {
revisionsTextKey: "post.revisions.controls.comparing_previous_to_current_out_of_total",
_changeViewModeOnMobile: function() {
if (Discourse.Mobile.mobileView) { this.set("viewMode", "inline"); }
if (this.site.mobileView) { this.set("viewMode", "inline"); }
}.on("init"),
refresh(postId, postVersion) {

View File

@ -61,8 +61,10 @@ export default Ember.Controller.extend({
// containing a single invisible character
markerElement.appendChild(document.createTextNode("\ufeff"));
const isMobileDevice = this.site.isMobileDevice;
// collapse the range at the beginning/end of the selection
range.collapse(!Discourse.Mobile.isMobileDevice);
range.collapse(!isMobileDevice);
// and insert it at the start of our selection range
range.insertNode(markerElement);
@ -83,7 +85,7 @@ export default Ember.Controller.extend({
let topOff = markerOffset.top;
let leftOff = markerOffset.left;
if (Discourse.Mobile.isMobileDevice) {
if (isMobileDevice) {
topOff = topOff + 20;
leftOff = Math.min(leftOff + 10, $(window).width() - $quoteButton.outerWidth());
} else {

View File

@ -45,7 +45,7 @@ export default Ember.Controller.extend({
}
// Don't show on mobile
if (Discourse.Mobile.mobileView) {
if (this.site.mobileView) {
const url = "/users/" + username;
DiscourseURL.routeTo(url);
return;

View File

@ -12,7 +12,7 @@ export default Ember.Controller.extend({
bulkSelectEnabled: Em.computed.alias('controllers.user-topics-list.bulkSelectEnabled'),
mobileView: function() {
return Discourse.Mobile.mobileView;
return this.site.mobileView;
}.property(),
showNewPM: function(){

View File

@ -141,7 +141,7 @@ export default Ember.DefaultResolver.extend({
},
findMobileTemplate(parsedName) {
if (Discourse.Mobile.mobileView) {
if (this.mobileView) {
var mobileParsedName = this.parseName(parsedName.fullName.replace("template:", "template:mobile/"));
return this.findTemplate(mobileParsedName);
}

View File

@ -1,7 +1,7 @@
export default {
name: 'ensure-image-dimensions',
after: 'mobile',
initialize: function() {
initialize(container) {
if (!window) { return; }
// This enforces maximum dimensions of images based on site settings
@ -11,7 +11,8 @@ export default {
var width = Discourse.SiteSettings.max_image_width;
var height = Discourse.SiteSettings.max_image_height;
if (Discourse.Mobile.mobileView) {
const site = container.lookup('site:main');
if (site.mobileView) {
width = $(window).width() - 20;
}

View File

@ -1,14 +1,19 @@
/**
Initializes the `Discourse.Mobile` helper object.
**/
import Mobile from 'discourse/lib/mobile';
// Initializes the `Mobile` helper object.
export default {
name: 'mobile',
after: 'inject-objects',
initialize: function(container) {
Discourse.Mobile.init();
var site = container.lookup('site:main');
site.set('mobileView', Discourse.Mobile.mobileView);
initialize(container, app) {
Mobile.init();
const site = container.lookup('site:main');
site.set('mobileView', Mobile.mobileView);
site.set('isMobileDevice', Mobile.isMobileDevice);
// This is a bit weird but you can't seem to inject into the resolver?
app.registry.resolver.__resolver__.mobileView = Mobile.mobileView;
}
};

View File

@ -96,7 +96,7 @@ export default {
});
if (!Ember.testing) {
if (!Discourse.Mobile.mobileView) {
if (!site.mobileView) {
bus.subscribe("/notification-alert/" + user.get('id'), function(data){
onNotification(data, user);
});

View File

@ -3,7 +3,6 @@
@module $.fn.autocomplete
**/
export var CANCELLED_STATUS = "__CANCELLED";
const allowedLettersRegex = /[\s\t\[\{\(\/]/;
@ -226,7 +225,7 @@ export default function(options) {
vOffset = div.height();
}
if (Discourse.Mobile.mobileView && !isInput) {
if (Discourse.Site.currentProp('mobileView') && !isInput) {
div.css('width', 'auto');
if ((me.height() / 2) >= pos.top) { vOffset = -23; }

View File

@ -161,7 +161,7 @@ function showSelector(options) {
options.appendTo.append('<div class="emoji-modal-wrapper"></div>');
$('.emoji-modal-wrapper').click(() => closeSelector());
if (Discourse.Mobile.mobileView) PER_ROW = 9;
if (Discourse.Site.currentProp('mobileView')) { PER_ROW = 9; }
const page = keyValueStore.getInt("emojiPage", 0);
const offset = keyValueStore.getInt("emojiOffset", 0);

View File

@ -1,10 +1,10 @@
// An object that is responsible for logic related to mobile devices.
Discourse.Mobile = {
const Mobile = {
isMobileDevice: false,
mobileView: false,
init: function() {
var $html = $('html');
init() {
const $html = $('html');
this.isMobileDevice = $html.hasClass('mobile-device');
this.mobileView = $html.hasClass('mobile-view');
@ -42,3 +42,13 @@ Discourse.Mobile = {
window.location.assign(window.location.pathname + '?mobile_view=' + (mobile ? '1' : '0'));
}
};
// Backwards compatibiltity, deprecated
Object.defineProperty(Discourse, 'Mobile', {
get: function() {
Ember.warn("DEPRECATION: `Discourse.Mobile` is deprecated, use `this.site.mobileView` instead");
return Mobile;
}
});
export default Mobile;

View File

@ -154,7 +154,7 @@ const Composer = RestModel.extend({
usernameLink
});
if (!Discourse.Mobile.mobileView) {
if (!this.site.mobileView) {
const replyUsername = post.get('reply_to_user.username');
const replyAvatarTemplate = post.get('reply_to_user.avatar_template');
if (replyUsername && replyAvatarTemplate && this.get('action') === EDIT) {

View File

@ -7,7 +7,7 @@ const NavItem = Discourse.Model.extend({
name = this.get('name'),
count = this.get('count') || 0;
if (name === 'latest' && !Discourse.Mobile.mobileView) {
if (name === 'latest' && !Discourse.Site.currentProp('mobileView')) {
count = 0;
}

View File

@ -4,7 +4,7 @@ export default Discourse.Route.extend({
// HACK: Something with the way the user card intercepts clicks seems to break how the
// transition into a user's activity works. This makes the back button work on mobile
// where there is no user card as well as desktop where there is.
if (Discourse.Mobile.mobileView) {
if (this.site.mobileView) {
this.replaceWith('userActivity');
} else {
this.transitionTo('userActivity');

View File

@ -9,7 +9,7 @@ export default Ember.View.extend({
$('#discourse-modal').modal('show');
// Focus on first element
if (!Discourse.Mobile.mobileView && this.get('focusInput')) {
if (!this.site.mobileView && this.get('focusInput')) {
Em.run.schedule('afterRender', () => this.$('input:first').focus());
}

View File

@ -82,7 +82,7 @@ export default Ember.View.extend({
$shareLink.css({top: "" + y + "px"});
if (!Discourse.Mobile.mobileView) {
if (!this.site.mobileView) {
$shareLink.css({left: "" + x + "px"});
}

View File

@ -6,7 +6,7 @@ export default ContainerView.extend({
@on('init')
createButtons() {
const mobileView = Discourse.Mobile.mobileView;
const mobileView = this.site.mobileView;
if (!mobileView && this.currentUser.get('staff')) {
const viewArgs = {action: 'showTopicAdminMenu', title: 'topic_admin_menu', icon: 'wrench', position: 'absolute'};

View File

@ -76,7 +76,7 @@ export default Ember.View.extend({
_focusWhenOpened: function() {
// Don't focus on mobile or touch
if (Discourse.Mobile.mobileView || this.capabilities.isIOS) {
if (this.site.mobileView || this.capabilities.isIOS) {
return;
}

View File

@ -58,7 +58,7 @@ export default class PostCooked {
// deferring work only for posts with images
// we got to use screen here, cause nothing is rendered yet.
// long term we may want to allow for weird margins that are enforced, instead of hardcoding at 70/20
maxWindowWidth = maxWindowWidth || $(window).width() - (Discourse.Mobile.mobileView ? 20 : 70);
maxWindowWidth = maxWindowWidth || $(window).width() - (this.attrs.mobileView ? 20 : 70);
if (maxImageWidth < maxWindowWidth) {
maxWindowWidth = maxImageWidth;
}

View File

@ -134,7 +134,7 @@ registerButton('reply', attrs => {
if (!attrs.canCreatePost) { return; }
if (!Discourse.Mobile.mobileView) {
if (!attrs.mobileView) {
args.label = 'topic.reply.title';
}

View File

@ -19,6 +19,7 @@ export default createWidget('post-stream', {
let prevPost;
let prevDate;
const mobileView = this.site.mobileView;
for (let i=0; i<postArray.length; i++) {
const post = postArray[i];
@ -31,6 +32,7 @@ export default createWidget('post-stream', {
const transformed = transformPost(this.currentUser, this.site, post, prevPost, nextPost);
transformed.canCreatePost = attrs.canCreatePost;
transformed.mobileView = mobileView;
if (transformed.canManage) {
transformed.multiSelect = attrs.multiSelect;

View File

@ -331,7 +331,7 @@ createWidget('post-article', {
const replyPostNumber = this.attrs.reply_to_post_number;
// jump directly on mobile
if (Discourse.Mobile.mobileView) {
if (this.attrs.mobileView) {
DiscourseURL.jumpToPost(replyPostNumber);
return Ember.RSVP.Promise.resolve();
}

View File

@ -1,7 +1,7 @@
import DiscourseResolver from 'discourse/ember/resolver';
var originalTemplates, originalMobileViewFlag;
var resolver = DiscourseResolver.create();
let originalTemplates;
let resolver;
function lookupTemplate(name, expectedTemplate, message) {
var parseName = resolver.parseName(name);
@ -20,13 +20,11 @@ module("lib:resolver", {
originalTemplates = Ember.TEMPLATES;
Ember.TEMPLATES = {};
originalMobileViewFlag = Discourse.Mobile.mobileView;
Discourse.Mobile.mobileView = false;
resolver = DiscourseResolver.create();
},
teardown: function() {
Ember.TEMPLATES = originalTemplates;
Discourse.Mobile.mobileView = originalMobileViewFlag;
}
});
@ -92,7 +90,7 @@ test("resolves mobile templates to 'mobile/' namespace", function() {
"baz"
]);
Discourse.Mobile.mobileView = true;
resolver.mobileView = true;
lookupTemplate("template:foo", "mobile/foo", "finding mobile version even if normal one is not present");
lookupTemplate("template:bar", "mobile/bar", "preferring mobile version when both mobile and normal versions are present");