Created
July 5, 2016 18:21
-
-
Save mikecharles/19b92107a0436fb126e53d7512a47d20 to your computer and use it in GitHub Desktop.
Browse image files by date/lead
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Image Browser</title> | |
| <style> | |
| img[name=image] { | |
| max-width: 100%; | |
| height: auto; | |
| } | |
| </style> | |
| <script src="https://code.jquery.com/jquery-3.0.0.slim.min.js" crossorigin="anonymous"></script> | |
| <script src="markup.min.js" type="text/javascript"></script> | |
| <script src="script.js" type="text/javascript"></script> | |
| </head> | |
| <body> | |
| <form action="javascript:void(0);"> | |
| <input type="text" name="issued_date" placeholder="issued date"> | |
| <button type="button" name="previous"><</button> | |
| <button type="button" name="next">></button><br> | |
| <input type="radio" name="lead" value="d08_d10" checked="checked">8-10day | |
| <input type="radio" name="lead" value="d10_d12">10-12day | |
| <input type="radio" name="lead" value="d12_d14">12-14day | |
| </form> | |
| <img name='image' src=''></img> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var app = {}; | |
| app.imgURLTemplate = '{{year}}/{{month}}/{{day}}/00/gefs-rfcst_precip_{{year}}{{month}}{{day}}_00z_{{lead}}_{{ptile}}.png' | |
| app.variables = { | |
| year: '2012', | |
| month: '03', | |
| day: '01', | |
| lead: 'd08_d10', | |
| ptile: 'gt85' | |
| } | |
| app.imgURL = Mark.up(app.imgURLTemplate, app.variables); | |
| function updateVariables(name, value) { | |
| app.variables[name] = value; | |
| } | |
| function updateImgURL(template, variables) { | |
| app.imgURL = Mark.up(template, variables); | |
| } | |
| function setIssuedDateInputVal(issuedDate) { | |
| var [year, month, day] = splitDateIntoVariables(issuedDate); | |
| $('input[name=issued_date]').val(year+month+day); | |
| } | |
| function splitDateIntoVariables(date) { | |
| var array = new Array(); | |
| console.log('In splitDateIntoVariables, date = '+date); | |
| array[0] = date.substring(0, 4); | |
| array[1] = date.substring(4, 6); | |
| array[2] = date.substring(6, 8); | |
| return array; | |
| } | |
| function updateIssuedDateVariables(issuedDate) { | |
| var [year, month, day] = splitDateIntoVariables(issuedDate); | |
| updateVariables('year', year); | |
| updateVariables('month', month); | |
| updateVariables('day', day); | |
| } | |
| function addSubtractDays(date, numDays) { | |
| var [year, month, day] = splitDateIntoVariables(date); | |
| d1 = new Date(year, month-1, day); | |
| var d2 = new Date(d1); | |
| d2.setDate(d1.getDate() + numDays); | |
| return '' + d2.getFullYear() + ('0' + (d2.getMonth()+1)).slice(-2) + ('0' + d2.getDate()).slice(-2); | |
| } | |
| function goToPrevDay() { | |
| var issuedDate = $("input[name=issued_date]").val(); | |
| issuedDate = addSubtractDays(issuedDate, -1); | |
| updateIssuedDateVariables(issuedDate); | |
| updateImgURL(app.imgURLTemplate, app.variables); | |
| $('img[name=image]').attr('src', app.imgURL); | |
| $('input[name=issued_date]').val(issuedDate); | |
| } | |
| function goToNextDay() { | |
| var issuedDate = $("input[name=issued_date]").val(); | |
| issuedDate = addSubtractDays(issuedDate, 1); | |
| updateIssuedDateVariables(issuedDate); | |
| updateImgURL(app.imgURLTemplate, app.variables); | |
| $('img[name=image]').attr('src', app.imgURL); | |
| $('input[name=issued_date]').val(issuedDate); | |
| } | |
| $(document).ready(function() { | |
| // Default image | |
| $('img[name=image]').attr('src', app.imgURL); | |
| // Default date in text input | |
| setIssuedDateInputVal(app.variables.year+app.variables.month+app.variables.day); | |
| // Update lead | |
| $("input[name=lead]").change(function() { | |
| app.variables.lead = $(this).val(); | |
| updateImgURL(app.imgURLTemplate, app.variables); | |
| $('img[name=image]').attr('src', app.imgURL); | |
| }); | |
| // Update issued date by entering a date | |
| $("input[name=issued_date]").keyup(function(e) { | |
| if (e.keyCode == 13) { | |
| var issuedDate = $("input[name=issued_date]").val(); | |
| updateIssuedDateVariables(issuedDate); | |
| updateImgURL(app.imgURLTemplate, app.variables); | |
| $('img[name=image]').attr('src', app.imgURL); | |
| } | |
| }); | |
| $("input[name=issued_date]").blur(function(e) { | |
| var issuedDate = $("input[name=issued_date]").val(); | |
| updateIssuedDateVariables(issuedDate); | |
| updateImgURL(app.imgURLTemplate, app.variables); | |
| $('img[name=image]').attr('src', app.imgURL); | |
| }); | |
| // Update issued date by clicking 'previous' or 'next' | |
| $('button[name=previous]').click(function() { | |
| goToPrevDay(); | |
| }); | |
| $('button[name=next]').click(function() { | |
| goToNextDay(); | |
| }); | |
| // Update issued date by hitting the left and right arrow keys | |
| $(document).keydown(function(e) { | |
| switch(e.which) { | |
| case 37: goToPrevDay(); | |
| break; | |
| case 38: goToPrevDay(); | |
| break; | |
| case 39: goToNextDay(); | |
| break; | |
| case 40: goToNextDay(); | |
| break; | |
| default: return; // exit this handler for other keys | |
| } | |
| e.preventDefault(); // prevent the default action (scroll / move caret) | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment