Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow options to be customized per directory #696

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/_h5ai/private/php/ext/class-custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,42 @@ private function read_custom_file($path, $name, &$content, &$type) {
}
}

/**
* Load custom options for the specified path. Traverses the directory structure to include
* options for all parent directories too. Options in children will override the options
* inherited from their ancestors.
*
* @param $href string
* @return array<mixed>
*/
private function get_options(string $href) {
if (!$this->context->query_option('custom.enabled', false)) {
return [];
}

$file_prefix = $this->context->get_setup()->get('FILE_PREFIX');
$root_path = $this->context->get_setup()->get('ROOT_PATH');

// Find all options files, from the current path all the way to the root
$option_files = [];
$path = $this->context->to_path($href);
do {
$file = $path . '/' . $file_prefix . '.options.json';
if (is_readable($file)) {
$option_files[] = Json::load($file);
}
$path = Util::normalize_path(dirname($path));
} while ($path !== $root_path && $path !== '/' && $href !== '/');

return count($option_files) === 0 ? [] : array_merge(...array_reverse($option_files));
}

public function get_customizations($href) {
if (!$this->context->query_option('custom.enabled', false)) {
return [
'header' => ['content' => null, 'type' => null],
'footer' => ['content' => null, 'type' => null]
'footer' => ['content' => null, 'type' => null],
'options' => (object)[],
];
}

Expand Down Expand Up @@ -59,7 +90,8 @@ public function get_customizations($href) {

return [
'header' => ['content' => $header, 'type' => $header_type],
'footer' => ['content' => $footer, 'type' => $footer_type]
'footer' => ['content' => $footer, 'type' => $footer_type],
'options' => (object)self::get_options($href),
];
}
}
1 change: 1 addition & 0 deletions src/_h5ai/public/js/lib/ext/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const onLocationChanged = item => {
server.request({action: 'get', custom: item.absHref}).then(response => {
const data = response && response.custom;
each(['header', 'footer'], key => update(data, key));
event.pub('custom.optionsLoaded', data.options);
});
};

Expand Down
63 changes: 50 additions & 13 deletions src/_h5ai/public/js/lib/view/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const settings = Object.assign({
setParentFolderLabels: false,
sizes
}, allsettings.view);
let folderSettings = null;
const sortedSizes = settings.sizes.sort((a, b) => a - b);
const checkedModes = intersection(settings.modes, modes);
const storekey = 'view';
Expand Down Expand Up @@ -86,15 +87,32 @@ const addCssStyles = () => {
dom('<style></style>').text(styles.join('\n')).appTo('head');
};

const set = (mode, size) => {
const stored = store.get(storekey);
// Gets the first truthy value in `candidates` that is also listed in `validOptions`
const findFirstValid = (candidates, validOptions) => candidates.find(x => x && includes(validOptions, x));

mode = mode || stored && stored.mode;
size = size || stored && stored.size;
mode = includes(settings.modes, mode) ? mode : settings.modes[0];
size = includes(settings.sizes, size) ? size : settings.sizes[0];
store.put(storekey, {mode, size});
const getModes = () => checkedModes;
const getMode = () => findFirstValid(
[
(store.get(storekey) && store.get(storekey).mode),
(folderSettings && folderSettings.mode),
settings.modes[0]
],
settings.modes
);

const getSizes = () => sortedSizes;
const getSize = () => findFirstValid(
[
(store.get(storekey) && store.get(storekey).size),
(folderSettings && folderSettings.size),
settings.sizes[0]
],
settings.sizes
);

const updateView = () => {
const mode = getMode();
const size = getSize();
each(checkedModes, m => {
if (m === mode) {
$view.addCls('view-' + m);
Expand All @@ -114,12 +132,25 @@ const set = (mode, size) => {
event.pub('view.mode.changed', mode, size);
};

const getModes = () => checkedModes;
const getMode = () => store.get(storekey).mode;
const setMode = mode => set(mode, null);
const set = (mode, size) => {
const stored = store.get(storekey);

const getSizes = () => sortedSizes;
const getSize = () => store.get(storekey).size;
mode = mode || stored && stored.mode;
size = size || stored && stored.size;

// Unset if it's being set back to the default value
if (mode === settings.modes[0]) {
mode = undefined;
}
if (size === settings.sizes[0]) {
size = undefined;
}

store.put(storekey, {mode, size});
updateView();
};

const setMode = mode => set(mode, null);
const setSize = size => set(null, size);

const onMouseenter = ev => {
Expand Down Expand Up @@ -258,9 +289,14 @@ const onResize = () => {
}
};

const onCustomOptionsLoaded = options => {
folderSettings = options && options.view;
updateView();
};

const init = () => {
addCssStyles();
set();
updateView();

$view.appTo(base.$content);
$hint.hide();
Expand All @@ -270,6 +306,7 @@ const init = () => {
event.sub('location.changed', onLocationChanged);
event.sub('location.refreshed', onLocationRefreshed);
event.sub('resize', onResize);
event.sub('custom.optionsLoaded', onCustomOptionsLoaded);
onResize();
};

Expand Down