package.grid.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /**
  2. * Loads a grid of Packages.
  3. *
  4. * @class MODx.grid.Package
  5. * @extends MODx.grid.Grid
  6. * @param {Object} config An object of options.
  7. * @xtype modx-package-grid
  8. */
  9. MODx.grid.Package = function(config) {
  10. config = config || {};
  11. this.exp = new Ext.grid.RowExpander({
  12. tpl : new Ext.XTemplate(
  13. '<p class="package-readme"><i>{readme}</i></p>'
  14. )
  15. });
  16. /* Package name + action button renderer */
  17. this.mainColumnTpl = new Ext.XTemplate('<tpl for=".">'
  18. +'<h3 class="main-column{state:defaultValue("")}">{name}</h3>'
  19. +'<tpl if="actions !== null">'
  20. +'<ul class="actions">'
  21. +'<tpl for="actions">'
  22. +'<li><button type="button" class="controlBtn {className}">{text}</button></li>'
  23. +'</tpl>'
  24. +'</ul>'
  25. +'</tpl>'
  26. +'<tpl if="message !== null">'
  27. +'<tpl for="message">'
  28. +'<div class="{className}">{text}</div>'
  29. +'</tpl>'
  30. +'</tpl>'
  31. +'</tpl>', {
  32. compiled: true
  33. });
  34. var cols = [];
  35. cols.push(this.exp);
  36. cols.push({ header: _('name') ,dataIndex: 'name', id:'main',renderer: { fn: this.mainColumnRenderer, scope: this } });
  37. cols.push({ header: _('version') ,dataIndex: 'version', id: 'meta-col', fixed:true, width:120, renderer: function (v, md, record) { return v + '-' + record.data.release;} });
  38. cols.push({ header: _('installed') ,dataIndex: 'installed', id: 'info-col', fixed:true, width: 160 ,renderer: this.dateColumnRenderer });
  39. cols.push({ header: _('provider') ,dataIndex: 'provider_name', id: 'text-col', fixed:true, width:120 });
  40. var dlbtn;
  41. if (MODx.curlEnabled) {
  42. dlbtn = {
  43. text: _('download_extras')
  44. ,xtype: 'splitbutton'
  45. ,cls:'primary-button'
  46. ,handler: this.onDownloadMoreExtra
  47. ,menu: {
  48. items:[{
  49. text: _('provider_select')
  50. ,handler: this.changeProvider
  51. ,scope: this
  52. },{
  53. text: _('package_search_local_title')
  54. ,handler: this.searchLocal
  55. ,scope: this
  56. },{
  57. text: _('transport_package_upload')
  58. ,handler: this.uploadTransportPackage
  59. ,scope: this
  60. }]
  61. }
  62. };
  63. } else {
  64. dlbtn = {
  65. text: _('package_search_local_title')
  66. ,xtype: 'splitbutton'
  67. ,handler: this.searchLocal
  68. ,scope: this
  69. ,menu: {
  70. items: [{
  71. text: _('transport_package_upload')
  72. ,handler: this.uploadTransportPackage
  73. ,scope: this
  74. }]
  75. }
  76. };
  77. }
  78. Ext.applyIf(config,{
  79. title: _('packages')
  80. ,id: 'modx-package-grid'
  81. ,url: MODx.config.connector_url
  82. ,action: 'workspace/packages/getlist'
  83. ,fields: ['signature','name','version','release','created','updated','installed','state','workspace'
  84. ,'provider','provider_name','disabled','source','attributes','readme','menu'
  85. ,'install','textaction','iconaction','updateable']
  86. ,plugins: [this.exp]
  87. ,pageSize: Math.min(parseInt(MODx.config.default_per_page), 25)
  88. ,columns: cols
  89. ,primaryKey: 'signature'
  90. ,paging: true
  91. ,autosave: true
  92. ,tbar: [dlbtn, {
  93. text: _('packages_purge')
  94. ,handler: this.purgePackages
  95. },'->',{
  96. xtype: 'textfield'
  97. ,name: 'search'
  98. ,id: 'modx-package-search'
  99. ,cls: 'x-form-filter'
  100. ,emptyText: _('search_ellipsis')
  101. ,listeners: {
  102. 'change': {fn: this.search, scope: this}
  103. ,'render': {fn: function(pnl) {
  104. new Ext.KeyMap(pnl.getEl(), {
  105. key: Ext.EventObject.ENTER
  106. ,fn: this.blur
  107. ,scope: pnl
  108. });
  109. },scope:this}
  110. }
  111. },{
  112. xtype: 'button'
  113. ,id: 'modx-package-filter-clear'
  114. ,cls: 'x-form-filter-clear'
  115. ,text: _('filter_clear')
  116. ,listeners: {
  117. 'click': {fn: this.clearFilter, scope: this},
  118. 'mouseout': { fn: function(evt){
  119. this.removeClass('x-btn-focus');
  120. }
  121. }
  122. }
  123. }]
  124. });
  125. MODx.grid.Package.superclass.constructor.call(this,config);
  126. this.on('render',function() {
  127. this.getView().mainBody.update('<div class="x-grid-empty">' + _('loading') + '</div>');
  128. },this);
  129. this.on('click', this.onClick, this);
  130. };
  131. Ext.extend(MODx.grid.Package,MODx.grid.Grid,{
  132. console: null
  133. ,activate: function() {
  134. var west = Ext.getCmp('modx-leftbar-tabs')
  135. ,stateId = 'modx-leftbar-tabs';
  136. if (west && west.stateId) {
  137. stateId = west.stateId;
  138. }
  139. var state = Ext.state.Manager.get(stateId);
  140. if (state && state.collapsed === false) {
  141. // Panel was not collapsed before, lets restore it
  142. Ext.getCmp('modx-layout').showLeftbar();
  143. }
  144. Ext.getCmp('modx-panel-packages').getLayout().setActiveItem(this.id);
  145. this.updateBreadcrumbs(_('packages_desc'));
  146. }
  147. ,updateBreadcrumbs: function(msg, highlight){
  148. msg = Ext.getCmp('packages-breadcrumbs').desc;
  149. if(highlight){
  150. msg.text = msg;
  151. msg.className = 'highlight';
  152. }
  153. Ext.getCmp('packages-breadcrumbs').reset(msg);
  154. }
  155. ,search: function(tf,newValue,oldValue) {
  156. var nv = newValue || tf;
  157. this.getStore().baseParams.search = Ext.isEmpty(nv) || Ext.isObject(nv) ? '' : nv;
  158. this.getBottomToolbar().changePage(1);
  159. //this.refresh();
  160. return true;
  161. }
  162. ,clearFilter: function() {
  163. this.getStore().baseParams = {
  164. action: 'workspace/packages/getList'
  165. };
  166. Ext.getCmp('modx-package-search').reset();
  167. this.getBottomToolbar().changePage(1);
  168. //this.refresh();
  169. }
  170. /* Main column renderer */
  171. ,mainColumnRenderer:function (value, metaData, record, rowIndex, colIndex, store){
  172. var rec = record.data;
  173. var state = (rec.installed !== null) ? ' installed' : ' not-installed';
  174. var values = { name: value, state: state, actions: null, message: null };
  175. var h = [];
  176. if(rec.installed !== null) {
  177. h.push({ className:'uninstall', text: rec.textaction });
  178. h.push({ className:'reinstall', text: _('package_reinstall_action_button') });
  179. } else {
  180. h.push({ className:'install primary-button', text: rec.textaction });
  181. }
  182. if (rec.updateable) {
  183. h.push({ className:'update orange', text: _('package_update_action_button') });
  184. } else {
  185. if( rec.provider != 0 ){
  186. h.push({ className:'checkupdate', text: _('package_check_for_updates') });
  187. }
  188. }
  189. h.push({ className:'remove', text: _('package_remove_action_button') });
  190. h.push({ className:'details', text: _('view_details') });
  191. values.actions = h;
  192. return this.mainColumnTpl.apply(values);
  193. }
  194. ,dateColumnRenderer: function(d,c) {
  195. switch(d) {
  196. case '':
  197. case null:
  198. c.css = 'not-installed';
  199. return _('not_installed');
  200. default:
  201. c.css = '';
  202. return _('installed_on',{'time':d});
  203. }
  204. }
  205. ,onClick: function(e){
  206. var t = e.getTarget();
  207. var elm = t.className.split(' ')[0];
  208. if(elm == 'controlBtn'){
  209. var act = t.className.split(' ')[1];
  210. var record = this.getSelectionModel().getSelected();
  211. this.menu.record = record.data;
  212. switch (act) {
  213. case 'remove':
  214. this.remove(record, e);
  215. break;
  216. case 'install':
  217. case 'reinstall':
  218. this.install(record);
  219. break;
  220. case 'uninstall':
  221. this.uninstall(record, e);
  222. break;
  223. case 'update':
  224. case 'checkupdate':
  225. this.update(record, e);
  226. break;
  227. case 'details':
  228. this.viewPackage(record, e);
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. }
  235. /* Install a package */
  236. ,install: function( record ){
  237. Ext.Ajax.request({
  238. url : MODx.config.connector_url
  239. ,params : {
  240. action : 'workspace/packages/getAttribute'
  241. ,attributes: 'license,readme,changelog,setup-options,requires'
  242. ,signature: record.data.signature
  243. }
  244. ,method: 'GET'
  245. ,scope: this
  246. ,success: function ( result, request ) {
  247. this.processResult( result.responseText, record );
  248. }
  249. ,failure: function ( result, request) {
  250. Ext.MessageBox.alert(_('failed'), result.responseText);
  251. }
  252. });
  253. }
  254. /* Go through the install process */
  255. ,processResult: function( response, record ){
  256. var data = Ext.util.JSON.decode( response );
  257. if ( data.object.license !== null && data.object.readme !== null && data.object.changelog !== null ){
  258. /* Show license/changelog panel */
  259. p = Ext.getCmp('modx-package-beforeinstall');
  260. p.activate();
  261. p.updatePanel( data.object, record );
  262. }
  263. else if ( data.object['setup-options'] !== null ) {
  264. /* No license/changelog, show setup-options */
  265. Ext.getCmp('package-show-setupoptions-btn').signature = record.data.signature;
  266. Ext.getCmp('modx-panel-packages').onSetupOptions();
  267. } else {
  268. /* No license/changelog, no setup-options, install directly */
  269. Ext.getCmp('package-install-btn').signature = record.data.signature;
  270. Ext.getCmp('modx-panel-packages').install();
  271. }
  272. }
  273. /* Launch Package Browser */
  274. ,onDownloadMoreExtra: function(btn,e){
  275. MODx.provider = MODx.defaultProvider;
  276. Ext.getCmp('modx-panel-packages-browser').activate();
  277. }
  278. ,changeProvider: function(btn, e){
  279. this.loadWindow(btn,e,{
  280. xtype: 'modx-package-changeprovider'
  281. });
  282. }
  283. /**
  284. * Open a window allowing user to upload a transport package directly
  285. */
  286. ,uploadTransportPackage: function(btn,e){
  287. if (!this.uploader) {
  288. this.uploader = new MODx.util.MultiUploadDialog.Dialog({
  289. url: MODx.config.connector_url
  290. ,base_params: {
  291. action: 'workspace/packages/upload'
  292. ,wctx: MODx.ctx || ''
  293. ,source: MODx.config.default_media_source
  294. ,path: MODx.config.core_path+'packages/'
  295. }
  296. ,permitted_extensions: ['zip']
  297. ,cls: 'ext-ux-uploaddialog-dialog modx-upload-window'
  298. });
  299. this.uploader.on('hide',function(){
  300. this.searchLocalWithoutPrompt();
  301. },this);
  302. this.uploader.on('close',function(){
  303. this.searchLocalWithoutPrompt();
  304. },this);
  305. }
  306. this.uploader.base_params.source = 1;
  307. this.uploader.show(btn);
  308. }
  309. ,searchLocal: function() {
  310. MODx.msg.confirm({
  311. title: _('package_search_local_title')
  312. ,text: _('package_search_local_confirm')
  313. ,url: MODx.config.connector_url
  314. ,params: {
  315. action: 'workspace/packages/scanLocal'
  316. }
  317. ,listeners: {
  318. 'success':{fn:function(r) {
  319. this.getStore().reload();
  320. },scope:this}
  321. }
  322. });
  323. }
  324. /**
  325. * Scan for new packages, without the pointless & annoying confirmation box
  326. */
  327. ,searchLocalWithoutPrompt: function(){
  328. MODx.Ajax.request({
  329. url: MODx.config.connector_url
  330. ,params: {
  331. action: 'workspace/packages/scanLocal'
  332. }
  333. ,listeners: {
  334. 'success':{fn:function(r) {
  335. this.getStore().reload();
  336. },scope:this}
  337. }
  338. })
  339. }
  340. /* Go to package details @TODO : Stay on the same page */
  341. ,viewPackage: function(btn,e) {
  342. MODx.loadPage('workspaces/package/view', 'signature='+this.menu.record.signature+'&package_name='+this.menu.record.name);
  343. }
  344. /* Search for a package update - only for installed package */
  345. ,update: function(btn,e) {
  346. if (this.windows['modx-window-package-update']) {
  347. this.windows['modx-window-package-update'].destroy();
  348. }
  349. MODx.Ajax.request({
  350. url: this.config.url
  351. ,params: {
  352. action: 'workspace/packages/update-remote'
  353. ,signature: this.menu.record.signature
  354. }
  355. ,listeners: {
  356. 'success': {fn:function(r) {
  357. this.loadWindow(btn,e,{
  358. xtype: 'modx-window-package-update'
  359. ,cls: 'modx-alert'
  360. ,packages: r.object
  361. ,record: this.menu.record
  362. ,force: true
  363. ,listeners: {
  364. 'success': {fn: function(o) {
  365. this.refresh();
  366. this.menu.record = {data:o.a.result.object};
  367. this.install(this.menu.record);
  368. },scope:this}
  369. }
  370. });
  371. },scope:this}
  372. ,'failure': {fn: function(r) {
  373. MODx.msg.alert(_('package_update'),r.message);
  374. return false;
  375. },scope:this}
  376. }
  377. });
  378. }
  379. /* Uninstall a package */
  380. ,uninstall: function(btn,e) {
  381. this.loadWindow(btn,e,{
  382. xtype: 'modx-window-package-uninstall'
  383. ,listeners: {
  384. success: {
  385. fn: function(va) {
  386. this._uninstall(this.menu.record,va,btn);
  387. }
  388. ,scope: this
  389. }
  390. }
  391. });
  392. }
  393. ,_uninstall: function(r,va,btn) {
  394. r = this.menu.record;
  395. va = va || {};
  396. var topic = '/workspace/package/uninstall/'+r.signature+'/';
  397. this.loadConsole(btn,topic);
  398. Ext.apply(va,{
  399. action: 'workspace/packages/uninstall'
  400. ,signature: r.signature
  401. ,register: 'mgr'
  402. ,topic: topic
  403. });
  404. MODx.Ajax.request({
  405. url: this.config.url
  406. ,params: va
  407. ,listeners: {
  408. 'success': {fn:function(r) {
  409. Ext.Msg.hide();
  410. this.refresh();
  411. Ext.getCmp('modx-layout').refreshTrees();
  412. },scope:this}
  413. ,'failure': {fn:function(r) {
  414. Ext.Msg.hide();
  415. this.refresh();
  416. },scope:this}
  417. }
  418. });
  419. }
  420. /* Remove a package entirely */
  421. ,remove: function(btn,e) {
  422. if (this.destroying) {
  423. return MODx.grid.Package.superclass.remove.apply(this, arguments);
  424. }
  425. var r = this.menu.record;
  426. var topic = '/workspace/package/remove/'+r.signature+'/';
  427. this.loadWindow(btn,e,{
  428. xtype: 'modx-window-package-remove'
  429. ,record: {
  430. signature: r.signature
  431. ,topic: topic
  432. ,register: 'mgr'
  433. }
  434. });
  435. }
  436. /* Purge old packages */
  437. ,purgePackages: function(btn,e) {
  438. var topic = '/workspace/packages/purge/';
  439. this.loadWindow(btn,e,{
  440. xtype: 'modx-window-packages-purge'
  441. ,record: {
  442. packagename: '*'
  443. ,topic: topic
  444. ,register: 'mgr'
  445. }
  446. ,listeners: {
  447. success: {fn: function(o) {
  448. this.refresh();
  449. },scope:this}
  450. }
  451. });
  452. }
  453. /* Load the console */
  454. ,loadConsole: function(btn,topic) {
  455. this.console = MODx.load({
  456. xtype: 'modx-console'
  457. ,register: 'mgr'
  458. ,topic: topic
  459. });
  460. this.console.show(btn);
  461. }
  462. ,getConsole: function() {
  463. return this.console;
  464. }
  465. });
  466. Ext.reg('modx-package-grid',MODx.grid.Package);
  467. MODx.window.PackageUpdate = function(config) {
  468. config = config || {};
  469. Ext.applyIf(config,{
  470. title: _('package_update')
  471. ,url: MODx.config.connector_url
  472. ,action: 'workspace/packages/rest/download'
  473. // ,height: 400
  474. // ,width: 400
  475. ,id: 'modx-window-package-update'
  476. ,saveBtnText: _('update')
  477. ,fields: this.setupOptions(config.packages,config.record)
  478. });
  479. MODx.window.PackageUpdate.superclass.constructor.call(this,config);
  480. this.on('hide',function() { this.destroy(); },this);
  481. };
  482. Ext.extend(MODx.window.PackageUpdate,MODx.Window,{
  483. setupOptions: function(ps,rec) {
  484. var items = [{
  485. html: _('package_update_to_version')
  486. ,border: false
  487. },{
  488. xtype: 'hidden'
  489. ,name: 'provider'
  490. ,value: Ext.isDefined(rec.provider) ? rec.provider : MODx.provider
  491. }];
  492. for (var i=0;i<ps.length;i=i+1) {
  493. var pkg = ps[i]
  494. ,label = pkg.signature;
  495. if (pkg.changelog) {
  496. // We have a changelog string, allow users to view it
  497. label += '<a href="javascript:;" class="changelog">'+ _('changelog') +'</a>';
  498. }
  499. items.push({
  500. xtype: 'radio'
  501. ,name: 'info'
  502. ,boxLabel: label
  503. ,itemCls: 'radio-version'
  504. ,hideLabel: true
  505. ,description: pkg.description
  506. ,inputValue: pkg.info
  507. ,labelSeparator: ''
  508. ,checked: i === 0
  509. ,listeners: {
  510. afterrender: {
  511. fn: function (radio) {
  512. var changelog = radio.container.query('.changelog')[0];
  513. if (!changelog) {
  514. return;
  515. }
  516. // When the changelog link is clicked, display the changelog in a window
  517. Ext.get(changelog).on('click', function(elem) {
  518. var win = MODx.load({
  519. xtype: 'modx-window'
  520. ,title: _('changelog')
  521. ,cls: 'modx-alert'
  522. ,width: 520
  523. ,style: 'white-space: pre-wrap'
  524. ,fields: [{
  525. xtype: 'box'
  526. ,html: pkg.changelog
  527. }]
  528. ,buttons: [{
  529. text: _('close')
  530. ,handler: function() {
  531. win.close();
  532. }
  533. }]
  534. });
  535. win.show();
  536. });
  537. }
  538. }
  539. }
  540. });
  541. }
  542. return items;
  543. }
  544. });
  545. Ext.reg('modx-window-package-update',MODx.window.PackageUpdate);