aboutsummaryrefslogtreecommitdiff
path: root/mod/minify/vendors/min/builder/_index.js
blob: 8e5313a3b18aea9c11ae7a35a345dfb7ab4f22ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
var MUB = {
    _uid : 0
    ,_minRoot : '/min/?'
    ,checkRewrite : function () {
        var testUri = location.pathname.replace(/\/[^\/]*$/, '/rewriteTest.js').substr(1);
        function fail() {
            $('#minRewriteFailed')[0].className = 'topNote';
        };
        $.ajax({
            url : '../f=' + testUri + '&' + (new Date()).getTime()
            ,success : function (data) {
                if (data === '1') {
                    MUB._minRoot = '/min/';
                    $('span.minRoot').html('/min/');
                } else
                    fail();                
            }
            ,error : fail
        });
    }
    /**
     * Get markup for new source LI element
     */
    ,newLi : function () {
        return '<li id="li' + MUB._uid + '">http://' + location.host + '/<input type=text size=20>' 
        + ' <button title="Remove">x</button> <button title="Include Earlier">&uarr;</button>'
        + ' <button title="Include Later">&darr;</button> <span></span></li>';
    }
    /**
     * Add new empty source LI and attach handlers to buttons
     */
    ,addLi : function () {
        $('#sources').append(MUB.newLi());
        var li = $('#li' + MUB._uid)[0];
        $('button[title=Remove]', li).click(function () {
            $('#results').hide();
            var hadValue = !!$('input', li)[0].value;
            $(li).remove();
        });
        $('button[title$=Earlier]', li).click(function () {
            $(li).prev('li').find('input').each(function () {
                $('#results').hide();
                // this = previous li input
                var tmp = this.value;
                this.value = $('input', li).val();
                $('input', li).val(tmp);
                MUB.updateAllTestLinks();
            });
        });
        $('button[title$=Later]', li).click(function () {
            $(li).next('li').find('input').each(function () {
                $('#results').hide();
                // this = next li input
                var tmp = this.value;
                this.value = $('input', li).val();
                $('input', li).val(tmp);
                MUB.updateAllTestLinks();
            });
        });
        ++MUB._uid;
    }
    /**
     * In the context of a source LI element, this will analyze the URI in
     * the INPUT and check the URL on the site.
     */
    ,liUpdateTestLink : function () { // call in context of li element
        if (! $('input', this)[0].value) 
            return;
        var li = this;
        $('span', this).html('');
        var url = 'http://' + location.host + '/' 
                + $('input', this)[0].value.replace(/^\//, '');
        $.ajax({
            url : url
            ,complete : function (xhr, stat) {
                if ('success' == stat)
                    $('span', li).html('&#x2713;');
                else {
                    $('span', li).html('<button><b>404! </b> recheck</button>')
                        .find('button').click(function () {
                            MUB.liUpdateTestLink.call(li);
                        });
                }
            }
            ,dataType : 'text'
        });
    }
    /**
     * Check all source URLs
     */
    ,updateAllTestLinks : function () {
        $('#sources li').each(MUB.liUpdateTestLink);
    }
    /**
     * In a given array of strings, find the character they all have at
     * a particular index
     * @param Array arr array of strings
     * @param Number pos index to check
     * @return mixed a common char or '' if any do not match
     */
    ,getCommonCharAtPos : function (arr, pos) {
        var i
           ,l = arr.length
           ,c = arr[0].charAt(pos);
        if (c === '' || l === 1)
            return c;
        for (i = 1; i < l; ++i)
            if (arr[i].charAt(pos) !== c)
                return '';
        return c;
    }
    /**
     * Get the shortest URI to minify the set of source files
     * @param Array sources URIs
     */
    ,getBestUri : function (sources) {
        var pos = 0
           ,base = ''
           ,c;
        while (true) {
            c = MUB.getCommonCharAtPos(sources, pos);
            if (c === '')
                break;
            else
                base += c;
            ++pos;
        }
        base = base.replace(/[^\/]+$/, '');
        var uri = MUB._minRoot + 'f=' + sources.join(',');
        if (base.charAt(base.length - 1) === '/') {
            // we have a base dir!
            var basedSources = sources
               ,i
               ,l = sources.length;
            for (i = 0; i < l; ++i) {
                basedSources[i] = sources[i].substr(base.length);
            }
            base = base.substr(0, base.length - 1);
            var bUri = MUB._minRoot + 'b=' + base + '&f=' + basedSources.join(',');
            //window.console && console.log([uri, bUri]);
            uri = uri.length < bUri.length
                ? uri
                : bUri;
        }
        return uri;
    }
    /**
     * Create the Minify URI for the sources
     */
    ,update : function () {
        MUB.updateAllTestLinks();
        var sources = []
           ,ext = false
           ,fail = false;
        $('#sources input').each(function () {
            var m, val;
            if (! fail && this.value && (m = this.value.match(/\.(css|js)$/))) {
                var thisExt = m[1];
                if (ext === false)
                    ext = thisExt; 
                else if (thisExt !== ext) {
                    fail = true;
                    return alert('extensions must match!');
                }
                this.value = this.value.replace(/^\//, '');
                if (-1 != $.inArray(this.value, sources)) {
                    fail = true;
                    return alert('duplicate file!');
                }
                sources.push(this.value);
            } 
        });
        if (fail || ! sources.length)
            return;
        $('#groupConfig').val("    'keyName' => array('//" + sources.join("', '//") + "'),");
        var uri = MUB.getBestUri(sources)
           ,uriH = uri.replace(/</, '&lt;').replace(/>/, '&gt;').replace(/&/, '&amp;');
        $('#uriA').html(uriH)[0].href = uri;
        $('#uriHtml').val(
            ext === 'js' 
            ? '<script type="text/javascript" src="' + uriH + '"></script>'
            : '<link type="text/css" rel="stylesheet" href="' + uriH + '" />'
        );
        $('#results').show();
    }
    /**
     * Handler for the "Add file +" button
     */
    ,addButtonClick : function () {
        $('#results').hide();
        MUB.addLi();
        MUB.updateAllTestLinks();
        $('#update').show().click(MUB.update);
        $('#sources li:last input')[0].focus();
    }
    /**
     * Runs on DOMready
     */
    ,init : function () {
        $('#app').show();
        $('#sources').html('');
        $('#add button').click(MUB.addButtonClick);
        // make easier to copy text out of
        $('#uriHtml, #groupConfig').click(function () {
            this.select();
        }).focus(function () {
            this.select();
        });
        $('a.ext').attr({target:'_blank'});
        if (location.hash) {
            // make links out of URIs from bookmarklet
            $('#getBm').hide();
            $('#bmUris').html('<p><strong>Found by bookmarklet:</strong> /<a href=#>'
                + location.hash.substr(1).split(',').join('</a> | /<a href=#>')
                + '</a></p>'
            );
            $('#bmUris a').click(function () {
                MUB.addButtonClick();
                $('#sources li:last input').val(this.innerHTML)
                MUB.liUpdateTestLink.call($('#sources li:last')[0]);
                $('#results').hide();
                return false;
            }).attr({title:'Add file +'});
        } else {
            // copy bookmarklet code into href
            var bmUri = location.pathname.replace(/\/[^\/]*$/, '/bm.js').substr(1);
            $.ajax({
                url : '../?f=' + bmUri
                ,success : function (code) {
                    $('#bm')[0].href = code
                        .replace('%BUILDER_URL%', location.href)
                        .replace(/\n/g, ' ');
                }
                ,dataType : 'text'
            });
            $.browser.msie && $('#getBm p:last').append(' Sorry, not supported in MSIE!');
            MUB.addButtonClick();
        }
        MUB.checkRewrite();
    }
};
window.onload = MUB.init;