customizer.js 15.42 KiB
/*!
 * Bootstrap Customizer (http://getbootstrap.com/customize/)
 * Copyright 2011-2014 Twitter, Inc.
 * Licensed under the Creative Commons Attribution 3.0 Unported License. For
 * details, see http://creativecommons.org/licenses/by/3.0/.
 */
/* global JSZip, less, saveAs, UglifyJS, __configBridge, __js, __less, __fonts */
window.onload = function () { // wait for load in a dumb way because B-0
  'use strict';
  var cw = '/*!\n' +
           ' * Bootstrap v3.3.0 (http://getbootstrap.com)\n' +
           ' * Copyright 2011-' + new Date().getFullYear() + ' Twitter, Inc.\n' +
           ' * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)\n' +
           ' */\n\n'
  var supportsFile = (window.File && window.FileReader && window.FileList && window.Blob)
  var importDropTarget = $('#import-drop-target')
  function showError(msg, err) {
    $('<div id="bsCustomizerAlert" class="bs-customizer-alert">' +
        '<div class="container">' +
          '<a href="#bsCustomizerAlert" data-dismiss="alert" class="close pull-right">&times;</a>' +
          '<p class="bs-customizer-alert-text"><span class="glyphicon glyphicon-warning-sign"></span>' + msg + '</p>' +
          (err.extract ? '<pre class="bs-customizer-alert-extract">' + err.extract.join('\n') + '</pre>' : '') +
        '</div>' +
      '</div>').appendTo('body').alert()
    throw err
  function showSuccess(msg) {
    $('<div class="bs-callout bs-callout-info">' +
      '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + msg +
    '</div>').insertAfter('.bs-customize-download')
  function showCallout(msg, showUpTop) {
    var callout = $('<div class="bs-callout bs-callout-danger">' +
       '<h4>Attention!</h4>' +
      '<p>' + msg + '</p>' +
    '</div>')
    if (showUpTop) {
      callout.appendTo('.bs-docs-container')
    } else {
      callout.insertAfter('.bs-customize-download')
  function showAlert(type, msg, insertAfter) {
    $('<div class="alert alert-' + type + '">' + msg + '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button></div>')
      .insertAfter(insertAfter)
  function getQueryParam(key) {
    key = key.replace(/[*+?^$.\[\]{}()|\\\/]/g, '\\$&') // escape RegEx meta chars
    var match = location.search.match(new RegExp('[?&]' + key + '=([^&]+)(&|$)'))
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '))
  function createGist(configJson, callback) {
    var data = {
      description: 'Bootstrap Customizer Config',
      'public': true,
      files: {
        'config.json': {
          content: configJson
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
} } $.ajax({ url: 'https://api.github.com/gists', type: 'POST', contentType: 'application/json; charset=UTF-8', dataType: 'json', data: JSON.stringify(data) }) .success(function (result) { var gistUrl = result.html_url; var origin = window.location.protocol + '//' + window.location.host var customizerUrl = origin + window.location.pathname + '?id=' + result.id showSuccess('<strong>Success!</strong> Your configuration has been saved to <a href="' + gistUrl + '">' + gistUrl + '</a> ' + 'and can be revisited here at <a href="' + customizerUrl + '">' + customizerUrl + '</a> for further customization.') history.replaceState(false, document.title, customizerUrl) callback(gistUrl, customizerUrl) }) .error(function (err) { try { showError('<strong>Ruh roh!</strong> Could not save gist file, configuration not saved.', err) } catch (sameErr) { // deliberately ignore the error } callback('<none>', '<none>') }) } function getCustomizerData() { var vars = {} $('#less-variables-section input') .each(function () { $(this).val() && (vars[$(this).prev().text()] = $(this).val()) }) var data = { vars: vars, css: $('#less-section input:checked') .map(function () { return this.value }).toArray(), js: $('#plugin-section input:checked').map(function () { return this.value }).toArray() } if ($.isEmptyObject(data.vars) && !data.css.length && !data.js.length) return return data } function updateCustomizerFromJson(data) { if (data.js) { $('#plugin-section input').each(function () { $(this).prop('checked', ~$.inArray(this.value, data.js)) }) } if (data.css) { $('#less-section input').each(function () { $(this).prop('checked', ~$.inArray(this.value, data.css)) }) } if (data.vars) { for (var i in data.vars) { $('input[data-var="' + i + '"]').val(data.vars[i]) } } } function parseUrl() { var id = getQueryParam('id') if (!id) return
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
$.ajax({ url: 'https://api.github.com/gists/' + id, type: 'GET', dataType: 'json' }) .success(function (result) { var data = JSON.parse(result.files['config.json'].content) updateCustomizerFromJson(data) }) .error(function (err) { showError('Error fetching bootstrap config file', err) }) } function generateZip(css, js, fonts, config, complete) { if (!css && !js) return showError('<strong>Ruh roh!</strong> No Bootstrap files selected.', new Error('no Bootstrap')) var zip = new JSZip() if (css) { var cssFolder = zip.folder('css') for (var fileName in css) { cssFolder.file(fileName, css[fileName]) } } if (js) { var jsFolder = zip.folder('js') for (var jsFileName in js) { jsFolder.file(jsFileName, js[jsFileName]) } } if (fonts) { var fontsFolder = zip.folder('fonts') for (var fontsFileName in fonts) { fontsFolder.file(fontsFileName, fonts[fontsFileName], { base64: true }) } } if (config) { zip.file('config.json', config) } var content = zip.generate({ type: 'blob' }) complete(content) } function generateCustomLess(vars) { var result = '' for (var key in vars) { result += key + ': ' + vars[key] + ';\n' } return result + '\n\n' } function generateFonts() { var glyphicons = $('#less-section [value="glyphicons.less"]:checked') if (glyphicons.length) { return __fonts } } // Returns an Array of @import'd filenames in the order // in which they appear in the file. function includedLessFilenames(lessFilename) { var IMPORT_REGEX = /^@import \"(.*?)\";$/