Sha256: c856c288b731877c4b27feeaecf5f2575fdaef14b4b702db6f8df217cc091ec7

Contents?: true

Size: 1.65 KB

Versions: 11

Compression:

Stored size: 1.65 KB

Contents

;(function (global) {
  'use strict'

  var $ = global.jQuery
  var GOVUK = global.GOVUK || {}
  GOVUK.Modules = GOVUK.Modules || {}

  GOVUK.modules = {
    find: function (container) {
      container = container || $('body')

      var modules
      var moduleSelector = '[data-module]'

      modules = container.find(moduleSelector)

      // Container could be a module too
      if (container.is(moduleSelector)) {
        modules = modules.add(container)
      }

      return modules
    },

    start: function (container) {
      var modules = this.find(container)

      for (var i = 0, l = modules.length; i < l; i++) {
        var module
        var element = $(modules[i])
        var type = camelCaseAndCapitalise(element.data('module'))
        var started = element.data('module-started')

        if (typeof GOVUK.Modules[type] === 'function' && !started) {
          module = new GOVUK.Modules[type]()
          module.start(element)
          element.data('module-started', true)
        }
      }

      // eg selectable-table to SelectableTable
      function camelCaseAndCapitalise (string) {
        return capitaliseFirstLetter(camelCase(string))
      }

      // http://stackoverflow.com/questions/6660977/convert-hyphens-to-camel-case-camelcase
      function camelCase (string) {
        return string.replace(/-([a-z])/g, function (g) {
          return g[1].toUpperCase()
        })
      }

      // http://stackoverflow.com/questions/1026069/capitalize-the-first-letter-of-string-in-javascript
      function capitaliseFirstLetter (string) {
        return string.charAt(0).toUpperCase() + string.slice(1)
      }
    }
  }

  global.GOVUK = GOVUK
})(window)

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
govuk_frontend_toolkit-6.0.0 app/assets/javascripts/govuk/modules.js
govuk_frontend_toolkit-5.2.0 app/assets/javascripts/govuk/modules.js
govuk_frontend_toolkit-5.1.3 app/assets/javascripts/govuk/modules.js
govuk_frontend_toolkit-5.1.2 app/assets/javascripts/govuk/modules.js
govuk_frontend_toolkit-5.1.1 app/assets/javascripts/govuk/modules.js
govuk_frontend_toolkit-5.1.0 app/assets/javascripts/govuk/modules.js
govuk_frontend_toolkit-5.0.3 app/assets/javascripts/govuk/modules.js
govuk_frontend_toolkit-5.0.2 app/assets/javascripts/govuk/modules.js
govuk_frontend_toolkit-5.0.1 app/assets/javascripts/govuk/modules.js
govuk_frontend_toolkit-5.0.0 app/assets/javascripts/govuk/modules.js
govuk_frontend_toolkit-4.18.4 app/assets/javascripts/govuk/modules.js