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

divio / django-cms / #29287

10 Jan 2025 08:55PM UTC coverage: 76.168% (-1.4%) from 77.576%
#29287

push

travis-ci

web-flow
Merge 16bd3c1ee into ec268c7b2

1053 of 1568 branches covered (67.16%)

16 of 28 new or added lines in 2 files covered. (57.14%)

405 existing lines in 6 files now uncovered.

2528 of 3319 relevant lines covered (76.17%)

26.8 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!
UNCOV
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!
UNCOV
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) {
UNCOV
176
                    if (e.keyCode === CMS.KEYS.TAB) {
×
UNCOV
177
                        reset();
×
178
                    }
179
                });
180

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

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

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

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

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

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

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

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

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

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

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

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

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

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

293
                    // reset
294
                    lists.find('li').removeClass(hover);
2✔
295

296
                    // add hover effect
297
                    el.addClass(hover);
2✔
298

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

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

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

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

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

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

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

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

370
        this._show({ duration: 0 });
1✔
371

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

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

380
        hideDropdownIfRequired(publishBtn);
1✔
381

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

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

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

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

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

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

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

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

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

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

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

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

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

495
        showLoader();
13✔
496

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

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

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

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

536
    hideLoader: function () {
UNCOV
537
        hideLoader();
×
538
    },
539

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

679
        this._position.top = this.ui.window.scrollTop();
10✔
680

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

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

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

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

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

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

742
        $(this.ui.toolbar).html(newToolbar.children());
1✔
743

744
        $('.cms-toolbar-item-cms-mode-switcher').replaceWith(switcher);
1✔
745

746
        this._setupUI();
1✔
747

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

752
        this.ui.navigations.replaceWith(clone);
1✔
753
        this.ui.navigations = clone;
1✔
754

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

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

765
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