4 ,checkRewrite : function () {
5 var testUri = location.pathname.replace(/\/[^\/]*$/, '/rewriteTest.js').substr(1);
7 $('#minRewriteFailed')[0].className = 'topNote';
10 url : '../f=' + testUri + '&' + (new Date()).getTime()
11 ,success : function (data) {
13 MUB._minRoot = '/min/';
14 $('span.minRoot').html('/min/');
22 * Get markup for new source LI element
24 ,newLi : function () {
25 return '<li id="li' + MUB._uid + '">http://' + location.host + '/<input type=text size=20>'
26 + ' <button title="Remove">x</button> <button title="Include Earlier">↑</button>'
27 + ' <button title="Include Later">↓</button> <span></span></li>';
30 * Add new empty source LI and attach handlers to buttons
32 ,addLi : function () {
33 $('#sources').append(MUB.newLi());
34 var li = $('#li' + MUB._uid)[0];
35 $('button[title=Remove]', li).click(function () {
37 var hadValue = !!$('input', li)[0].value;
40 $('button[title$=Earlier]', li).click(function () {
41 $(li).prev('li').find('input').each(function () {
43 // this = previous li input
45 this.value = $('input', li).val();
46 $('input', li).val(tmp);
47 MUB.updateAllTestLinks();
50 $('button[title$=Later]', li).click(function () {
51 $(li).next('li').find('input').each(function () {
53 // this = next li input
55 this.value = $('input', li).val();
56 $('input', li).val(tmp);
57 MUB.updateAllTestLinks();
63 * In the context of a source LI element, this will analyze the URI in
64 * the INPUT and check the URL on the site.
66 ,liUpdateTestLink : function () { // call in context of li element
67 if (! $('input', this)[0].value)
70 $('span', this).html('');
71 var url = 'http://' + location.host + '/'
72 + $('input', this)[0].value.replace(/^\//, '');
75 ,complete : function (xhr, stat) {
76 if ('success' == stat)
77 $('span', li).html('✓');
79 $('span', li).html('<button><b>404! </b> recheck</button>')
80 .find('button').click(function () {
81 MUB.liUpdateTestLink.call(li);
89 * Check all source URLs
91 ,updateAllTestLinks : function () {
92 $('#sources li').each(MUB.liUpdateTestLink);
95 * In a given array of strings, find the character they all have at
97 * @param Array arr array of strings
98 * @param Number pos index to check
99 * @return mixed a common char or '' if any do not match
101 ,getCommonCharAtPos : function (arr, pos) {
104 ,c = arr[0].charAt(pos);
105 if (c === '' || l === 1)
107 for (i = 1; i < l; ++i)
108 if (arr[i].charAt(pos) !== c)
113 * Get the shortest URI to minify the set of source files
114 * @param Array sources URIs
116 ,getBestUri : function (sources) {
121 c = MUB.getCommonCharAtPos(sources, pos);
128 base = base.replace(/[^\/]+$/, '');
129 var uri = MUB._minRoot + 'f=' + sources.join(',');
130 if (base.charAt(base.length - 1) === '/') {
131 // we have a base dir!
132 var basedSources = sources
135 for (i = 0; i < l; ++i) {
136 basedSources[i] = sources[i].substr(base.length);
138 base = base.substr(0, base.length - 1);
139 var bUri = MUB._minRoot + 'b=' + base + '&f=' + basedSources.join(',');
140 //window.console && console.log([uri, bUri]);
141 uri = uri.length < bUri.length
148 * Create the Minify URI for the sources
150 ,update : function () {
151 MUB.updateAllTestLinks();
155 $('#sources input').each(function () {
157 if (! fail && this.value && (m = this.value.match(/\.(css|js)$/))) {
161 else if (thisExt !== ext) {
163 return alert('extensions must match!');
165 this.value = this.value.replace(/^\//, '');
166 if (-1 != $.inArray(this.value, sources)) {
168 return alert('duplicate file!');
170 sources.push(this.value);
173 if (fail || ! sources.length)
175 $('#groupConfig').val(" 'keyName' => array('//" + sources.join("', '//") + "'),");
176 var uri = MUB.getBestUri(sources)
177 ,uriH = uri.replace(/</, '<').replace(/>/, '>').replace(/&/, '&');
178 $('#uriA').html(uriH)[0].href = uri;
181 ? '<script type="text/javascript" src="' + uriH + '"></script>'
182 : '<link type="text/css" rel="stylesheet" href="' + uriH + '" />'
184 $('#results').show();
187 * Handler for the "Add file +" button
189 ,addButtonClick : function () {
190 $('#results').hide();
192 MUB.updateAllTestLinks();
193 $('#update').show().click(MUB.update);
194 $('#sources li:last input')[0].focus();
199 ,init : function () {
201 $('#sources').html('');
202 $('#add button').click(MUB.addButtonClick);
203 // make easier to copy text out of
204 $('#uriHtml, #groupConfig').click(function () {
206 }).focus(function () {
209 $('a.ext').attr({target:'_blank'});
211 // make links out of URIs from bookmarklet
213 $('#bmUris').html('<p><strong>Found by bookmarklet:</strong> /<a href=#>'
214 + location.hash.substr(1).split(',').join('</a> | /<a href=#>')
217 $('#bmUris a').click(function () {
218 MUB.addButtonClick();
219 $('#sources li:last input').val(this.innerHTML)
220 MUB.liUpdateTestLink.call($('#sources li:last')[0]);
221 $('#results').hide();
223 }).attr({title:'Add file +'});
225 // copy bookmarklet code into href
226 var bmUri = location.pathname.replace(/\/[^\/]*$/, '/bm.js').substr(1);
228 url : '../?f=' + bmUri
229 ,success : function (code) {
230 $('#bm')[0].href = code
231 .replace('%BUILDER_URL%', location.href)
232 .replace(/\n/g, ' ');
236 $.browser.msie && $('#getBm p:last').append(' Sorry, not supported in MSIE!');
237 MUB.addButtonClick();
242 window.onload = MUB.init;