2015-02-13 07:22:32 +03:00
import Ember from 'ember' ;
2015-08-10 18:43:49 +03:00
import SettingsSaveMixin from 'ghost/mixins/settings-save' ;
2015-05-11 18:35:55 +03:00
import randomPassword from 'ghost/utils/random-password' ;
2016-01-19 16:03:27 +03:00
const {
Controller ,
computed ,
inject : { service } ,
2016-03-03 11:52:27 +03:00
observer ,
run
2016-01-19 16:03:27 +03:00
} = Ember ;
2015-05-26 05:10:50 +03:00
2015-10-28 14:36:45 +03:00
export default Controller . extend ( SettingsSaveMixin , {
2015-11-18 13:50:48 +03:00
showUploadLogoModal : false ,
showUploadCoverModal : false ,
2016-01-19 16:03:27 +03:00
notifications : service ( ) ,
config : service ( ) ,
2016-03-03 11:52:27 +03:00
_scratchFacebook : null ,
_scratchTwitter : null ,
2015-06-13 17:34:09 +03:00
2015-10-28 14:36:45 +03:00
selectedTheme : computed ( 'model.activeTheme' , 'themes' , function ( ) {
let activeTheme = this . get ( 'model.activeTheme' ) ;
let themes = this . get ( 'themes' ) ;
let selectedTheme ;
themes . forEach ( ( theme ) => {
2015-06-13 17:34:09 +03:00
if ( theme . name === activeTheme ) {
selectedTheme = theme ;
}
} ) ;
return selectedTheme ;
} ) ,
2014-12-30 05:11:24 +03:00
2015-10-28 14:36:45 +03:00
logoImageSource : computed ( 'model.logo' , function ( ) {
2015-02-09 18:57:50 +03:00
return this . get ( 'model.logo' ) || '' ;
} ) ,
2015-10-28 14:36:45 +03:00
coverImageSource : computed ( 'model.cover' , function ( ) {
2015-02-09 18:57:50 +03:00
return this . get ( 'model.cover' ) || '' ;
} ) ,
2015-10-28 14:36:45 +03:00
isDatedPermalinks : computed ( 'model.permalinks' , {
set ( key , value ) {
2014-12-30 05:11:24 +03:00
this . set ( 'model.permalinks' , value ? '/:year/:month/:day/:slug/' : '/:slug/' ) ;
2014-03-21 06:55:32 +04:00
2015-10-28 14:36:45 +03:00
let slugForm = this . get ( 'model.permalinks' ) ;
2015-06-03 05:56:42 +03:00
return slugForm !== '/:slug/' ;
} ,
2015-10-28 14:36:45 +03:00
get ( ) {
let slugForm = this . get ( 'model.permalinks' ) ;
2014-03-21 06:55:32 +04:00
2015-06-03 05:56:42 +03:00
return slugForm !== '/:slug/' ;
}
2014-07-30 05:57:19 +04:00
} ) ,
2014-03-21 06:55:32 +04:00
2015-10-28 14:36:45 +03:00
themes : computed ( function ( ) {
2014-12-30 05:11:24 +03:00
return this . get ( 'model.availableThemes' ) . reduce ( function ( themes , t ) {
2015-10-28 14:36:45 +03:00
let theme = { } ;
2014-06-20 06:29:49 +04:00
theme . name = t . name ;
2015-10-28 14:36:45 +03:00
theme . label = t . package ? ` ${ t . package . name } - ${ t . package . version } ` : t . name ;
2014-06-20 06:29:49 +04:00
theme . package = t . package ;
theme . active = ! ! t . active ;
themes . push ( theme ) ;
return themes ;
} , [ ] ) ;
2014-07-30 05:57:19 +04:00
} ) . readOnly ( ) ,
2014-06-20 06:29:49 +04:00
2015-10-28 14:36:45 +03:00
generatePassword : observer ( 'model.isPrivate' , function ( ) {
2015-09-02 12:01:20 +03:00
this . get ( 'model.errors' ) . remove ( 'password' ) ;
2015-09-03 14:06:50 +03:00
if ( this . get ( 'model.isPrivate' ) && this . get ( 'model.hasDirtyAttributes' ) ) {
2015-05-11 18:35:55 +03:00
this . get ( 'model' ) . set ( 'password' , randomPassword ( ) ) ;
}
} ) ,
2015-10-28 14:36:45 +03:00
save ( ) {
let notifications = this . get ( 'notifications' ) ;
let config = this . get ( 'config' ) ;
2015-07-07 20:14:23 +03:00
2015-10-28 14:36:45 +03:00
return this . get ( 'model' ) . save ( ) . then ( ( model ) => {
2015-08-10 18:43:49 +03:00
config . set ( 'blogTitle' , model . get ( 'title' ) ) ;
2014-03-21 06:55:32 +04:00
2016-05-16 03:41:28 +03:00
// this forces the document title to recompute after
// a blog title change
this . send ( 'collectTitleTokens' , [ ] ) ;
2015-08-10 18:43:49 +03:00
return model ;
2015-10-28 14:36:45 +03:00
} ) . catch ( ( error ) => {
2015-08-10 18:43:49 +03:00
if ( error ) {
2015-10-07 17:44:23 +03:00
notifications . showAPIError ( error , { key : 'settings.save' } ) ;
2015-08-10 18:43:49 +03:00
}
2016-03-03 11:52:27 +03:00
throw error ;
2015-08-10 18:43:49 +03:00
} ) ;
} ,
2014-06-24 10:33:24 +04:00
2015-08-10 18:43:49 +03:00
actions : {
2015-10-28 14:36:45 +03:00
checkPostsPerPage ( ) {
let postsPerPage = this . get ( 'model.postsPerPage' ) ;
2014-12-30 05:11:24 +03:00
if ( postsPerPage < 1 || postsPerPage > 1000 || isNaN ( postsPerPage ) ) {
this . set ( 'model.postsPerPage' , 5 ) ;
2014-08-19 02:56:28 +04:00
}
2015-06-13 17:34:09 +03:00
} ,
2015-10-28 14:36:45 +03:00
setTheme ( theme ) {
2015-06-13 17:34:09 +03:00
this . set ( 'model.activeTheme' , theme . name ) ;
2015-11-18 13:50:48 +03:00
} ,
toggleUploadCoverModal ( ) {
this . toggleProperty ( 'showUploadCoverModal' ) ;
} ,
toggleUploadLogoModal ( ) {
this . toggleProperty ( 'showUploadLogoModal' ) ;
2016-03-03 11:52:27 +03:00
} ,
validateFacebookUrl ( ) {
let newUrl = this . get ( '_scratchFacebook' ) ;
let oldUrl = this . get ( 'model.facebook' ) ;
let errMessage = '' ;
2016-05-17 21:14:14 +03:00
if ( newUrl === '' ) {
2016-03-03 11:52:27 +03:00
// Clear out the Facebook url
this . set ( 'model.facebook' , '' ) ;
this . get ( 'model.errors' ) . remove ( 'facebook' ) ;
return ;
}
2016-05-17 21:14:14 +03:00
// _scratchFacebook will be null unless the user has input something
if ( ! newUrl ) {
newUrl = oldUrl ;
}
2016-03-03 11:52:27 +03:00
// If new url didn't change, exit
if ( newUrl === oldUrl ) {
2016-05-16 21:16:40 +03:00
this . get ( 'model.errors' ) . remove ( 'facebook' ) ;
2016-03-03 11:52:27 +03:00
return ;
}
2016-05-16 21:16:40 +03:00
if ( newUrl . match ( /(?:facebook\.com\/)(\S+)/ ) || newUrl . match ( /([a-z\d\.]+)/i ) ) {
let username = [ ] ;
2016-03-03 11:52:27 +03:00
2016-05-16 21:16:40 +03:00
if ( newUrl . match ( /(?:facebook\.com\/)(\S+)/ ) ) {
[ , username ] = newUrl . match ( /(?:facebook\.com\/)(\S+)/ ) ;
} else {
[ , username ] = newUrl . match ( /(?:https\:\/\/|http\:\/\/)?(?:www\.)?(?:\w+\.\w+\/+)?(\S+)/mi ) ;
}
2016-03-03 11:52:27 +03:00
2016-05-16 21:16:40 +03:00
// check if we have a /page/username or without
if ( username . match ( /^(?:\/)?(pages?\/\S+)/mi ) ) {
// we got a page url, now save the username without the / in the beginning
[ , username ] = username . match ( /^(?:\/)?(pages?\/\S+)/mi ) ;
} else if ( username . match ( /^(http|www)|(\/)/ ) || ! username . match ( /^([a-z\d\.]{5,50})$/mi ) ) {
errMessage = ! username . match ( /^([a-z\d\.]{5,50})$/mi ) ? 'Your Page name is not a valid Facebook Page name' : 'The URL must be in a format like https://www.facebook.com/yourPage' ;
2016-03-03 11:52:27 +03:00
this . get ( 'model.errors' ) . add ( 'facebook' , errMessage ) ;
this . get ( 'model.hasValidated' ) . pushObject ( 'facebook' ) ;
return ;
}
2016-05-16 21:16:40 +03:00
newUrl = ` https://www.facebook.com/ ${ username } ` ;
this . set ( 'model.facebook' , newUrl ) ;
this . get ( 'model.errors' ) . remove ( 'facebook' ) ;
this . get ( 'model.hasValidated' ) . pushObject ( 'facebook' ) ;
// User input is validated
return this . save ( ) . then ( ( ) => {
this . set ( 'model.facebook' , '' ) ;
run . schedule ( 'afterRender' , this , function ( ) {
this . set ( 'model.facebook' , newUrl ) ;
} ) ;
} ) ;
} else {
errMessage = 'The URL must be in a format like ' +
'https://www.facebook.com/yourPage' ;
this . get ( 'model.errors' ) . add ( 'facebook' , errMessage ) ;
this . get ( 'model.hasValidated' ) . pushObject ( 'facebook' ) ;
return ;
2016-03-03 11:52:27 +03:00
}
} ,
validateTwitterUrl ( ) {
let newUrl = this . get ( '_scratchTwitter' ) ;
let oldUrl = this . get ( 'model.twitter' ) ;
let errMessage = '' ;
2016-05-17 21:14:14 +03:00
if ( newUrl === '' ) {
// Clear out the Twitter url
2016-03-03 11:52:27 +03:00
this . set ( 'model.twitter' , '' ) ;
this . get ( 'model.errors' ) . remove ( 'twitter' ) ;
return ;
}
2016-05-17 21:14:14 +03:00
// _scratchTwitter will be null unless the user has input something
if ( ! newUrl ) {
newUrl = oldUrl ;
}
2016-03-03 11:52:27 +03:00
// If new url didn't change, exit
if ( newUrl === oldUrl ) {
2016-05-16 21:16:40 +03:00
this . get ( 'model.errors' ) . remove ( 'twitter' ) ;
2016-03-03 11:52:27 +03:00
return ;
}
2016-05-16 21:16:40 +03:00
if ( newUrl . match ( /(?:twitter\.com\/)(\S+)/ ) || newUrl . match ( /([a-z\d\.]+)/i ) ) {
let username = [ ] ;
2016-03-03 11:52:27 +03:00
2016-05-16 21:16:40 +03:00
if ( newUrl . match ( /(?:twitter\.com\/)(\S+)/ ) ) {
[ , username ] = newUrl . match ( /(?:twitter\.com\/)(\S+)/ ) ;
} else {
[ username ] = newUrl . match ( /([^/]+)\/?$/mi ) ;
}
2016-03-03 11:52:27 +03:00
2016-05-16 21:16:40 +03:00
// check if username starts with http or www and show error if so
if ( username . match ( /^(http|www)|(\/)/ ) || ! username . match ( /^[a-z\d\.\_]{1,15}$/mi ) ) {
errMessage = ! username . match ( /^[a-z\d\.\_]{1,15}$/mi ) ? 'Your Username is not a valid Twitter Username' : 'The URL must be in a format like https://twitter.com/yourUsername' ;
2016-03-03 11:52:27 +03:00
this . get ( 'model.errors' ) . add ( 'twitter' , errMessage ) ;
this . get ( 'model.hasValidated' ) . pushObject ( 'twitter' ) ;
return ;
}
2016-05-16 21:16:40 +03:00
newUrl = ` https://twitter.com/ ${ username } ` ;
this . set ( 'model.twitter' , newUrl ) ;
this . get ( 'model.errors' ) . remove ( 'twitter' ) ;
this . get ( 'model.hasValidated' ) . pushObject ( 'twitter' ) ;
// User input is validated
return this . save ( ) . then ( ( ) => {
this . set ( 'model.twitter' , '' ) ;
run . schedule ( 'afterRender' , this , function ( ) {
this . set ( 'model.twitter' , newUrl ) ;
} ) ;
} ) ;
} else {
errMessage = 'The URL must be in a format like ' +
'https://twitter.com/yourUsername' ;
this . get ( 'model.errors' ) . add ( 'twitter' , errMessage ) ;
this . get ( 'model.hasValidated' ) . pushObject ( 'twitter' ) ;
return ;
2016-03-03 11:52:27 +03:00
}
2014-08-19 02:56:28 +04:00
}
2014-03-21 06:55:32 +04:00
}
} ) ;