-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.js
More file actions
95 lines (86 loc) · 3.08 KB
/
Copy pathscript.js
File metadata and controls
95 lines (86 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
jQuery(function () {
/**
* Attach the click handler
*/
jQuery('.structstatus-full.editable').find('button.struct_status')
.click(function () {
const $self = jQuery(this);
$self.parent().css('visibility', 'hidden');
const set = makeDataSet($self.parent(), $self.data('rid'));
const data = {
sectok: $self.parent().data('st'),
field: $self.parent().data('field'),
pid: $self.parent().data('page'),
rid: $self.parent().data('rid'),
rev: $self.parent().data('rev'),
entry: set,
call: 'plugin_struct_inline_save'
};
jQuery.post(
DOKU_BASE + 'lib/exe/ajax.php',
data
)
.fail(function (jqXHR) {
console.log('error');
alert(jqXHR.responseText);
})
.done(function (response) {
const value = JSON.parse(response).value;
applyDataSet($self.parent(), set);
jQuery('#plugin__struct_output').find('td[data-struct="' + $self.parent().data('field') + '"]').html(value);
})
.always(function () {
$self.parent().css('visibility', 'visible');
})
;
})
;
/**
* Set the statuses according to the given set
*
* @param {jQuery} $full the wrapper around the statuses
* @param {Array} set the status or the list of statuses to enable
*/
function applyDataSet($full, set) {
$full.find('button.struct_status').each(function () {
if (typeof set == 'undefined') {
set = [];
}
const $self = jQuery(this);
if (set.indexOf(JSON.stringify($self.data('rid'))) === -1) {
$self.addClass('disabled');
} else {
$self.removeClass('disabled');
}
});
}
/**
* Create a set based on the current set and the status to toggle
*
* @param {jQuery} $full the wrapper around the statuses
* @param {[]} toggle the rid of the status to toggle
* @return {Array} the resulting new set
*/
function makeDataSet($full, toggle) {
const set = [];
let wason = false;
$full.find('button.struct_status').not('.disabled').each(function () {
const rid = jQuery(this).data('rid');
if (rid === toggle) {
wason = true; // this is the value we're toggling
} else {
set.push(rid); // this is an enabled value we keep
}
});
if (!wason) {
set.push(toggle); // value was not enabled previously, we toggle it
}
// for non-multi field only one value is allowed
if (!$full.data('multi')) {
return [JSON.stringify(set.pop())];
}
return set.map(function (entry) {
return JSON.stringify(entry);
});
}
});