@ -1,14 +1,17 @@
"use strict" ;
"use strict" ;
/* global define, app, socket */
/* global define, app, socket , bootbox */
define ( 'admin/extend/plugins' , function ( ) {
define ( 'admin/extend/plugins' , function ( ) {
var Plugins = {
var Plugins = { } ;
init : function ( ) {
Plugins . init = function ( ) {
var pluginsList = $ ( '.plugins' ) ,
var pluginsList = $ ( '.plugins' ) ,
numPlugins = pluginsList [ 0 ] . querySelectorAll ( 'li' ) . length ,
numPlugins = pluginsList [ 0 ] . querySelectorAll ( 'li' ) . length ,
pluginID ;
pluginID ;
if ( numPlugins > 0 ) {
if ( ! numPlugins ) {
pluginsList . append ( '<li><p><i>No plugins found.</i></p></li>' ) ;
return ;
}
pluginsList . on ( 'click' , 'button[data-action="toggleActive"]' , function ( ) {
pluginsList . on ( 'click' , 'button[data-action="toggleActive"]' , function ( ) {
pluginID = $ ( this ) . parents ( 'li' ) . attr ( 'data-plugin-id' ) ;
pluginID = $ ( this ) . parents ( 'li' ) . attr ( 'data-plugin-id' ) ;
@ -33,31 +36,29 @@ define('admin/extend/plugins', function() {
pluginsList . on ( 'click' , 'button[data-action="toggleInstall"]' , function ( ) {
pluginsList . on ( 'click' , 'button[data-action="toggleInstall"]' , function ( ) {
pluginID = $ ( this ) . parents ( 'li' ) . attr ( 'data-plugin-id' ) ;
pluginID = $ ( this ) . parents ( 'li' ) . attr ( 'data-plugin-id' ) ;
if ( $ ( this ) . attr ( 'data-installed' ) === '1' ) {
return Plugins . toggleInstall ( pluginID , $ ( this ) . parents ( 'li' ) . attr ( 'data-version' ) ) ;
}
Plugins . suggest ( pluginID , function ( err , payload ) {
Plugins . suggest ( pluginID , function ( err , payload ) {
if ( ! err ) {
if ( err ) {
require ( [ 'semver' ] , function ( semver ) {
if ( payload . version !== 'latest' ) {
Plugins . toggleInstall ( pluginID , payload . version ) ;
} else if ( payload . version === 'latest' ) {
bootbox . confirm (
'<div class="alert alert-warning"><p><strong>No Compatibility Infomation Found</strong></p><p>This plugin did not specify a specific version for installation given your NodeBB version. Full compatibility cannot be guaranteed, and may cause your NodeBB to no longer start properly.</p></div>' +
'<p>In the event that NodeBB cannot boot properly:</p>' +
'<pre><code>$ ./nodebb reset plugin="' + pluginID + '"</code></pre>' +
'<p>Continue installation of latest version of this plugin?</p>'
, function ( confirm ) {
if ( confirm ) {
Plugins . toggleInstall ( pluginID , 'latest' ) ;
}
} ) ;
}
} ) ;
} else {
bootbox . confirm ( '<p>NodeBB could not reach the package manager, proceed with installation of latest version?</p><div class="alert alert-danger"><strong>Server returned (' + err . status + ')</strong>: ' + err . responseText + '</div>' , function ( confirm ) {
bootbox . confirm ( '<p>NodeBB could not reach the package manager, proceed with installation of latest version?</p><div class="alert alert-danger"><strong>Server returned (' + err . status + ')</strong>: ' + err . responseText + '</div>' , function ( confirm ) {
if ( confirm ) {
if ( confirm ) {
Plugins . toggleInstall ( pluginID , 'latest' ) ;
Plugins . toggleInstall ( pluginID , 'latest' ) ;
}
}
} ) ;
} ) ;
return ;
}
}
require ( [ 'semver' ] , function ( semver ) {
if ( payload . version !== 'latest' ) {
Plugins . toggleInstall ( pluginID , payload . version ) ;
} else if ( payload . version === 'latest' ) {
confirmInstall ( pluginID , function ( ) {
Plugins . toggleInstall ( pluginID , 'latest' ) ;
} ) ;
}
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
@ -66,55 +67,25 @@ define('admin/extend/plugins', function() {
var parent = btn . parents ( 'li' ) ;
var parent = btn . parents ( 'li' ) ;
pluginID = parent . attr ( 'data-plugin-id' ) ;
pluginID = parent . attr ( 'data-plugin-id' ) ;
Plugins . suggest ( pluginID , function ( err , payload ) {
Plugins . suggest ( pluginID , function ( err , payload ) {
if ( ! err ) {
if ( err ) {
return bootbox . alert ( '<p>NodeBB could not reach the package manager, an upgrade is not suggested at this time.</p>' ) ;
}
require ( [ 'semver' ] , function ( semver ) {
require ( [ 'semver' ] , function ( semver ) {
if ( payload . version !== 'latest' && semver . gt ( payload . version , parent . find ( '.currentVersion' ) . text ( ) ) ) {
if ( payload . version !== 'latest' && semver . gt ( payload . version , parent . find ( '.currentVersion' ) . text ( ) ) ) {
btn . attr ( 'disabled' , true ) . find ( 'i' ) . attr ( 'class' , 'fa fa-refresh fa-spin' ) ;
upgrade ( pluginID , btn , payload . version ) ;
socket . emit ( 'admin.plugins.upgrade' , {
id : pluginID ,
version : payload . version
} , function ( err ) {
if ( err ) {
return app . alertError ( err . message ) ;
}
parent . find ( '.fa-exclamation-triangle' ) . remove ( ) ;
parent . find ( '.currentVersion' ) . text ( payload . version ) ;
btn . remove ( ) ;
} ) ;
} else if ( payload . version === 'latest' ) {
} else if ( payload . version === 'latest' ) {
bootbox . confirm (
confirmInstall ( pluginID , function ( ) {
'<div class="alert alert-warning"><p><strong>No Compatibility Infomation Found</strong></p><p>This plugin did not specify a specific version for installation given your NodeBB version. Full compatibility cannot be guaranteed, and may cause your NodeBB to no longer start properly.</p></div>' +
upgrade ( pluginID , btn , payload . version ) ;
'<p>In the event that NodeBB cannot boot properly:</p>' +
'<pre><code>$ ./nodebb reset plugin="' + pluginID + '"</code></pre>' +
'<p>Continue installation of latest version of this plugin?</p>'
, function ( confirm ) {
if ( confirm ) {
socket . emit ( 'admin.plugins.upgrade' , {
id : pluginID ,
version : payload . version
} , function ( err ) {
if ( err ) {
return app . alertError ( err . message ) ;
}
parent . find ( '.fa-exclamation-triangle' ) . remove ( ) ;
parent . find ( '.currentVersion' ) . text ( payload . version ) ;
btn . remove ( ) ;
} ) ;
}
} ) ;
} ) ;
} else {
} else {
bootbox . alert ( '<p>Your version of NodeBB (v' + app . config . version + ') is only cleared to upgrade to v' + payload . version + ' of this plugin. Please update your NodeBB if you wish to install a newer version of this plugin.' ) ;
bootbox . alert ( '<p>Your version of NodeBB (v' + app . config . version + ') is only cleared to upgrade to v' + payload . version + ' of this plugin. Please update your NodeBB if you wish to install a newer version of this plugin.' ) ;
}
}
} ) ;
} ) ;
} else {
bootbox . alert ( '<p>NodeBB could not reach the package manager, an upgrade is not suggested at this time.</p>' ) ;
}
} ) ;
} ) ;
} ) ;
} ) ;
$ ( '#plugin-search' ) . on ( 'input propertychange' , function ( ) {
$ ( '#plugin-search' ) . on ( 'input propertychange' , function ( ) {
var term = $ ( this ) . val ( ) ;
var term = $ ( this ) . val ( ) ;
$ ( '.plugins li' ) . each ( function ( ) {
$ ( '.plugins li' ) . each ( function ( ) {
@ -122,12 +93,37 @@ define('admin/extend/plugins', function() {
$ ( this ) . toggleClass ( 'hide' , pluginId && pluginId . indexOf ( term ) === - 1 ) ;
$ ( this ) . toggleClass ( 'hide' , pluginId && pluginId . indexOf ( term ) === - 1 ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ;
} else {
function confirmInstall ( pluginID , callback ) {
pluginsList . append ( '<li><p><i>No plugins found.</i></p></li>' ) ;
bootbox . confirm (
'<div class="alert alert-warning"><p><strong>No Compatibility Infomation Found</strong></p><p>This plugin did not specify a specific version for installation given your NodeBB version. Full compatibility cannot be guaranteed, and may cause your NodeBB to no longer start properly.</p></div>' +
'<p>In the event that NodeBB cannot boot properly:</p>' +
'<pre><code>$ ./nodebb reset plugin="' + pluginID + '"</code></pre>' +
'<p>Continue installation of latest version of this plugin?</p>' , function ( confirm ) {
if ( confirm ) {
callback ( ) ;
}
}
} ,
} ) ;
toggleInstall : function ( pluginID , version , callback ) {
}
function upgrade ( pluginID , btn , version ) {
btn . attr ( 'disabled' , true ) . find ( 'i' ) . attr ( 'class' , 'fa fa-refresh fa-spin' ) ;
socket . emit ( 'admin.plugins.upgrade' , {
id : pluginID ,
version : version
} , function ( err ) {
if ( err ) {
return app . alertError ( err . message ) ;
}
var parent = btn . parents ( 'li' ) ;
parent . find ( '.fa-exclamation-triangle' ) . remove ( ) ;
parent . find ( '.currentVersion' ) . text ( version ) ;
btn . remove ( ) ;
} ) ;
}
Plugins . toggleInstall = function ( pluginID , version , callback ) {
var btn = $ ( 'li[data-plugin-id="' + pluginID + '"] button[data-action="toggleInstall"]' ) ;
var btn = $ ( 'li[data-plugin-id="' + pluginID + '"] button[data-action="toggleInstall"]' ) ;
var activateBtn = btn . siblings ( '[data-action="toggleActive"]' ) ;
var activateBtn = btn . siblings ( '[data-action="toggleActive"]' ) ;
btn . html ( btn . html ( ) + 'ing' )
btn . html ( btn . html ( ) + 'ing' )
@ -146,12 +142,14 @@ define('admin/extend/plugins', function() {
btn . html ( '<i class="fa fa-trash-o"></i> Uninstall' ) ;
btn . html ( '<i class="fa fa-trash-o"></i> Uninstall' ) ;
} else {
} else {
btn . html ( '<i class="fa fa-download"></i> Install' ) ;
btn . html ( '<i class="fa fa-download"></i> Install' ) ;
}
}
activateBtn . toggleClass ( 'hidden' , ! status . installed ) ;
activateBtn . toggleClass ( 'hidden' , ! status . installed ) ;
btn . toggleClass ( 'btn-danger' , status . installed ) . toggleClass ( 'btn-success' , ! status . installed )
btn . toggleClass ( 'btn-danger' , status . installed )
. attr ( 'disabled' , false ) ;
. toggleClass ( 'btn-success' , ! status . installed )
. attr ( 'disabled' , false )
. attr ( 'data-installed' , status . installed ? 1 : 0 ) ;
app . alert ( {
app . alert ( {
alert _id : 'plugin_toggled' ,
alert _id : 'plugin_toggled' ,
@ -165,8 +163,9 @@ define('admin/extend/plugins', function() {
callback . apply ( this , arguments ) ;
callback . apply ( this , arguments ) ;
}
}
} ) ;
} ) ;
} ,
} ;
suggest : function ( pluginId , callback ) {
Plugins . suggest = function ( pluginId , callback ) {
var nbbVersion = app . config . version ;
var nbbVersion = app . config . version ;
$ . ajax ( 'https://packages.nodebb.org/api/v1/suggest' , {
$ . ajax ( 'https://packages.nodebb.org/api/v1/suggest' , {
type : 'GET' ,
type : 'GET' ,
@ -178,7 +177,6 @@ define('admin/extend/plugins', function() {
} ) . done ( function ( payload ) {
} ) . done ( function ( payload ) {
callback ( undefined , payload ) ;
callback ( undefined , payload ) ;
} ) . fail ( callback ) ;
} ) . fail ( callback ) ;
}
} ;
} ;
return Plugins ;
return Plugins ;