summaryrefslogtreecommitdiffstats
path: root/theme/javascript/toolbar.js
diff options
context:
space:
mode:
Diffstat (limited to 'theme/javascript/toolbar.js')
-rw-r--r--theme/javascript/toolbar.js29
1 files changed, 25 insertions, 4 deletions
diff --git a/theme/javascript/toolbar.js b/theme/javascript/toolbar.js
index b987c88..49edc7c 100644
--- a/theme/javascript/toolbar.js
+++ b/theme/javascript/toolbar.js
@@ -6,6 +6,18 @@ var events = require('./events');
// List of created buttons
var buttons = [];
+// Insert a jquery element at a specific position
+function insertAt(parent, selector, index, element) {
+ var lastIndex = parent.children(selector).size();
+ if (index < 0) {
+ index = Math.max(0, lastIndex + 1 + index);
+ }
+ parent.append(element);
+
+ if (index < lastIndex) {
+ parent.children(selector).eq(index).before(parent.children(selector).last());
+ }
+}
// Default click handler
function defaultOnClick(e) {
@@ -83,7 +95,10 @@ function createButton(opts) {
onClick: defaultOnClick,
// Button is a dropdown
- dropdown: null
+ dropdown: null,
+
+ // Position in the toolbar
+ index: null
});
buttons.push(opts);
@@ -92,6 +107,7 @@ function createButton(opts) {
// Update a button
function updateButton(opts) {
+ var $result;
var $toolbar = $('.book-header');
var $title = $toolbar.find('h1');
@@ -133,12 +149,17 @@ function updateButton(opts) {
$menu.addClass('dropdown-'+(opts.position == 'right'? 'left' : 'right'));
$container.append($menu);
-
- $container.insertBefore($title);
+ $result = $container;
} else {
$btn.addClass(positionClass);
$btn.addClass(opts.className);
- $btn.insertBefore($title);
+ $result = $btn;
+ }
+
+ if (_.isNumber(opts.index) && opts.index >= 0) {
+ insertAt($toolbar, '.btn, .dropdown', opts.index, $result);
+ } else {
+ $result.insertBefore($title);
}
}