如何使用Bootstrap和Drupal覆盖JS文件?

时间:2018-01-21 00:25:28

标签: javascript jquery twitter-bootstrap templates drupal-8

我有一个网站" Drupal 8"和#34; Bootstrap 3"。我定制了我的" Collapse"的行为。菜单通过复制" Bootstrap"的collapse.js文件主题进入我的子主题。

我的问题:

我复制整个文件,有没有办法通过只复制自定义代码来覆盖JS文件?

以下是bootstrap_subtheme_front_office.libraries.yml文件的内容:

global-styling:
  css:
    theme:
      fonts/font-awesome/css/font-awesome.css: {}
#      bootstrap/dist/css/bootstrap.css: {}
#      css/bootstrap-cosmo.css: {}
      css/style.css: {}
#      css/style-noel.css: {}
#      css/style-nouvel-an.css: {}

bootstrap-scripts:
  js:
    bootstrap/js/affix.js: {}
    bootstrap/js/alert.js: {}
    bootstrap/js/button.js: {}
    bootstrap/js/carousel.js: {}
    bootstrap/js/collapse.js: {}
#    bootstrap/js/dropdown.js: {}
    bootstrap/js/modal.js: {}
    bootstrap/js/tooltip.js: {}
    bootstrap/js/popover.js: {}
    bootstrap/js/scrollspy.js: {}
    bootstrap/js/tab.js: {}
    bootstrap/js/transition.js: {}
    js/tour.js: {}
    js/collapse.js: {}

以下是文件collapse.js的内容:

/* ========================================================================
 * Bootstrap: collapse.js v3.3.7
 * http://getbootstrap.com/javascript/#collapse
 * ========================================================================
 * Copyright 2011-2016 Twitter, Inc.
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * ======================================================================== */

/* jshint latedef: false */

+function ($) {
  'use strict';

  // COLLAPSE PUBLIC CLASS DEFINITION
  // ================================

  var Collapse = function (element, options) {
    this.$element      = $(element)
    this.options       = $.extend({}, Collapse.DEFAULTS, options)
    this.$trigger      = $('[data-toggle="collapse"][href="#' + element.id + '"],' +
                           '[data-toggle="collapse"][data-target="#' + element.id + '"]')
    this.transitioning = null

    if (this.options.parent) {
      this.$parent = this.getParent()
    } else {
      this.addAriaAndCollapsedClass(this.$element, this.$trigger)
    }

    if (this.options.toggle) this.toggle()
  }

  Collapse.VERSION  = '3.3.7'

  Collapse.TRANSITION_DURATION = 350

  Collapse.DEFAULTS = {
    toggle: true
  }

  Collapse.prototype.dimension = function () {
    var hasWidth = this.$element.hasClass('width')
    return hasWidth ? 'width' : 'height'
  }

  Collapse.prototype.show = function () {
    if (this.transitioning || this.$element.hasClass('in')) return

    var activesData
    var actives = this.$parent && this.$parent.children('.panel').children('.in, .collapsing')

    if (actives && actives.length) {
      activesData = actives.data('bs.collapse')
      if (activesData && activesData.transitioning) return
    }

    var startEvent = $.Event('show.bs.collapse')
    this.$element.trigger(startEvent)
    if (startEvent.isDefaultPrevented()) return

    if (actives && actives.length) {
      Plugin.call(actives, 'hide')
      activesData || actives.data('bs.collapse', null)
    }

    var dimension = this.dimension()

    this.$element
      .removeClass('collapse')
      .addClass('collapsing')[dimension](0)
      .attr('aria-expanded', true)

    this.$trigger
      .removeClass('collapsed')
      .attr('aria-expanded', true)

    this.transitioning = 1

    var complete = function () {
      this.$element
        .removeClass('collapsing')
        .addClass('collapse in')[dimension]('')
      this.transitioning = 0
      this.$element
        .trigger('shown.bs.collapse')
    }

    if (!$.support.transition) return complete.call(this)

    var scrollSize = $.camelCase(['scroll', dimension].join('-'))

    this.$element
      .one('bsTransitionEnd', $.proxy(complete, this))
      .emulateTransitionEnd(Collapse.TRANSITION_DURATION)[dimension](this.$element[0][scrollSize])
  }

  Collapse.prototype.hide = function () {
    if (this.transitioning || !this.$element.hasClass('in')) return

    var startEvent = $.Event('hide.bs.collapse')
    this.$element.trigger(startEvent)
    if (startEvent.isDefaultPrevented()) return

    var dimension = this.dimension()

    this.$element[dimension](this.$element[dimension]())[0].offsetHeight

    this.$element
      .addClass('collapsing')
      .removeClass('collapse in')
      .attr('aria-expanded', false)

    this.$trigger
      .addClass('collapsed')
      .attr('aria-expanded', false)

    this.transitioning = 1

    var complete = function () {
      this.transitioning = 0
      this.$element
        .removeClass('collapsing')
        .addClass('collapse')
        .trigger('hidden.bs.collapse')
    }

    if (!$.support.transition) return complete.call(this)

    this.$element
      [dimension](0)
      .one('bsTransitionEnd', $.proxy(complete, this))
      .emulateTransitionEnd(Collapse.TRANSITION_DURATION)
  }

  Collapse.prototype.toggle = function () {
    this[this.$element.hasClass('in') ? 'hide' : 'show']()
  }

  Collapse.prototype.getParent = function () {
    return $(this.options.parent)
      .find('[data-toggle="collapse"][data-parent="' + this.options.parent + '"]')
      .each($.proxy(function (i, element) {
        var $element = $(element)
        this.addAriaAndCollapsedClass(getTargetFromTrigger($element), $element)
      }, this))
      .end()
  }

  Collapse.prototype.addAriaAndCollapsedClass = function ($element, $trigger) {
    var isOpen = $element.hasClass('in')

    $element.attr('aria-expanded', isOpen)
    $trigger
      .toggleClass('collapsed', !isOpen)
      .attr('aria-expanded', isOpen)
  }

  function getTargetFromTrigger($trigger) {
    var href
    var target = $trigger.attr('data-target')
      || (href = $trigger.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') // strip for ie7

    return $(target)
  }


  // COLLAPSE PLUGIN DEFINITION
  // ==========================

  function Plugin(option) {
    return this.each(function () {
      var $this   = $(this)
      var data    = $this.data('bs.collapse')
      var options = $.extend({}, Collapse.DEFAULTS, $this.data(), typeof option == 'object' && option)

      if (!data && options.toggle && /show|hide/.test(option)) options.toggle = false
      if (!data) $this.data('bs.collapse', (data = new Collapse(this, options)))
      if (typeof option == 'string') data[option]()
    })
  }

  var old = $.fn.collapse

  $.fn.collapse             = Plugin
  $.fn.collapse.Constructor = Collapse


  // COLLAPSE NO CONFLICT
  // ====================

  $.fn.collapse.noConflict = function () {
    $.fn.collapse = old
    return this
  }


  // COLLAPSE DATA-API
  // =================

  $(document).on('click.bs.collapse.data-api', '[data-toggle="collapse"]', function (e) {
    var $this   = $(this)

    if (!$this.attr('data-target')) e.preventDefault()

    var $target = getTargetFromTrigger($this)
    var data    = $target.data('bs.collapse')
    var option  = data ? 'toggle' : $this.data()

    Plugin.call($target, option)
  })

}(jQuery);

我在最后添加了以下代码(我的自定义代码):

$('#navbar-collapse-first').on('show.bs.collapse', function () {
     $('#navbar-collapse-second').collapse('hide');
})

$('#navbar-collapse-second').on('show.bs.collapse', function () {
     $('#navbar-collapse-first').collapse('hide');
})

$('#navbar-collapse-first').on('show.bs.collapse', function () {
     $('body').addClass('overlay-is-navbar-collapse');
});

$('#navbar-collapse-first').on('hide.bs.collapse', function () {
     $('body').removeClass('overlay-is-navbar-collapse');
});

$('#navbar-collapse-second').on('show.bs.collapse', function () {
     $('body').addClass('overlay-is-navbar-collapse');
});

$('#navbar-collapse-second').on('hide.bs.collapse', function () {
     $('body').removeClass('overlay-is-navbar-collapse');
});
  

更新:这是正确的代码

(function ($) {
  var $document = $(document);
  var $body = $(document.body);

  // Wrap everything in a DOM ready handler.
  $document.ready(function () {

    // Save the navbar collapse selectors so making updates/tracking easier.
    var navbarCollapseFirst = '#navbar-collapse-first';
    var navbarCollapseSecond = '#navbar-collapse-second';
    var navbarCollapseBoth = navbarCollapseFirst + ',' + navbarCollapseSecond;

    // Save the jQuery instances (for performance).
    var $navbarCollapseFirst = $(navbarCollapseFirst);
    var $navbarCollapseSecond = $(navbarCollapseSecond);

    // Variable for saving which navbar collapse is currently open.
    var $open = $();

    // For performance reasons, bind evens directly on the document. jQuery
    // allows you to pass a targeting selector between the event and handler
    // so it will only call said handler when the event matches said selector.
    $document
      // Bind "show" event for first navbar collapse.
      .on('show.bs.collapse', navbarCollapseBoth, function (e) {
        // Indicate that the first is open.
        $open = $(e.target);

        // Collapse the first if it's not the one that just opened.
        if (!$navbarCollapseFirst.is($open)) {
          $navbarCollapseFirst.collapse('hide');
        }
        // Collapse the second if it's not the one that just opened.
        else if (!$navbarCollapseSecond.is($open)) {
          $navbarCollapseSecond.collapse('hide');
        }

        // Add the body class.
        $body.addClass('overlay-is-navbar-collapse');
      })
      // Bind "hide" event for first navbar collapse.
      .on('hide.bs.collapse', navbarCollapseFirst, function (e) {
        // Indicate that the first is open.
        var $hide = $(e.target);

        // Remove the first as the opened navbar collapse.
        if ($navbarCollapseFirst.is($hide) && $navbarCollapseFirst.is($open)) {
          $open = $();
        }
        // Remove the second as the opened navbar collapse.
        else if ($navbarCollapseSecond.is($hide) && $navbarCollapseSecond.is($open)) {
          $open = $();
        }

        // Remove the body class if there is no open navbar collapse.
        if (!$open[0]) {
          $body.removeClass('overlay-is-navbar-collapse');
        }
      });
  });

})(window.jQuery);

1 个答案:

答案 0 :(得分:1)

我认为你必须将你的代码包装在document.ready函数中,在你自己的js文件中,如下所示:

$(function(){
$('#navbar-collapse-first').on('show.bs.collapse', function () {
     $('#navbar-collapse-second').collapse('hide');
})

$('#navbar-collapse-second').on('show.bs.collapse', function () {
     $('#navbar-collapse-first').collapse('hide');
})

$('#navbar-collapse-first').on('show.bs.collapse', function () {
     $('body').addClass('overlay-is-navbar-collapse');
});

$('#navbar-collapse-first').on('hide.bs.collapse', function () {
     $('body').removeClass('overlay-is-navbar-collapse');
});

$('#navbar-collapse-second').on('show.bs.collapse', function () {
     $('body').addClass('overlay-is-navbar-collapse');
});

$('#navbar-collapse-second').on('hide.bs.collapse', function () {
     $('body').removeClass('overlay-is-navbar-collapse');
});
});