info@54
|
1 |
/*!
|
info@54
|
2 |
* Copyright 2013 Twitter, Inc.
|
info@54
|
3 |
*
|
info@54
|
4 |
* Licensed under the Creative Commons Attribution 3.0 Unported License. For
|
info@54
|
5 |
* details, see http://creativecommons.org/licenses/by/3.0/.
|
info@54
|
6 |
*/
|
info@54
|
7 |
|
info@54
|
8 |
|
info@54
|
9 |
window.onload = function () { // wait for load in a dumb way because B-0
|
info@54
|
10 |
var cw = '/*!\n * Bootstrap v3.0.3\n *\n * Copyright 2013 Twitter, Inc\n * Licensed under the Apache License v2.0\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Designed and built with all the love in the world @twitter by @mdo and @fat.\n */\n\n'
|
info@54
|
11 |
|
info@54
|
12 |
function showError(msg, err) {
|
info@54
|
13 |
$('<div id="bsCustomizerAlert" class="bs-customizer-alert">\
|
info@54
|
14 |
<div class="container">\
|
info@54
|
15 |
<a href="#bsCustomizerAlert" data-dismiss="alert" class="close pull-right">×</a>\
|
info@54
|
16 |
<p class="bs-customizer-alert-text"><span class="glyphicon glyphicon-warning-sign"></span>' + msg + '</p>' +
|
info@54
|
17 |
(err.extract ? '<pre class="bs-customizer-alert-extract">' + err.extract.join('\n') + '</pre>' : '') + '\
|
info@54
|
18 |
</div>\
|
info@54
|
19 |
</div>').appendTo('body').alert()
|
info@54
|
20 |
throw err
|
info@54
|
21 |
}
|
info@54
|
22 |
|
info@54
|
23 |
function showCallout(msg, showUpTop) {
|
info@54
|
24 |
var callout = $('<div class="bs-callout bs-callout-danger">\
|
info@54
|
25 |
<h4>Attention!</h4>\
|
info@54
|
26 |
<p>' + msg + '</p>\
|
info@54
|
27 |
</div>')
|
info@54
|
28 |
|
info@54
|
29 |
if (showUpTop) {
|
info@54
|
30 |
callout.appendTo('.bs-docs-container')
|
info@54
|
31 |
} else {
|
info@54
|
32 |
callout.insertAfter('.bs-customize-download')
|
info@54
|
33 |
}
|
info@54
|
34 |
}
|
info@54
|
35 |
|
info@54
|
36 |
function getQueryParam(key) {
|
info@54
|
37 |
key = key.replace(/[*+?^$.\[\]{}()|\\\/]/g, "\\$&"); // escape RegEx meta chars
|
info@54
|
38 |
var match = location.search.match(new RegExp("[?&]"+key+"=([^&]+)(&|$)"));
|
info@54
|
39 |
return match && decodeURIComponent(match[1].replace(/\+/g, " "));
|
info@54
|
40 |
}
|
info@54
|
41 |
|
info@54
|
42 |
function createGist(configJson) {
|
info@54
|
43 |
var data = {
|
info@54
|
44 |
"description": "Bootstrap Customizer Config",
|
info@54
|
45 |
"public": true,
|
info@54
|
46 |
"files": {
|
info@54
|
47 |
"config.json": {
|
info@54
|
48 |
"content": configJson
|
info@54
|
49 |
}
|
info@54
|
50 |
}
|
info@54
|
51 |
}
|
info@54
|
52 |
$.ajax({
|
info@54
|
53 |
url: 'https://api.github.com/gists',
|
info@54
|
54 |
type: 'POST',
|
info@54
|
55 |
dataType: 'json',
|
info@54
|
56 |
data: JSON.stringify(data)
|
info@54
|
57 |
})
|
info@54
|
58 |
.success(function(result) {
|
info@54
|
59 |
var origin = window.location.protocol + "//" + window.location.host
|
info@54
|
60 |
history.replaceState(false, document.title, origin + window.location.pathname + '?id=' + result.id)
|
info@54
|
61 |
})
|
info@54
|
62 |
.error(function(err) {
|
info@54
|
63 |
showError('<strong>Ruh roh!</strong> Could not save gist file, configuration not saved.', err)
|
info@54
|
64 |
})
|
info@54
|
65 |
}
|
info@54
|
66 |
|
info@54
|
67 |
function getCustomizerData() {
|
info@54
|
68 |
var vars = {}
|
info@54
|
69 |
|
info@54
|
70 |
$('#less-variables-section input')
|
info@54
|
71 |
.each(function () {
|
info@54
|
72 |
$(this).val() && (vars[ $(this).prev().text() ] = $(this).val())
|
info@54
|
73 |
})
|
info@54
|
74 |
|
info@54
|
75 |
var data = {
|
info@54
|
76 |
vars: vars,
|
info@54
|
77 |
css: $('#less-section input:checked') .map(function () { return this.value }).toArray(),
|
info@54
|
78 |
js: $('#plugin-section input:checked').map(function () { return this.value }).toArray()
|
info@54
|
79 |
}
|
info@54
|
80 |
|
info@54
|
81 |
if ($.isEmptyObject(data.vars) && !data.css.length && !data.js.length) return
|
info@54
|
82 |
|
info@54
|
83 |
return data
|
info@54
|
84 |
}
|
info@54
|
85 |
|
info@54
|
86 |
function parseUrl() {
|
info@54
|
87 |
var id = getQueryParam('id')
|
info@54
|
88 |
|
info@54
|
89 |
if (!id) return
|
info@54
|
90 |
|
info@54
|
91 |
$.ajax({
|
info@54
|
92 |
url: 'https://api.github.com/gists/' + id,
|
info@54
|
93 |
type: 'GET',
|
info@54
|
94 |
dataType: 'json'
|
info@54
|
95 |
})
|
info@54
|
96 |
.success(function(result) {
|
info@54
|
97 |
var data = JSON.parse(result.files['config.json'].content)
|
info@54
|
98 |
if (data.js) {
|
info@54
|
99 |
$('#plugin-section input').each(function () {
|
info@54
|
100 |
$(this).prop('checked', ~$.inArray(this.value, data.js))
|
info@54
|
101 |
})
|
info@54
|
102 |
}
|
info@54
|
103 |
if (data.css) {
|
info@54
|
104 |
$('#less-section input').each(function () {
|
info@54
|
105 |
$(this).prop('checked', ~$.inArray(this.value, data.css))
|
info@54
|
106 |
})
|
info@54
|
107 |
}
|
info@54
|
108 |
if (data.vars) {
|
info@54
|
109 |
for (var i in data.vars) {
|
info@54
|
110 |
$('input[data-var="' + i + '"]').val(data.vars[i])
|
info@54
|
111 |
}
|
info@54
|
112 |
}
|
info@54
|
113 |
})
|
info@54
|
114 |
.error(function(err) {
|
info@54
|
115 |
showError('Error fetching bootstrap config file', err)
|
info@54
|
116 |
})
|
info@54
|
117 |
}
|
info@54
|
118 |
|
info@54
|
119 |
function generateZip(css, js, fonts, config, complete) {
|
info@54
|
120 |
if (!css && !js) return showError('<strong>Ruh roh!</strong> No Bootstrap files selected.', new Error('no Bootstrap'))
|
info@54
|
121 |
|
info@54
|
122 |
var zip = new JSZip()
|
info@54
|
123 |
|
info@54
|
124 |
if (css) {
|
info@54
|
125 |
var cssFolder = zip.folder('css')
|
info@54
|
126 |
for (var fileName in css) {
|
info@54
|
127 |
cssFolder.file(fileName, css[fileName])
|
info@54
|
128 |
}
|
info@54
|
129 |
}
|
info@54
|
130 |
|
info@54
|
131 |
if (js) {
|
info@54
|
132 |
var jsFolder = zip.folder('js')
|
info@54
|
133 |
for (var fileName in js) {
|
info@54
|
134 |
jsFolder.file(fileName, js[fileName])
|
info@54
|
135 |
}
|
info@54
|
136 |
}
|
info@54
|
137 |
|
info@54
|
138 |
if (fonts) {
|
info@54
|
139 |
var fontsFolder = zip.folder('fonts')
|
info@54
|
140 |
for (var fileName in fonts) {
|
info@54
|
141 |
fontsFolder.file(fileName, fonts[fileName], {base64: true})
|
info@54
|
142 |
}
|
info@54
|
143 |
}
|
info@54
|
144 |
|
info@54
|
145 |
if (config) {
|
info@54
|
146 |
zip.file('config.json', config)
|
info@54
|
147 |
}
|
info@54
|
148 |
|
info@54
|
149 |
var content = zip.generate({type:"blob"})
|
info@54
|
150 |
|
info@54
|
151 |
complete(content)
|
info@54
|
152 |
}
|
info@54
|
153 |
|
info@54
|
154 |
function generateCustomCSS(vars) {
|
info@54
|
155 |
var result = ''
|
info@54
|
156 |
|
info@54
|
157 |
for (var key in vars) {
|
info@54
|
158 |
result += key + ': ' + vars[key] + ';\n'
|
info@54
|
159 |
}
|
info@54
|
160 |
|
info@54
|
161 |
return result + '\n\n'
|
info@54
|
162 |
}
|
info@54
|
163 |
|
info@54
|
164 |
function generateFonts() {
|
info@54
|
165 |
var glyphicons = $('#less-section [value="glyphicons.less"]:checked')
|
info@54
|
166 |
if (glyphicons.length) {
|
info@54
|
167 |
return __fonts
|
info@54
|
168 |
}
|
info@54
|
169 |
}
|
info@54
|
170 |
|
info@54
|
171 |
// Returns an Array of @import'd filenames from 'bootstrap.less' in the order
|
info@54
|
172 |
// in which they appear in the file.
|
info@54
|
173 |
function bootstrapLessFilenames() {
|
info@54
|
174 |
var IMPORT_REGEX = /^@import \"(.*?)\";$/
|
info@54
|
175 |
var bootstrapLessLines = __less['bootstrap.less'].split('\n')
|
info@54
|
176 |
|
info@54
|
177 |
for (var i = 0, imports = []; i < bootstrapLessLines.length; i++) {
|
info@54
|
178 |
var match = IMPORT_REGEX.exec(bootstrapLessLines[i])
|
info@54
|
179 |
if (match) imports.push(match[1])
|
info@54
|
180 |
}
|
info@54
|
181 |
|
info@54
|
182 |
return imports
|
info@54
|
183 |
}
|
info@54
|
184 |
|
info@54
|
185 |
function generateCSS() {
|
info@54
|
186 |
var oneChecked = false
|
info@54
|
187 |
var lessFileIncludes = {}
|
info@54
|
188 |
$('#less-section input').each(function() {
|
info@54
|
189 |
var $this = $(this)
|
info@54
|
190 |
var checked = $this.is(':checked')
|
info@54
|
191 |
lessFileIncludes[$this.val()] = checked
|
info@54
|
192 |
|
info@54
|
193 |
oneChecked = oneChecked || checked
|
info@54
|
194 |
})
|
info@54
|
195 |
|
info@54
|
196 |
if (!oneChecked) return false
|
info@54
|
197 |
|
info@54
|
198 |
var result = {}
|
info@54
|
199 |
var vars = {}
|
info@54
|
200 |
var css = ''
|
info@54
|
201 |
|
info@54
|
202 |
$('#less-variables-section input')
|
info@54
|
203 |
.each(function () {
|
info@54
|
204 |
$(this).val() && (vars[ $(this).prev().text() ] = $(this).val())
|
info@54
|
205 |
})
|
info@54
|
206 |
|
info@54
|
207 |
$.each(bootstrapLessFilenames(), function(index, filename) {
|
info@54
|
208 |
var fileInclude = lessFileIncludes[filename]
|
info@54
|
209 |
|
info@54
|
210 |
// Files not explicitly unchecked are compiled into the final stylesheet.
|
info@54
|
211 |
// Core stylesheets like 'normalize.less' are not included in the form
|
info@54
|
212 |
// since disabling them would wreck everything, and so their 'fileInclude'
|
info@54
|
213 |
// will be 'undefined'.
|
info@54
|
214 |
if (fileInclude || (fileInclude == null)) css += __less[filename]
|
info@54
|
215 |
|
info@54
|
216 |
// Custom variables are added after Bootstrap variables so the custom
|
info@54
|
217 |
// ones take precedence.
|
info@54
|
218 |
if (('variables.less' === filename) && vars) css += generateCustomCSS(vars)
|
info@54
|
219 |
})
|
info@54
|
220 |
|
info@54
|
221 |
css = css.replace(/@import[^\n]*/gi, '') //strip any imports
|
info@54
|
222 |
|
info@54
|
223 |
try {
|
info@54
|
224 |
var parser = new less.Parser({
|
info@54
|
225 |
paths: ['variables.less', 'mixins.less']
|
info@54
|
226 |
, optimization: 0
|
info@54
|
227 |
, filename: 'bootstrap.css'
|
info@54
|
228 |
}).parse(css, function (err, tree) {
|
info@54
|
229 |
if (err) {
|
info@54
|
230 |
return showError('<strong>Ruh roh!</strong> Could not parse less files.', err)
|
info@54
|
231 |
}
|
info@54
|
232 |
result = {
|
info@54
|
233 |
'bootstrap.css' : cw + tree.toCSS(),
|
info@54
|
234 |
'bootstrap.min.css' : cw + tree.toCSS({ compress: true }).replace(/\n/g, '') // FIXME: remove newline hack once less.js upgraded to v1.4
|
info@54
|
235 |
}
|
info@54
|
236 |
})
|
info@54
|
237 |
} catch (err) {
|
info@54
|
238 |
return showError('<strong>Ruh roh!</strong> Could not parse less files.', err)
|
info@54
|
239 |
}
|
info@54
|
240 |
|
info@54
|
241 |
return result
|
info@54
|
242 |
}
|
info@54
|
243 |
|
info@54
|
244 |
function generateJavascript() {
|
info@54
|
245 |
var $checked = $('#plugin-section input:checked')
|
info@54
|
246 |
if (!$checked.length) return false
|
info@54
|
247 |
|
info@54
|
248 |
var js = $checked
|
info@54
|
249 |
.map(function () { return __js[this.value] })
|
info@54
|
250 |
.toArray()
|
info@54
|
251 |
.join('\n')
|
info@54
|
252 |
|
info@54
|
253 |
return {
|
info@54
|
254 |
'bootstrap.js': js,
|
info@54
|
255 |
'bootstrap.min.js': cw + uglify(js)
|
info@54
|
256 |
}
|
info@54
|
257 |
}
|
info@54
|
258 |
|
info@54
|
259 |
var inputsComponent = $('#less-section input')
|
info@54
|
260 |
var inputsPlugin = $('#plugin-section input')
|
info@54
|
261 |
var inputsVariables = $('#less-variables-section input')
|
info@54
|
262 |
|
info@54
|
263 |
$('#less-section .toggle').on('click', function (e) {
|
info@54
|
264 |
e.preventDefault()
|
info@54
|
265 |
inputsComponent.prop('checked', !inputsComponent.is(':checked'))
|
info@54
|
266 |
})
|
info@54
|
267 |
|
info@54
|
268 |
$('#plugin-section .toggle').on('click', function (e) {
|
info@54
|
269 |
e.preventDefault()
|
info@54
|
270 |
inputsPlugin.prop('checked', !inputsPlugin.is(':checked'))
|
info@54
|
271 |
})
|
info@54
|
272 |
|
info@54
|
273 |
$('#less-variables-section .toggle').on('click', function (e) {
|
info@54
|
274 |
e.preventDefault()
|
info@54
|
275 |
inputsVariables.val('')
|
info@54
|
276 |
})
|
info@54
|
277 |
|
info@54
|
278 |
$('[data-dependencies]').on('click', function () {
|
info@54
|
279 |
if (!$(this).is(':checked')) return
|
info@54
|
280 |
var dependencies = this.getAttribute('data-dependencies')
|
info@54
|
281 |
if (!dependencies) return
|
info@54
|
282 |
dependencies = dependencies.split(',')
|
info@54
|
283 |
for (var i = 0; i < dependencies.length; i++) {
|
info@54
|
284 |
var dependency = $('[value="' + dependencies[i] + '"]')
|
info@54
|
285 |
dependency && dependency.prop('checked', true)
|
info@54
|
286 |
}
|
info@54
|
287 |
})
|
info@54
|
288 |
|
info@54
|
289 |
$('[data-dependents]').on('click', function () {
|
info@54
|
290 |
if ($(this).is(':checked')) return
|
info@54
|
291 |
var dependents = this.getAttribute('data-dependents')
|
info@54
|
292 |
if (!dependents) return
|
info@54
|
293 |
dependents = dependents.split(',')
|
info@54
|
294 |
for (var i = 0; i < dependents.length; i++) {
|
info@54
|
295 |
var dependent = $('[value="' + dependents[i] + '"]')
|
info@54
|
296 |
dependent && dependent.prop('checked', false)
|
info@54
|
297 |
}
|
info@54
|
298 |
})
|
info@54
|
299 |
|
info@54
|
300 |
var $compileBtn = $('#btn-compile')
|
info@54
|
301 |
var $downloadBtn = $('#btn-download')
|
info@54
|
302 |
|
info@54
|
303 |
$compileBtn.on('click', function (e) {
|
info@54
|
304 |
var configData = getCustomizerData()
|
info@54
|
305 |
var configJson = JSON.stringify(configData, null, 2)
|
info@54
|
306 |
|
info@54
|
307 |
e.preventDefault()
|
info@54
|
308 |
|
info@54
|
309 |
$compileBtn.attr('disabled', 'disabled')
|
info@54
|
310 |
|
info@54
|
311 |
generateZip(generateCSS(), generateJavascript(), generateFonts(), configJson, function (blob) {
|
info@54
|
312 |
$compileBtn.removeAttr('disabled')
|
info@54
|
313 |
saveAs(blob, "bootstrap.zip")
|
info@54
|
314 |
createGist(configJson)
|
info@54
|
315 |
})
|
info@54
|
316 |
})
|
info@54
|
317 |
|
info@54
|
318 |
// browser support alerts
|
info@54
|
319 |
if (!window.URL && navigator.userAgent.toLowerCase().indexOf('safari') != -1) {
|
info@54
|
320 |
showCallout("Looks like you're using safari, which sadly doesn't have the best support\
|
info@54
|
321 |
for HTML5 blobs. Because of this your file will be downloaded with the name <code>\"untitled\"</code>.\
|
info@54
|
322 |
However, if you check your downloads folder, just rename this <code>\"untitled\"</code> file\
|
info@54
|
323 |
to <code>\"bootstrap.zip\"</code> and you should be good to go!")
|
info@54
|
324 |
} else if (!window.URL && !window.webkitURL) {
|
info@54
|
325 |
$('.bs-docs-section, .bs-sidebar').css('display', 'none')
|
info@54
|
326 |
|
info@54
|
327 |
showCallout("Looks like your current browser doesn't support the Bootstrap Customizer. Please take a second\
|
info@54
|
328 |
to <a href=\"https://www.google.com/intl/en/chrome/browser/\"> upgrade to a more modern browser</a>.", true)
|
info@54
|
329 |
}
|
info@54
|
330 |
|
info@54
|
331 |
parseUrl()
|
info@54
|
332 |
}
|