Skip to content

Commit

Permalink
Fix messed up #16394 merge (my bad...)
Browse files Browse the repository at this point in the history
Merge remote-tracking branch 'upstream/pr/16394' into 3.x

# By bezumkin
# Via bezumkin
* upstream/pr/16394:
  Use History API to save path of Media Browser

# Conflicts:
#	manager/assets/modext/util/utilities.js
#	manager/assets/modext/widgets/media/modx.browser.js
  • Loading branch information
Mark-H committed Feb 10, 2024
2 parents dcf11a9 + 7fb2af1 commit d37e0a1
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 15 deletions.
3 changes: 2 additions & 1 deletion manager/assets/modext/util/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ MODx.util.Progress = {
}
};

MODx.util.History = {

MODx.util.UrlParams = {
get() {
return this.parse(window.location.search)
},
Expand Down
13 changes: 13 additions & 0 deletions manager/assets/modext/widgets/core/tree/modx.tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,19 @@ Ext.extend(MODx.tree.Tree,Ext.tree.TreePanel,{
return langs;
}

,expandTreePath(dir = '/') {
const root = this.getRootNode().getPath('text')
const path = root.replace(/\/$/, '') + '/' + dir.replace(/^\//, '')
this.expandPath(path, 'text', () => {
let node = this.getNodeById(encodeURIComponent(dir))
if (!node) {
node = this.getRootNode()
}
node.select()
this.cm.activeNode = node
})
}

});
Ext.reg('modx-tree',MODx.tree.Tree);

Expand Down
101 changes: 87 additions & 14 deletions manager/assets/modext/widgets/media/modx.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,10 +522,11 @@ MODx.browser.Window = function(config) {
this.view.run();
}
,scope: this
,source: config.source || MODx.config.default_media_source
,source: config.source || MODx.util.UrlParams.get().source || MODx.config.default_media_source
,originalSource: config.source || MODx.config.default_media_source
,hideFiles: config.hideFiles || MODx.config.modx_browser_tree_hide_files
,hideTooltips: config.hideTooltips || MODx.config.modx_browser_tree_hide_tooltips || true // by default do not request image preview tooltips in the media browser
,openTo: config.openTo || ''
,openTo: config.openTo || MODx.util.UrlParams.get().dir || ''
,ident: this.ident
,rootIconCls: MODx.config.mgr_source_icon
,rootId: config.rootId || '/'
Expand Down Expand Up @@ -566,6 +567,7 @@ MODx.browser.Window = function(config) {
this.view.baseParams.source = s;
this.view.dir = '/';
this.view.run();
MODx.util.UrlParams.set({...MODx.util.UrlParams.get(), source: s, dir: '/'})
}
,scope: this
}
Expand Down Expand Up @@ -594,6 +596,15 @@ MODx.browser.Window = function(config) {
}
,scope: this
}
,'load': {
fn: function() {
const dir = MODx.util.UrlParams.get().dir
if (dir) {
this.tree.expandTreePath(dir)
}
}
,scope: this
}
}
});

Expand All @@ -604,16 +615,21 @@ MODx.browser.Window = function(config) {
fn: this.onSelect
,scope: this
}
,source: config.source || MODx.config.default_media_source
,source: config.source || MODx.util.UrlParams.get().source || MODx.config.default_media_source
,allowedFileTypes: config.allowedFileTypes || ''
,wctx: config.wctx || 'web'
,openTo: config.openTo || ''
,openTo: config.openTo || MODx.util.UrlParams.get().dir || ''
,multiSelect: config.multiSelect || false
,ident: this.ident
,id: this.ident+'-view'
,tree: this.tree
});

// Add event to reload on History change
window.onpopstate = (e) => {
MODx.browser.onPopState(e, this)
}

Ext.applyIf(config,{
title: _('modx_browser')+' ('+(MODx.ctx ? MODx.ctx : 'web')+')'
,cls: 'modx-browser modx-browser-window'
Expand Down Expand Up @@ -673,8 +689,14 @@ MODx.browser.Window = function(config) {
MODx.browser.Window.superclass.constructor.call(this,config);
this.config = config;
this.addEvents({
'select': true
'select': true,
'hide': true,
});

this.on('hide', () => {
MODx.util.UrlParams.remove('source')
MODx.util.UrlParams.remove('dir')
})
};
Ext.extend(MODx.browser.Window,Ext.Window,{
returnEl: null
Expand All @@ -696,6 +718,8 @@ Ext.extend(MODx.browser.Window,Ext.Window,{
*/
,load: function(dir) {
dir = dir || (Ext.isEmpty(this.config.openTo) ? '' : this.config.openTo);
MODx.util.UrlParams.set({...MODx.util.UrlParams.get(), dir: decodeURIComponent(dir)})

this.view.run({
dir: dir
,source: this.config.source
Expand Down Expand Up @@ -916,10 +940,11 @@ MODx.Media = function(config) {
this.view.run();
}
,scope: this
,source: MODx.util.History.get().source || config.source || MODx.config.default_media_source
,source: config.source || MODx.util.UrlParams.get().source || MODx.config.default_media_source
,originalSource: config.source || MODx.config.default_media_source
,hideFiles: config.hideFiles || MODx.config.modx_browser_tree_hide_files
,hideTooltips: config.hideTooltips || MODx.config.modx_browser_tree_hide_tooltips || true // by default do not request image preview tooltips in the media browser
,openTo: MODx.util.History.get().dir || config.openTo || ''
,openTo: config.openTo || MODx.util.UrlParams.get().dir || ''
,ident: this.ident
,rootIconCls: MODx.config.mgr_source_icon
,rootId: config.rootId || '/'
Expand Down Expand Up @@ -960,7 +985,7 @@ MODx.Media = function(config) {
this.view.baseParams.source = s;
this.view.dir = '/';
this.view.run();
MODx.util.History.set({...MODx.util.History.get(), source: s, dir: '/'})
MODx.util.UrlParams.set({...MODx.util.UrlParams.get(), source: s, dir: '/'})
}
,scope: this
}
Expand Down Expand Up @@ -1006,6 +1031,15 @@ MODx.Media = function(config) {
}
,scope: this
}
,'load': {
fn: function() {
const dir = MODx.util.UrlParams.get().dir
if (dir) {
this.tree.expandTreePath(dir)
}
}
,scope: this
}
}
});

Expand All @@ -1016,19 +1050,20 @@ MODx.Media = function(config) {
fn: this.onSelect
,scope: this
}
,source: MODx.util.History.get().source || config.source || MODx.config.default_media_source
,source: config.source || MODx.util.UrlParams.get().source || MODx.config.default_media_source
,originalSource: config.source || MODx.config.default_media_source
,allowedFileTypes: config.allowedFileTypes || ''
,wctx: config.wctx || 'web'
,openTo: MODx.util.History.get().dir || config.openTo || ''
,openTo: config.openTo || MODx.util.UrlParams.get().dir || ''
,ident: this.ident
,id: this.ident+'-view'
,tree: this.tree
});

// Add event to reload on History change
window.addEventListener('popstate', () => {
window.location.reload()
})
window.onpopstate = (e) => {
MODx.browser.onPopState(e, this)
}

Ext.applyIf(config, {
cls: 'modx-browser modx-browser-panel'
Expand Down Expand Up @@ -1088,7 +1123,7 @@ Ext.extend(MODx.Media, Ext.Container, {
*/
,load: function(dir) {
dir = dir || (Ext.isEmpty(this.config.openTo) ? '' : this.config.openTo);
MODx.util.History.set({...MODx.util.History.get(), dir: decodeURIComponent(dir)})
MODx.util.UrlParams.set({...MODx.util.UrlParams.get(), dir: decodeURIComponent(dir)})

this.view.run({
dir: dir
Expand Down Expand Up @@ -1673,3 +1708,41 @@ Ext.extend(MODx.browser.RTE,Ext.Viewport,{
}
});
Ext.reg('modx-browser-rte',MODx.browser.RTE);

MODx.browser.onPopState = function ({state}, {tree, view}) {
const params = MODx.util.UrlParams.parse(state)
const defaultSource = view.config.originalSource
const source = params.source || defaultSource

if (tree.sourceCombo && !tree.config.hideSourceCombo && source !== tree.sourceCombo.getValue()) {
tree.config.source = source
tree.baseParams.source = source
tree.dir = params.dir || '/'
tree.sourceCombo.getStore().load({
scope: tree,
callback() {
tree.sourceCombo.setValue(source)
tree.refresh()
const root = tree.getRootNode();
if (root) {
root.setText(tree.sourceCombo.getRawValue());
}
view.run({
dir: params.dir || '/',
source,
allowedFileTypes: view.config.allowedFileTypes || '',
wctx: view.config.wctx || 'web',
})
tree.expandTreePath(params.dir || '/')
},
})
} else {
view.run({
dir: params.dir || '/',
source,
allowedFileTypes: view.config.allowedFileTypes || '',
wctx: view.config.wctx || 'web',
})
tree.expandTreePath(params.dir || '/')
}
}

0 comments on commit d37e0a1

Please sign in to comment.