Skip to content

Commit

Permalink
#343 use the current user's preference to display the guide
Browse files Browse the repository at this point in the history
  • Loading branch information
dmcassel committed Nov 22, 2016
1 parent abb1d91 commit efff725
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/apidoc/controller/rewrite.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,9 @@ declare function m:rewrite()

else if (starts-with($PATH, '/xray')) then m:xray()

(: Support retrieving user preferences :)
else if ($PATH eq "/people/preferences") then "/controller/preferences.xqy"

(: Root request: "/" means "index.xml" inside the default version directory :)
else if ($PATH eq '/') then m:transform($ROOT-DOC-URL)

Expand Down
31 changes: 29 additions & 2 deletions src/apidoc/js/toc_filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ function tocInit() {
prerendered: true,
url: tocPartsDir }); });

// Set up the select widget
tocSelect.change(function(e) {
function changeSelection() {
LOG.debug('TOC select option changed');
// Hide the old TOC tree.
$(".apidoc_tree:visible").hide();
Expand All @@ -159,6 +158,34 @@ function tocInit() {
tree.show();

tocFilterUpdate();
}

// Set up the select widget
tocSelect.change(changeSelection);

$.ajax({
url: '/people/preferences',
method: 'GET',
dataType: 'json',
success: function(data) {
var preferredSection = data['doc-section'];
var select = document.querySelector('#toc_select');
var options = document.querySelectorAll('#toc_select option');
var initiallySelected = select.selectedIndex;
for (var i in options) {
if (options.hasOwnProperty(i)) {
if (options[i].text === preferredSection) {
select.selectedIndex = i;
}
}
}
if (initiallySelected !== select.selectedIndex) {
changeSelection();
}
},
error: function(error) {
console.log('error trying to get preferences: ' + JSON.stringify(error));
}
});

// Set up the filter
Expand Down
37 changes: 23 additions & 14 deletions src/controller/preferences.xqy
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@ xquery version "1.0-ml";

import module namespace users="users" at "/lib/users.xqy";

let $setting := xdmp:get-request-field("setting")
let $value := xdmp:get-request-field("value")
return
try {
users:set-preference(users:getCurrentUser(), $setting, $value),
"success"
} catch ($e) {
if ($e/error:name = "NO-USER") then
xdmp:set-response-code(401, $e/error:code/fn:string())
else if ($e/error:name = "INVALID-PREFERENCE") then
xdmp:set-response-code(400, $e/error:code/fn:string())
else
xdmp:rethrow()
}

declare variable $REQUEST := xdmp:get-request-method();

if ($REQUEST = "GET") then
users:get-prefs-as-json(users:getCurrentUser())

else if ($REQUEST = "POST") then
let $setting := xdmp:get-request-field("setting")
let $value := xdmp:get-request-field("value")
return
try {
users:set-preference(users:getCurrentUser(), $setting, $value),
"success"
} catch ($e) {
if ($e/error:name = "NO-USER") then
xdmp:set-response-code(401, $e/error:code/fn:string())
else if ($e/error:name = "INVALID-PREFERENCE") then
xdmp:set-response-code(400, $e/error:code/fn:string())
else
xdmp:rethrow()
}
else
xdmp:set-response-code(405, "Method Not Allowed")
14 changes: 14 additions & 0 deletions src/lib/users.xqy
Original file line number Diff line number Diff line change
Expand Up @@ -627,3 +627,17 @@ as xs:string?
return
$user/preferences/element()[fn:node-name(.) = $qn-pref]
};

declare function users:get-prefs-as-json($user as element(person)?)
as xs:string?
{
if ($user) then
"{" ||
fn:string-join(
for $pref in users:getCurrentUser()/preferences/element()
return ('"' || fn:node-name($pref) || '": "' || $pref/fn:string() || '"'),
","
)
||"}"
else ()
};

0 comments on commit efff725

Please sign in to comment.