|
| 1 | +/** |
| 2 | + * NPR Story API meta box functions and features |
| 3 | + */ |
| 4 | +document.addEventListener('DOMContentLoaded', () => { |
| 5 | + 'use strict'; |
| 6 | + var $ = jQuery; |
| 7 | + |
| 8 | + // contains the inputs |
| 9 | + var $container = $( '#ds-npr-publish-actions' ); |
| 10 | + |
| 11 | + // initialize the form |
| 12 | + $container.find( 'input' ).on( 'change', li_checking ); |
| 13 | + |
| 14 | + // Upon update, do the thing |
| 15 | + li_checking.call( $container.find( '#send_to_api' ) ); |
| 16 | + |
| 17 | + /* |
| 18 | + * If a checkbox in an li gets unchecked, uncheck and disable its child li |
| 19 | + * If a checkbox in an li gets checked, enable its child li |
| 20 | + */ |
| 21 | + function li_checking( event ) { |
| 22 | + var checked = $( this ).prop('checked'); |
| 23 | + var $results = $( this ).closest( 'li' ).children( 'ul' ).children( 'li' ); // Only get the first level of list. |
| 24 | + $results.each( function( element ) { |
| 25 | + // Triggering the change event on the child does not work. |
| 26 | + if ( checked ) { |
| 27 | + var recurse = $( this ).children( 'label' ).children( 'input' ).prop( 'disabled', false ); |
| 28 | + li_checking.call( recurse ); |
| 29 | + } else { |
| 30 | + recurse = $( this ).children( 'label' ).children( 'input' ).prop( 'disabled', true ).prop( 'checked', false ); |
| 31 | + li_checking.call( recurse ); |
| 32 | + } |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + // edit the time selector |
| 37 | + $( '#nprone-expiry-edit' ).on( 'click', function( event ) { |
| 38 | + event.preventDefault(); |
| 39 | + $( '#nprone-expiry-form' ).toggleClass( 'hidden' ); |
| 40 | + $( this ).toggleClass( 'hidden' ); |
| 41 | + }); |
| 42 | + // close the time selector |
| 43 | + $( '#nprone-expiry-cancel' ).on( 'click', function( event ) { |
| 44 | + event.preventDefault(); |
| 45 | + $( '#nprone-expiry-form' ).toggleClass( 'hidden' ); |
| 46 | + $( '#nprone-expiry-edit' ).toggleClass( 'hidden' ); |
| 47 | + }); |
| 48 | + // save the time selector |
| 49 | + $( '#nprone-expiry-ok' ).on( 'click', function( event ) { |
| 50 | + event.preventDefault(); |
| 51 | + $( '#nprone-expiry-form' ).toggleClass( 'hidden' ); |
| 52 | + $( '#nprone-expiry-edit' ).toggleClass( 'hidden' ); |
| 53 | + |
| 54 | + // but then it needs to update the displayed data in #nprone-expiry-display. How is it to do that? |
| 55 | + // This needs to take: |
| 56 | + // - the YYYY-MM-DD value of #nprone-expiry-datepicker |
| 57 | + // - the HH:MM value of #nprone-expiry-hour |
| 58 | + // and output a string in the format Apr 1, 2020 @ 09:01 / Nov 1, 2018 @ 23:59 |
| 59 | + var d = new Date(); |
| 60 | + var dateinput = $( '#nprone-expiry-datepicker' ).val().split('-'); |
| 61 | + var timeinput = $( '#nprone-expiry-hour' ).val().split(':'); |
| 62 | + |
| 63 | + d.setFullYear(dateinput[0]); |
| 64 | + d.setMonth(dateinput[1] - 1); // because this is zero-indexed? |
| 65 | + d.setDate(dateinput[2]); |
| 66 | + d.setHours(timeinput[0]); |
| 67 | + d.setMinutes(timeinput[1]); |
| 68 | + |
| 69 | + var string = d.toLocaleString("en-us", { month: "short" }) |
| 70 | + + " " |
| 71 | + + d.getDate() |
| 72 | + + ", " |
| 73 | + + d.getFullYear() |
| 74 | + + " @ " |
| 75 | + + d.getHours() |
| 76 | + + ":" |
| 77 | + + (d.getMinutes() < 10? '0' : '') + d.getMinutes(); |
| 78 | + |
| 79 | + $( '#nprone-expiry-display time' ).text( string ); |
| 80 | + }); |
| 81 | + |
| 82 | + |
| 83 | + // Activate the date picker, if and only if the browser doesn't have a native datepicker |
| 84 | + // @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date#JavaScript |
| 85 | + var test = document.createElement( 'input' ); |
| 86 | + test.type = 'date'; |
| 87 | + if ( test.type !== 'date' ) { |
| 88 | + $( '#nprone-expiry-datepicker' ).attr('type', 'text').css('width', '8em').datepicker({ |
| 89 | + dateFormat: 'yy-mm-dd' |
| 90 | + }); |
| 91 | + } |
| 92 | +}); |
0 commit comments