1
0
Fork 0
mirror of https://github.com/TryGhost/Ghost-Admin.git synced 2023-12-14 02:33:04 +01:00

Remove lodash from admin client.

* Adds `bind`, `isFinite`, and `isNumber` utility functions from lodash.
* Use new util funtions instead of lodash throughout the codebase.
* Remove lodash from vendor builds.
This commit is contained in:
Robert Jackson 2014-07-31 16:29:35 -04:00 committed by Jason Williams
parent aeeabe5ada
commit 0bfa9dd3d1
15 changed files with 82 additions and 23 deletions

View file

@ -4,20 +4,21 @@ import MarkerManager from 'ghost/mixins/marker-manager';
import mobileCodeMirror from 'ghost/utils/codemirror-mobile';
import setScrollClassName from 'ghost/utils/set-scroll-classname';
import codeMirrorShortcuts from 'ghost/utils/codemirror-shortcuts';
import bind from 'ghost/utils/bind';
codeMirrorShortcuts.init();
var onChangeHandler = function (cm, changeObj) {
var line,
component = cm.component;
component = cm.component,
// fill array with a range of numbers
for (line = changeObj.from.line; line < changeObj.from.line + changeObj.text.length; line += 1) {
component.checkLine(line, changeObj.origin);
component.checkLine.call(component, line, changeObj.origin);
}
// Is this a line which may have had a marker on it?
component.checkMarkers();
component.checkMarkers.call(component);
cm.component.set('value', cm.getValue());
@ -50,7 +51,10 @@ var Codemirror = Ember.TextArea.extend(MarkerManager, {
},
afterRenderEvent: function () {
var initMarkers = _.bind(this.initMarkers, this);
var self = this;
function initMarkers () {
self.initMarkers.apply(self, arguments);
}
// replaces CodeMirror with TouchEditor only if we're on mobile
mobileCodeMirror.createIfMobile();

View file

@ -16,17 +16,24 @@ var Markdown = Ember.Component.extend({
// might need to make sure markdown has been processed first
reInitDropzones: function () {
Ember.run.scheduleOnce('afterRender', this, function () {
var dropzones = $('.js-drop-zone');
var dropzones = $('.js-drop-zone'),
self = this;
uploader.call(dropzones, {
editor: true,
fileStorage: this.get('config.fileStorage')
});
dropzones.on('uploadstart', _.bind(this.sendAction, this, 'uploadStarted'));
dropzones.on('uploadfailure', _.bind(this.sendAction, this, 'uploadFinished'));
dropzones.on('uploadsuccess', _.bind(this.sendAction, this, 'uploadFinished'));
dropzones.on('uploadsuccess', _.bind(this.sendAction, this, 'uploadSuccess'));
function boundSendAction(actionName) {
return function() {
self.sendAction.call(self, actionName);
}
}
dropzones.on('uploadstart', boundSendAction('uploadStarted'));
dropzones.on('uploadfailure', boundSendAction('uploadFinished'));
dropzones.on('uploadsuccess', boundSendAction('uploadFinished'));
dropzones.on('uploadsuccess', boundSendAction('uploadSuccess'));
});
}.observes('markdown')
});

View file

@ -2,6 +2,7 @@
import {parseDateString, formatDate} from 'ghost/utils/date-formatting';
import SlugGenerator from 'ghost/models/slug-generator';
import boundOneWay from 'ghost/utils/bound-one-way';
import isNumber from 'ghost/utils/isNumber';
var PostSettingsMenuController = Ember.ObjectController.extend({
//State for if the user is viewing a tab's pane.
@ -274,7 +275,7 @@ var PostSettingsMenuController = Ember.ObjectController.extend({
// if the candidate slug is the same as the existing slug except
// for the incrementor then the existing slug should be used
if (_.isNumber(check) && check > 0) {
if (isNumber(check) && check > 0) {
if (slug === slugTokens.join('-') && serverSlug !== newSlug) {
self.set('slugValue', slug);

View file

@ -1,4 +1,5 @@
import SlugGenerator from 'ghost/models/slug-generator';
import isNumber from 'ghost/utils/isNumber';
var SettingsUserController = Ember.ObjectController.extend({
@ -207,7 +208,7 @@ var SettingsUserController = Ember.ObjectController.extend({
// if the candidate slug is the same as the existing slug except
// for the incrementor then the existing slug should be used
if (_.isNumber(check) && check > 0) {
if (isNumber(check) && check > 0) {
if (slug === slugTokens.join('-') && serverSlug !== newSlug) {
self.set('slugValue', slug);

View file

@ -12,7 +12,11 @@ var PaginationRoute = Ember.Mixin.create({
setupPagination: function (settings) {
settings = settings || {};
settings = _.defaults(settings, defaultPaginationSettings);
for (var key in defaultPaginationSettings) {
if (!settings.hasOwnProperty(key)) {
settings[key] = defaultPaginationSettings[key];
}
}
this.set('paginationSettings', settings);
this.controller.set('paginationSettings', settings);

View file

@ -87,13 +87,15 @@ var User = DS.Model.extend(NProgressSaveMixin, SelectiveSaveMixin, ValidationEng
isPasswordValid: Ember.computed.empty('passwordValidationErrors.[]'),
active: Ember.computed('status', function () {
return _.contains(['active', 'warn-1', 'warn-2', 'warn-3', 'warn-4', 'locked'], this.get('status'));
}),
invited: Ember.computed('status', function () {
return _.contains(['invited', 'invited-pending'], this.get('status'));
}),
pending: Ember.computed.equal('status', 'invited-pending')
active: function () {
return ['active', 'warn-1', 'warn-2', 'warn-3', 'warn-4', 'locked'].indexOf(this.get('status')) > -1;
}.property('status'),
invited: function () {
return ['invited', 'invited-pending'].indexOf(this.get('status')) > -1;
}.property('status'),
pending: Ember.computed.equal('status', 'invited-pending').property('status')
});
export default User;

View file

@ -1,4 +1,6 @@
import base from 'ghost/mixins/editor-route-base';
import isNumber from 'ghost/utils/isNumber';
import isFinite from 'ghost/utils/isFinite';
var EditorEditRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin, base, {
classNames: ['editor'],
@ -11,7 +13,7 @@ var EditorEditRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin, bas
postId = Number(params.post_id);
if (!_.isNumber(postId) || !_.isFinite(postId) || postId % 1 !== 0 || postId <= 0) {
if (!isNumber(postId) || !isFinite(postId) || postId % 1 !== 0 || postId <= 0) {
return this.transitionTo('error404', 'editor/' + params.post_id);
}

View file

@ -1,5 +1,7 @@
import loadingIndicator from 'ghost/mixins/loading-indicator';
import ShortcutsRoute from 'ghost/mixins/shortcuts-route';
import isNumber from 'ghost/utils/isNumber';
import isFinite from 'ghost/utils/isFinite';
var PostsPostRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin, loadingIndicator, ShortcutsRoute, {
model: function (params) {
@ -10,7 +12,7 @@ var PostsPostRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin, load
postId = Number(params.post_id);
if (!_.isNumber(postId) || !_.isFinite(postId) || postId % 1 !== 0 || postId <= 0)
if (!isNumber(postId) || !isFinite(postId) || postId % 1 !== 0 || postId <= 0)
{
return this.transitionTo('error404', params.post_id);
}

View file

@ -1,6 +1,7 @@
import MobileIndexRoute from 'ghost/routes/mobile-index-route';
import CurrentUserSettings from 'ghost/mixins/current-user-settings';
import mobileQuery from 'ghost/utils/mobile';
import bind from 'ghost/utils/bind';
var SettingsIndexRoute = MobileIndexRoute.extend(SimpleAuth.AuthenticatedRouteMixin, CurrentUserSettings, {
// Redirect users without permission to view settings,

15
utils/bind.js Normal file
View file

@ -0,0 +1,15 @@
var slice = Array.prototype.slice;
function bind(/* func, args, thisArg */) {
var args = slice.call(arguments),
func = args.shift(),
thisArg = args.pop();
function bound() {
return func.apply(thisArg, args);
}
return bound;
}
export default bind;

9
utils/isFinite.js Normal file
View file

@ -0,0 +1,9 @@
/* globals window */
// isFinite function from lodash
function isFinite(value) {
return window.isFinite(value) && !window.isNaN(parseFloat(value));
}
export default isFinite;

10
utils/isNumber.js Normal file
View file

@ -0,0 +1,10 @@
// isNumber function from lodash
var toString = Object.prototype.toString;
function isNumber(value) {
return typeof value === 'number' ||
value && typeof value === 'object' && toString.call(value) === '[object Number]' || false;
}
export default isNumber;

View file

@ -6,7 +6,7 @@ function init() {
});
validator.extend('notContains', function (str, badString) {
return !_.contains(str, badString);
return str.indexOf(badString) === -1;
});
}

View file

@ -50,7 +50,7 @@ var UserValidator = Ember.Object.create({
validationErrors.push({ message: 'Location is too long' });
}
if (!_.isEmpty(website) &&
if (!Ember.isEmpty(website) &&
(!validator.isURL(website, { protocols: ['http', 'https'], require_protocol: true }) ||
!validator.isLength(website, 0, 2000))) {

View file

@ -1,4 +1,5 @@
import MobileParentView from 'ghost/views/mobile/parent-view';
import bind from 'ghost/utils/bind';
var SettingsView = MobileParentView.extend({
// MobileParentView callbacks