|
| 1 | +(function() { |
| 2 | + |
| 3 | + var log = (function(){ |
| 4 | + var logger = function(content, skip) { |
| 5 | + if ( !skip ) { |
| 6 | + content = "<div>"+content+"</div>"; |
| 7 | + } |
| 8 | + logger.output.push( content ); |
| 9 | + }; |
| 10 | + logger.heading = function( content ) { |
| 11 | + logger( "<h2>"+content+"</h2>", true ); |
| 12 | + }; |
| 13 | + logger.details = function( content ) { |
| 14 | + logger( "<pre>"+content+"</pre>", true ); |
| 15 | + }; |
| 16 | + logger.output = []; |
| 17 | + return logger; |
| 18 | + })(); |
| 19 | + |
| 20 | + var helpLinks = { |
| 21 | + people: "<a href=\"/docs/package-manifest/#people-fields\">People Fields</a>" |
| 22 | + }; |
| 23 | + |
| 24 | + var handler = function( e ) { |
| 25 | + e.preventDefault(); |
| 26 | + var output = $( ".validator-output" ).empty(); |
| 27 | + var files = e.target.files; |
| 28 | + var file = files && files[0]; |
| 29 | + if ( !file ) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + var filename = file.name.replace(".jquery.json", "" ); |
| 34 | + var reader = new FileReader(); |
| 35 | + |
| 36 | + // Closure to capture the file information. |
| 37 | + reader.onload = function( e ) { |
| 38 | + var result = e.target.result; |
| 39 | + var manifest; |
| 40 | + try { |
| 41 | + manifest = $.parseJSON( result ); |
| 42 | + } catch( e ) { |
| 43 | + log.heading( "JSON Validation Error" ); |
| 44 | + log( "Your json is not valid. See <a href=\"//json.org\">json.org</a> for more information." ); |
| 45 | + log.details( e.name + ": " + e.type + " " + e['arguments'].join(',') ); |
| 46 | + } |
| 47 | + if ( manifest ) { |
| 48 | + var required = "name version title author licenses dependencies".split(" "); |
| 49 | + $.each( required, function( i, v ) { |
| 50 | + if ( !manifest[ v ] ) { |
| 51 | + log( "<code>" + v + "</code> attribute is required" ); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + if ( manifest.name && ( manifest.name !== filename ) ) { |
| 56 | + log( "expected <code>" + manifest.name + ".jquery.json</code> as filename due " + |
| 57 | + "to <code>name</code> attribute of '" + manifest.name + "'" ); |
| 58 | + } |
| 59 | + |
| 60 | + if ( manifest.author ) { |
| 61 | + if ( typeof manifest.author !== "object" ) { |
| 62 | + log( "<code>author</code> property must be an object. See " + helpLinks.people + " for more information" ); |
| 63 | + } else if ( !manifest.author.name ) { |
| 64 | + log( "the <code>name</code> propety of the <code>author</code> people object is required. See " + helpLinks.people + " for more information" ); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + var logContent = log.output.join(''); |
| 69 | + output.html( logContent || "<h2>Your manifest file passed all tests</h2>" ); |
| 70 | + }; |
| 71 | + |
| 72 | + // Read in the image file as a data URL. |
| 73 | + reader.readAsText(file); |
| 74 | + }; |
| 75 | + |
| 76 | + $( document ).ready( function() { |
| 77 | + $( 'input[name="files"]' ).on( 'change', handler ); |
| 78 | + }); |
| 79 | + |
| 80 | +})(); |
0 commit comments