min
8 years ago
config.js
9 years ago
debug.js
9 years ago
delete.js
9 years ago
editor.js
8 years ago
podiff.js
8 years ago
poinit.js
8 years ago
potinit.js
8 years ago
poview.js
9 years ago
setup.js
9 years ago
setup.js
129 lines
| 1 | /** |
| 2 | * Script for bundle setup page |
| 3 | * TODO translations |
| 4 | */ |
| 5 | !function( window, document, $ ){ |
| 6 | |
| 7 | |
| 8 | /** |
| 9 | * Look up bundle configuration on remote server |
| 10 | */ |
| 11 | function find( vendor, slug, version ){ |
| 12 | |
| 13 | function onFailure(){ |
| 14 | if( timer ){ |
| 15 | destroy(); |
| 16 | onTimeout(); |
| 17 | } |
| 18 | }; |
| 19 | |
| 20 | function onResponse( data, status, obj ){ |
| 21 | if( timer ){ |
| 22 | destroy(); |
| 23 | var match = data && data.exact, |
| 24 | status = data && data.status |
| 25 | ; |
| 26 | if( match ){ |
| 27 | setJson( match ); |
| 28 | } |
| 29 | else if( 404 === status ){ |
| 30 | unsetJson("Sorry, we don't know a bundle by this name"); |
| 31 | } |
| 32 | else { |
| 33 | loco.notices.debug( data.error || 'Unknown server error' ); |
| 34 | onTimeout(); |
| 35 | } |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | function onTimeout(){ |
| 40 | unsetJson('Failed to contact remote API'); |
| 41 | timer = null; |
| 42 | } |
| 43 | |
| 44 | function destroy(){ |
| 45 | if( timer ){ |
| 46 | clearTimeout( timer ); |
| 47 | timer = null; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | var timer = setTimeout( onTimeout, 3000 ); |
| 52 | |
| 53 | unsetJson(''); |
| 54 | |
| 55 | $.ajax( { |
| 56 | url: conf.apiUrl+'/'+vendor+'/'+slug+'.jsonp?version='+encodeURIComponent(version), |
| 57 | dataType: 'jsonp', |
| 58 | success: onResponse, |
| 59 | error: onFailure, |
| 60 | cache: true |
| 61 | } ); |
| 62 | |
| 63 | return { |
| 64 | abort: destroy |
| 65 | }; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | function setJson( json ){ |
| 70 | elForm['json-content'].value = json; |
| 71 | $('#loco-remote-empty').hide(); |
| 72 | //$('#loco-remote-query').hide(); |
| 73 | $('#loco-remote-found').show(); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | function unsetJson( message ){ |
| 78 | elForm['json-content'].value = ''; |
| 79 | //$('#loco-remote-query').show(); |
| 80 | $('#loco-remote-empty').show().find('span').text( message ); |
| 81 | $('#loco-remote-found').hide().removeClass('jshide'); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | function onFindClick( event ){ |
| 86 | event.preventDefault(); |
| 87 | finder && finder.abort(); |
| 88 | finder = find( elForm.vendor.value, elForm.slug.value, elForm.version.value ); |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | function onCancelClick( event ){ |
| 93 | event.preventDefault(); |
| 94 | unsetJson(''); |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | function setVendors( list ){ |
| 99 | var i = -1, |
| 100 | value, label, |
| 101 | length = list.length, |
| 102 | $select = $(elForm.vendor).html('') |
| 103 | ; |
| 104 | while( ++i < length ){ |
| 105 | value = list[i][0]; |
| 106 | label = list[i][1]; |
| 107 | $select.append( $('<option></option>').attr('value',value).text(label) ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |
| 112 | var loco = window.locoScope, |
| 113 | conf = window.locoConf, |
| 114 | |
| 115 | finder, |
| 116 | elForm = document.getElementById('loco-remote'), |
| 117 | $findButt = $(elForm).find('button[type="button"]').click( onFindClick ), |
| 118 | $resetButt = $(elForm).find('input[type="reset"]').click( onCancelClick ); |
| 119 | |
| 120 | // pull vendor list |
| 121 | $.ajax( { |
| 122 | url: conf.apiUrl+'/vendors.jsonp', |
| 123 | dataType: 'jsonp', |
| 124 | success: setVendors, |
| 125 | cache: true |
| 126 | } ); |
| 127 | |
| 128 | |
| 129 | }( window, document, jQuery ); |