• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

divio / django-cms / #30078

11 Nov 2025 02:05PM UTC coverage: 90.241% (-0.01%) from 90.251%
#30078

push

travis-ci

web-flow
Merge 03851acbd into 609c5e043

1266 of 1993 branches covered (63.52%)

20 of 29 new or added lines in 4 files covered. (68.97%)

271 existing lines in 4 files now uncovered.

8942 of 9909 relevant lines covered (90.24%)

11.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.26
/cms/static/cms/js/modules/cms.toolbar.js
1
/*
2
 * Copyright https://github.com/divio/django-cms
3
 */
4

5
import $ from 'jquery';
6
import Class from 'classjs';
7
import Navigation from './cms.navigation';
8
import Sideframe from './cms.sideframe';
9
import Modal from './cms.modal';
10
import Plugin from './cms.plugins';
11
import { filter, throttle, uniq } from 'lodash';
12
import { showLoader, hideLoader } from './loader';
13
import { Helpers, KEYS } from './cms.base';
14

15
var SECOND = 1000;
1✔
16
var TOOLBAR_OFFSCREEN_OFFSET = 10; // required to hide box-shadow
1✔
17

18
export const getPlaceholderIds = pluginRegistry =>
1✔
19
    uniq(filter(pluginRegistry, ([, opts]) => opts.type === 'placeholder').map(([, opts]) => opts.placeholder_id));
2✔
20

21
/**
22
 * @function hideDropdownIfRequired
23
 * @private
24
 * @param {jQuery} publishBtn
25
 */
26
function hideDropdownIfRequired(publishBtn) {
27
    var dropdown = publishBtn.closest('.cms-dropdown');
1✔
28

29
    if (dropdown.length && dropdown.find('li[data-cms-hidden]').length === dropdown.find('li').length) {
1!
30
        dropdown.hide().attr('data-cms-hidden', 'true');
×
31
    }
32
}
33

34
/**
35
 * The toolbar is the generic element which holds various components
36
 * together and provides several commonly used API methods such as
37
 * show/hide, message display or loader indication.
38
 *
39
 * @class Toolbar
40
 * @namespace CMS
41
 * @uses CMS.API.Helpers
42
 */
43
var Toolbar = new Class({
1✔
44
    implement: [Helpers],
45

46
    options: {
47
        toolbarDuration: 200
48
    },
49

50
    initialize: function initialize(options) {
51
        this.options = $.extend(true, {}, this.options, options);
38✔
52

53
        // elements
54
        this._setupUI();
38✔
55

56
        /**
57
         * @property {CMS.Navigation} navigation
58
         */
59
        this.navigation = new Navigation();
38✔
60

61
        /**
62
         * @property {Object} _position
63
         * @property {Number} _position.top current position of the toolbar
64
         * @property {Number} _position.top position when toolbar became non-sticky
65
         * @property {Boolean} _position.isSticky is toolbar sticky?
66
         * @see _handleLongMenus
67
         * @private
68
         */
69
        this._position = {
38✔
70
            top: 0,
71
            stickyTop: 0,
72
            isSticky: true
73
        };
74

75
        // states
76
        this.click = 'click.cms.toolbar';
38✔
77
        this.touchStart = 'touchstart.cms.toolbar';
38✔
78
        this.pointerUp = 'pointerup.cms.toolbar';
38✔
79
        this.pointerOverOut = 'pointerover.cms.toolbar pointerout.cms.toolbar';
38✔
80
        this.pointerLeave = 'pointerleave.cms.toolbar';
38✔
81
        this.mouseEnter = 'mouseenter.cms.toolbar';
38✔
82
        this.mouseLeave = 'mouseleave.cms.toolbar';
38✔
83
        this.resize = 'resize.cms.toolbar';
38✔
84
        this.scroll = 'scroll.cms.toolbar';
38✔
85
        this.key = 'keydown.cms.toolbar keyup.cms.toolbar';
38✔
86

87
        // istanbul ignore next: function is always reassigned
88
        this.timer = function() {};
89
        this.lockToolbar = false;
38✔
90

91
        // setup initial stuff
92
        if (!this.ui.toolbar.data('ready')) {
38✔
93
            this._events();
36✔
94
        }
95

96
        this._initialStates();
38✔
97

98
        // set a state to determine if we need to reinitialize this._events();
99
        this.ui.toolbar.data('ready', true);
38✔
100
    },
101

102
    /**
103
     * Stores all jQuery references within `this.ui`.
104
     *
105
     * @method _setupUI
106
     * @private
107
     */
108
    _setupUI: function _setupUI() {
109
        var container = $('.cms');
38✔
110

111
        this.ui = {
38✔
112
            container: container,
113
            body: $('html'),
114
            document: $(document),
115
            window: $(window),
116
            toolbar: container.find('.cms-toolbar'),
117
            navigations: container.find('.cms-toolbar-item-navigation'),
118
            buttons: container.find('.cms-toolbar-item-buttons'),
119
            messages: container.find('.cms-messages'),
120
            structureBoard: container.find('.cms-structure'),
121
            toolbarSwitcher: $('.cms-toolbar-item-cms-mode-switcher'),
122
            revert: $('.cms-toolbar-revert')
123
        };
124
    },
125

126
    /**
127
     * Sets up all the event handlers, such as closing and resizing.
128
     *
129
     * @method _events
130
     * @private
131
     */
132
    _events: function _events() {
133
        var that = this;
36✔
134
        var LONG_MENUS_THROTTLE = 10;
36✔
135

136
        // attach event to the navigation elements
137
        this.ui.navigations.each(function() {
36✔
138
            var navigation = $(this);
72✔
139
            var lists = navigation.find('li');
72✔
140
            var root = 'cms-toolbar-item-navigation';
72✔
141
            var hover = 'cms-toolbar-item-navigation-hover';
72✔
142
            var disabled = 'cms-toolbar-item-navigation-disabled';
72✔
143
            var children = 'cms-toolbar-item-navigation-children';
72✔
144
            var isTouchingTopLevelMenu = false;
72✔
145
            var open = false;
72✔
146
            var cmdPressed = false;
72✔
147

148
            /**
149
             * Resets all the hover state classes and events
150
             * @function reset
151
             */
152
            function reset() {
153
                open = false;
6✔
154
                cmdPressed = false;
6✔
155
                lists.removeClass(hover);
6✔
156
                lists.find('ul ul').hide();
6✔
157
                navigation.find('> li').off(that.mouseEnter);
6✔
158
                that.ui.document.off(that.click);
6✔
159
                that.ui.toolbar.off(that.click, reset);
6✔
160
                that.ui.structureBoard.off(that.click);
6✔
161
                that.ui.window.off(that.resize + '.menu.reset');
6✔
162
                that._handleLongMenus();
6✔
163
            }
164

165
            that.ui.window.on('keyup.cms.toolbar', function(e) {
72✔
166
                if (e.keyCode === CMS.KEYS.ESC) {
1,622!
167
                    reset();
×
168
                }
169
            });
170

171
            navigation
72✔
172
                .find('> li > a')
173
                .add(that.ui.toolbar.find('.cms-toolbar-item:not(.cms-toolbar-item-navigation) > a'))
174
                .off('keyup.cms.toolbar.reset')
175
                .on('keyup.cms.toolbar.reset', function(e) {
176
                    if (e.keyCode === CMS.KEYS.TAB) {
×
177
                        reset();
×
178
                    }
179
                });
180

181
            // remove events from first level
182
            navigation
72✔
183
                .find('a')
184
                // eslint-disable-next-line complexity
185
                .on(that.click + ' ' + that.key, function(e) {
186
                    var el = $(this);
7✔
187

188
                    // we need to restore the default behaviour once a user
189
                    // presses ctrl/cmd and clicks on the entry. In this
190
                    // case a new tab should open. First we determine if
191
                    // ctrl/cmd is pressed:
192
                    if (
7✔
193
                        e.keyCode === KEYS.CMD_LEFT ||
35✔
194
                        e.keyCode === KEYS.CMD_RIGHT ||
195
                        e.keyCode === KEYS.CMD_FIREFOX ||
196
                        e.keyCode === KEYS.SHIFT ||
197
                        e.keyCode === KEYS.CTRL
198
                    ) {
199
                        cmdPressed = true;
2✔
200
                    }
201
                    if (e.type === 'keyup') {
7✔
202
                        cmdPressed = false;
1✔
203
                    }
204

205
                    if (el.attr('href') !== '' && el.attr('href') !== '#' && !el.parent().hasClass(disabled)) {
7✔
206
                        if (cmdPressed && e.type === 'click') {
3✔
207
                            // control the behaviour when ctrl/cmd is pressed
208
                            Helpers._getWindow().open(el.attr('href'), '_blank');
1✔
209
                        } else if (e.type === 'click') {
2✔
210
                            // otherwise delegate as usual
211
                            that._delegate($(this));
1✔
212
                        } else {
213
                            // tabbing through
214
                            return;
1✔
215
                        }
216

217
                        reset();
2✔
218
                        return false;
2✔
219
                    }
220
                })
221
                .on(that.touchStart, function() {
222
                    isTouchingTopLevelMenu = true;
4✔
223
                });
224

225
            // handle click states
226
            lists.on(that.click, function(e) {
72✔
227
                e.preventDefault();
9✔
228
                e.stopPropagation();
9✔
229
                var el = $(this);
9✔
230

231
                // close navigation once it's pressed again
232
                if (el.parent().hasClass(root) && open) {
9✔
233
                    that.ui.body.trigger(that.click);
1✔
234
                    return false;
1✔
235
                }
236

237
                // close if el does not have children
238
                if (!el.hasClass(children)) {
8✔
239
                    reset();
1✔
240
                }
241

242
                var isRootNode = el.parent().hasClass(root);
8✔
243

244
                if ((isRootNode && el.hasClass(hover)) || (el.hasClass(disabled) && !isRootNode)) {
8!
UNCOV
245
                    return false;
×
246
                }
247

248
                el.addClass(hover);
8✔
249
                that._handleLongMenus();
8✔
250

251
                // activate hover selection
252
                if (!isTouchingTopLevelMenu) {
8✔
253
                    // we only set the handler for mouseover when not touching because
254
                    // the mouseover actually is triggered on touch devices :/
255
                    navigation.find('> li').on(that.mouseEnter, function() {
7✔
256
                        // cancel if item is already active
257
                        if ($(this).hasClass(hover)) {
4✔
258
                            return false;
2✔
259
                        }
260
                        open = false;
2✔
261
                        $(this).trigger(that.click);
2✔
262
                    });
263
                }
264

265
                isTouchingTopLevelMenu = false;
8✔
266
                // create the document event
267
                that.ui.document.on(that.click, reset);
8✔
268
                that.ui.structureBoard.on(that.click, reset);
8✔
269
                that.ui.toolbar.on(that.click, reset);
8✔
270
                that.ui.window.on(that.resize + '.menu.reset', throttle(reset, SECOND));
8✔
271
                // update states
272
                open = true;
8✔
273
            });
274

275
            // attach hover
276
            lists
72✔
277
                // eslint-disable-next-line complexity
278
                .on(that.pointerOverOut + ' keyup.cms.toolbar', 'li', function(e) {
279
                    var el = $(this);
2✔
280
                    var parent = el
2✔
281
                        .closest('.cms-toolbar-item-navigation-children')
282
                        .add(el.parents('.cms-toolbar-item-navigation-children'));
283
                    var hasChildren = el.hasClass(children) || parent.length;
2✔
284

285
                    // do not attach hover effect if disabled
286
                    // cancel event if element has already hover class
287
                    if (el.hasClass(disabled)) {
2!
UNCOV
288
                        e.stopPropagation();
×
UNCOV
289
                        return;
×
290
                    }
291
                    if (el.hasClass(hover) && e.type !== 'keyup') {
2!
UNCOV
292
                        return true;
×
293
                    }
294

295
                    // reset
296
                    lists.find('li').removeClass(hover);
2✔
297

298
                    // add hover effect
299
                    el.addClass(hover);
2✔
300

301
                    // handle children elements
302
                    if (
2✔
303
                        (hasChildren && e.type !== 'keyup') ||
7✔
304
                        (hasChildren && e.type === 'keyup' && e.keyCode === CMS.KEYS.ENTER)
305
                    ) {
306
                        el.find('> ul').show();
1✔
307
                        // add parent class
308
                        parent.addClass(hover);
1✔
309
                        that._handleLongMenus();
1✔
310
                    } else if (e.type !== 'keyup') {
1!
UNCOV
311
                        lists.find('ul ul').hide();
×
UNCOV
312
                        that._handleLongMenus();
×
313
                    }
314

315
                    // Remove stale submenus
316
                    el.siblings().find('> ul').hide();
2✔
317
                })
318
                .on(that.click, function(e) {
319
                    e.preventDefault();
9✔
320
                    e.stopPropagation();
9✔
321
                });
322

323
            // fix leave event
324
            lists.on(that.pointerLeave, '> ul', function() {
72✔
UNCOV
325
                lists.find('li').removeClass(hover);
×
326
            });
327
        });
328

329
        // attach event for first page publish
330
        this.ui.buttons.each(function() {
36✔
331
            var btn = $(this);
108✔
332
            var links = btn.find('a');
108✔
333

334
            links.each(function(i, el) {
108✔
335
                var link = $(el);
108✔
336

337
                // in case the button has a data-rel attribute
338
                if (link.attr('data-rel') || link.hasClass('cms-form-post-method')) {
108✔
339
                    link.off(that.click).on(that.click, function(e) {
36✔
340
                        e.preventDefault();
1✔
341
                        that._delegate($(this));
1✔
342
                    });
343
                } else {
344
                    link.off(that.click).on(that.click, function(e) {
72✔
345
                        e.stopPropagation();
1✔
346
                    });
347
                }
348
            });
349
        });
350

351
        this.ui.window
36✔
352
            .off([this.resize, this.scroll].join(' '))
353
            .on(
354
                [this.resize, this.scroll].join(' '),
355
                throttle($.proxy(this._handleLongMenus, this), LONG_MENUS_THROTTLE)
356
            );
357
    },
358

359
    /**
360
     * We check for various states on load if elements in the toolbar
361
     * should appear or trigger other components. This precedes a timeout
362
     * which is not optimal and should be addressed separately.
363
     *
364
     * @method _initialStates
365
     * @private
366
     * @deprecated this method is deprecated now, it will be removed in > 3.2
367
     */
368
    // eslint-disable-next-line complexity
369
    _initialStates: function _initialStates() {
370
        var publishBtn = $('.cms-btn-publish').parent();
1✔
371

372
        this._show({ duration: 0 });
1✔
373

374
        // hide publish button
375
        publishBtn.hide().attr('data-cms-hidden', 'true');
1✔
376

377
        if ($('.cms-btn-publish-active').length) {
1!
378
            publishBtn.show().removeAttr('data-cms-hidden');
1✔
379
            this.ui.window.trigger('resize');
1✔
380
        }
381

382
        hideDropdownIfRequired(publishBtn);
1✔
383

384
        // check if debug is true
385
        if (CMS.config.debug) {
1!
UNCOV
386
            this._debug();
×
387
        }
388

389
        // check if there are messages and display them
390
        if (CMS.config.messages) {
1!
UNCOV
391
            CMS.API.Messages.open({
×
392
                message: CMS.config.messages
393
            });
394
        }
395

396
        // check if there are error messages and display them
397
        if (CMS.config.error) {
1!
UNCOV
398
            CMS.API.Messages.open({
×
399
                message: CMS.config.error,
400
                error: true
401
            });
402
        }
403

404
        // should switcher indicate that there is an unpublished page?
405
        if (CMS.config.publisher) {
1!
UNCOV
406
            CMS.API.Messages.open({
×
407
                message: CMS.config.publisher,
408
                dir: 'right'
409
            });
410
        }
411

412
        // open sideframe if it was previously opened and it's enabled
413
        var sideFrameEnabled = typeof CMS.settings.sideframe_enabled === 'undefined' || CMS.settings.sideframe_enabled;
1✔
414

415
        if (CMS.settings.sideframe
1!
416
            && CMS.settings.sideframe.url
417
            && CMS.config.auth
418
            && sideFrameEnabled
419
        ) {
UNCOV
420
            var sideframe = CMS.API.Sideframe || new Sideframe();
×
421

422
            sideframe.open({
×
423
                url: CMS.settings.sideframe.url,
424
                animate: false
425
            });
426
        }
427

428
        // set color scheme
429
        Helpers.setColorScheme (
1✔
430
            localStorage.getItem('theme') || CMS.config.color_scheme || 'auto'
1!
431
        );
432

433
        // add toolbar ready class to body and fire event
434
        this.ui.body.addClass('cms-ready');
1✔
435
        this.ui.document.trigger('cms-ready');
1✔
436
    },
437

438
    /**
439
     * Animation helper for opening the toolbar.
440
     *
441
     * @method _show
442
     * @private
443
     * @param {Object} [opts]
444
     * @param {Number} [opts.duration] time in milliseconds for toolbar to animate
445
     */
446
    _show: function _show(opts) {
447
        var that = this;
1✔
448
        var speed = opts && opts.duration !== undefined ? opts.duration : this.options.toolbarDuration;
1!
449
        var toolbarHeight = $('.cms-toolbar').height() + TOOLBAR_OFFSCREEN_OFFSET;
1✔
450

451
        this.ui.body.addClass('cms-toolbar-expanding');
1✔
452
        // animate html
453
        this.ui.body.animate(
1✔
454
            {
455
                'margin-top': toolbarHeight - TOOLBAR_OFFSCREEN_OFFSET
456
            },
457
            speed,
458
            'linear',
459
            function() {
460
                that.ui.body.removeClass('cms-toolbar-expanding');
1✔
461
                that.ui.body.addClass('cms-toolbar-expanded');
1✔
462
            }
463
        );
464
        // set messages top to toolbar height
465
        this.ui.messages.css('top', toolbarHeight - TOOLBAR_OFFSCREEN_OFFSET);
1✔
466
    },
467

468
    /**
469
     * Makes a request to the given url, runs optional callbacks.
470
     *
471
     * @method openAjax
472
     * @param {Object} opts
473
     * @param {String} opts.url url where the ajax points to
474
     * @param {String} [opts.post] post data to be passed (must be stringified JSON)
475
     * @param {String} [opts.method='POST'] ajax method
476
     * @param {String} [opts.text] message to be displayed
477
     * @param {Function} [opts.callback] custom callback instead of reload
478
     * @param {String} [opts.onSuccess] reload and display custom message
479
     * @returns {Boolean|jQuery.Deferred} either false or a promise
480
     */
481
    openAjax: function(opts) {
482
        var that = this;
14✔
483
        // url, post, text, callback, onSuccess
484
        var url = opts.url;
14✔
485
        var post = opts.post || '{}';
14✔
486
        var text = opts.text || '';
14✔
487
        var callback = opts.callback;
14✔
488
        var method = opts.method || 'POST';
14✔
489
        var onSuccess = opts.onSuccess;
14✔
490
        var question = text ? Helpers.secureConfirm(text) : true;
14✔
491

492
        // cancel if question has been denied
493
        if (!question) {
14✔
494
            return false;
1✔
495
        }
496

497
        showLoader();
13✔
498

499
        return $.ajax({
13✔
500
            type: method,
501
            url: url,
502
            data: JSON.parse(post)
503
        })
504
            .done(function(response) {
505
                CMS.API.locked = false;
8✔
506

507
                if (callback) {
8✔
508
                    callback(that, response);
2✔
509
                    hideLoader();
2✔
510
                } else if (onSuccess) {
6✔
511
                    if (onSuccess === 'FOLLOW_REDIRECT') {
4✔
512
                        Helpers.reloadBrowser(response.url);
1✔
513
                    } else {
514
                        Helpers.reloadBrowser(onSuccess);
3✔
515
                    }
516
                } else {
517
                    // reload
518
                    Helpers.reloadBrowser();
2✔
519
                }
520
            })
521
            .fail(function(jqXHR) {
522
                CMS.API.locked = false;
2✔
523

524
                CMS.API.Messages.open({
2✔
525
                    message: jqXHR.responseText + ' | ' + jqXHR.status + ' ' + jqXHR.statusText,
526
                    error: true
527
                });
528
            });
529
    },
530

531
    /**
532
     * Public api for `./loader.js`
533
     */
534
    showLoader: function () {
UNCOV
535
        showLoader();
×
536
    },
537

538
    hideLoader: function () {
UNCOV
539
        hideLoader();
×
540
    },
541

542
    /**
543
     * Delegates event from element to appropriate functionalities.
544
     *
545
     * @method _delegate
546
     * @param {jQuery} el trigger element
547
     * @private
548
     * @returns {Boolean|void}
549
     */
550
    _delegate: function _delegate(el) {
551
        // save local vars
552
        var target = el.data('rel');
7✔
553

554
        if (el.hasClass('cms-btn-disabled')) {
7✔
555
            return false;
1✔
556
        }
557

558
        switch (target) {
6!
559
            case 'modal':
560
                Plugin._removeAddPluginPlaceholder();
1✔
561

562
                var modal = new Modal({
1✔
563
                    onClose: el.data('on-close')
564
                });
565

566
                modal.open({
1✔
567
                    url: Helpers.updateUrlWithPath(el.attr('href')),
568
                    title: el.data('name')
569
                });
570
                break;
1✔
571
            case 'message':
572
                CMS.API.Messages.open({
1✔
573
                    message: el.data('text')
574
                });
575
                break;
1✔
576
            case 'ajax':
577
                this.openAjax({
1✔
578
                    url: el.attr('href'),
579
                    post: JSON.stringify(el.data('post')),
580
                    method: el.data('method'),
581
                    text: el.data('text'),
582
                    onSuccess: el.data('on-success')
583
                });
584
                break;
1✔
585
            case 'color-toggle':
UNCOV
586
                Helpers.toggleColorScheme();
×
UNCOV
587
                break;
×
588
            case 'sideframe':
589
                // If the sideframe is enabled, show it
590
                if (typeof CMS.settings.sideframe_enabled === 'undefined' || CMS.settings.sideframe_enabled) {
2✔
591
                    this._openSideFrame(el);
1✔
592
                    break;
1✔
593
                }
594
                // Else fall through to default, the sideframe is disabled
595

596
            default:
597
                if (el.hasClass('cms-form-post-method')) {
2!
UNCOV
598
                    this._sendPostRequest(el);
×
599
                } else {
600
                    Helpers._getWindow().location.href = el.attr('href');
2✔
601
                }
602
        }
603
    },
604

605
    _openSideFrame: function _openSideFrame(el) {
606
        var sideframe = CMS.API.Sideframe || new Sideframe({
1✔
607
            onClose: el.data('on-close')
608
        });
609

610
        sideframe.open({
1✔
611
            url: el.attr('href'),
612
            animate: true
613
        });
614
    },
615

616
    _sendPostRequest: function _sendPostRequest(el) {
617
        /* Allow post method to be used */
UNCOV
618
        var formToken = document.querySelector('form input[name="csrfmiddlewaretoken"]');
×
UNCOV
619
        var csrfToken = '<input type="hidden" name="csrfmiddlewaretoken" value="' +
×
620
            ((formToken ? formToken.value : formToken) || window.CMS.config.csrf) + '">';
×
621
        var fakeForm = $(
×
622
            '<form style="display: none" action="' + el.attr('href') + '" method="POST">' + csrfToken +
623
            '</form>'
624
        );
625

UNCOV
626
        fakeForm.appendTo(Helpers._getWindow().document.body).submit();
×
627
    },
628

629
    /**
630
     * Handles the debug bar when `DEBUG=true` on top of the toolbar.
631
     *
632
     * @method _debug
633
     * @private
634
     */
635
    _debug: function _debug() {
636
        if (!CMS.config.lang.debug) {
2!
UNCOV
637
            return;
×
638
        }
639

640
        var timeout = 1000;
2✔
641
        // istanbul ignore next: function always reassigned
642
        var timer = function() {};
643

644
        // bind message event
645
        var debug = this.ui.container.find('.cms-debug-bar');
2✔
646

647
        debug.on(this.mouseEnter + ' ' + this.mouseLeave, function(e) {
2✔
648
            clearTimeout(timer);
3✔
649

650
            if (e.type === 'mouseenter') {
3✔
651
                timer = setTimeout(function() {
2✔
652
                    CMS.API.Messages.open({
1✔
653
                        message: CMS.config.lang.debug
654
                    });
655
                }, timeout);
656
            }
657
        });
658
    },
659

660
    /**
661
     * Handles the case when opened menu doesn't fit the screen.
662
     *
663
     * @method _handleLongMenus
664
     * @private
665
     */
666
    _handleLongMenus: function _handleLongMenus() {
667
        var openMenus = $('.cms-toolbar-item-navigation-hover > ul');
16✔
668

669
        if (!openMenus.length) {
16✔
670
            this._stickToolbar();
6✔
671
            return;
6✔
672
        }
673

674
        var positions = openMenus.toArray().map(function(item) {
10✔
675
            var el = $(item);
12✔
676

677
            return $.extend({}, el.position(), { height: el.height() });
12✔
678
        });
679
        var windowHeight = this.ui.window.height();
10✔
680

681
        this._position.top = this.ui.window.scrollTop();
10✔
682

683
        var shouldUnstickToolbar = positions.some(function(item) {
10✔
684
            return item.top + item.height > windowHeight;
12✔
685
        });
686

687
        if (shouldUnstickToolbar && this._position.top >= this._position.stickyTop) {
10!
UNCOV
688
            if (this._position.isSticky) {
×
UNCOV
689
                this._unstickToolbar();
×
690
            }
691
        } else {
692
            this._stickToolbar();
10✔
693
        }
694
    },
695

696
    /**
697
     * Resets toolbar to the normal position.
698
     *
699
     * @method _stickToolbar
700
     * @private
701
     */
702
    _stickToolbar: function _stickToolbar() {
703
        this._position.stickyTop = 0;
16✔
704
        this._position.isSticky = true;
16✔
705
        this.ui.body.removeClass('cms-toolbar-non-sticky');
16✔
706
        this.ui.toolbar.css({
16✔
707
            top: 0
708
        });
709
    },
710

711
    /**
712
     * Positions toolbar absolutely so the long menus can be scrolled
713
     * (toolbar goes away from the screen if required)
714
     *
715
     * @method _unstickToolbar
716
     * @private
717
     */
718
    _unstickToolbar: function _unstickToolbar() {
UNCOV
719
        this._position.stickyTop = this._position.top;
×
UNCOV
720
        this.ui.body.addClass('cms-toolbar-non-sticky');
×
721
        // have to do the !important because of "debug" toolbar
722
        this.ui.toolbar[0].style.setProperty('top', this._position.stickyTop + 'px', 'important');
×
UNCOV
723
        this._position.isSticky = false;
×
724
    },
725

726
    /**
727
     * Show publish button and handle the case when it's in the dropdown.
728
     * Also enable revert to live
729
     *
730
     * @method onPublishAvailable
731
     * @public
732
     * @deprecated since 3.5 due to us reloading the toolbar instead
733
     */
734
    onPublishAvailable: function showPublishButton() {
735
        // show publish / save buttons
736
        // istanbul ignore next
737
        // eslint-disable-next-line no-console
738
        console.warn('This method is deprecated and will be removed in future versions');
739
    },
740

741
    _refreshMarkup: function(newToolbar) {
742
        const switcher = this.ui.toolbarSwitcher.detach();
1✔
743

744
        $(this.ui.toolbar).html(newToolbar.children());
1✔
745

746
        $('.cms-toolbar-item-cms-mode-switcher').replaceWith(switcher);
1✔
747

748
        this._setupUI();
1✔
749

750
        // have to clone the nav to eliminate double events
751
        // there must be a better way to do this
752
        var clone = this.ui.navigations.clone();
1✔
753

754
        this.ui.navigations.replaceWith(clone);
1✔
755
        this.ui.navigations = clone;
1✔
756

757
        this._events();
1✔
758
        this.navigation = new Navigation();
1✔
759
        this.navigation.ui.window.trigger('resize');
1✔
760

761
        CMS.API.Clipboard.ui.triggers = $('.cms-clipboard-trigger a');
1✔
762
        CMS.API.Clipboard.ui.triggerRemove = $('.cms-clipboard-empty a');
1✔
763
        CMS.API.Clipboard._toolbarEvents();
1✔
764
    }
765
});
766

767
export default Toolbar;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc