From cb17f798614f4a25c0b062608897264b6c73aaaa Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Tue, 29 Aug 2017 18:28:25 +0200 Subject: [PATCH] Enhance nav menu script + jsDoc --- includes/class-menu-meta-box.php | 2 +- js/nav-menu.js | 60 ++++++++++---------------------- slidebars/slidebars.js | 12 +++++-- slidebars/slidebars.min.js | 12 +++++-- 4 files changed, 38 insertions(+), 48 deletions(-) diff --git a/includes/class-menu-meta-box.php b/includes/class-menu-meta-box.php index f242eba..5307812 100644 --- a/includes/class-menu-meta-box.php +++ b/includes/class-menu-meta-box.php @@ -154,7 +154,7 @@ public function admin_enqueue_scripts() { $data['val'][ $item ] = get_post_meta( $item, '_off_canvas_control_menu_item', true ); // send all these data to javascript - wp_localize_script( 'off_canvas_control_nav_menu', 'off_canvas_control_data', $data ); + wp_localize_script( 'off_canvas_control_nav_menu', 'ocsNavControl', $data ); } /* diff --git a/js/nav-menu.js b/js/nav-menu.js index 315a6b5..9f0afe7 100644 --- a/js/nav-menu.js +++ b/js/nav-menu.js @@ -5,12 +5,14 @@ * @author Jory Hogeveen * @package off-canvas-slidebars * @version 0.4 + * @global ocsNavControl + * @preserve * * Credits to the Polylang plugin for inspiration */ -if ( 'undefined' === typeof off_canvas_control_data ) { - var off_canvas_control_data = {}; +if ( 'undefined' === typeof ocsNavControl ) { + var ocsNavControl = {}; } jQuery( document ).ready( function( $ ) { @@ -22,41 +24,35 @@ jQuery( document ).ready( function( $ ) { $this.addClass('off-canvas-control'); /* If control selected, then show it */ - if ( off_canvas_control_data.val[ db_id ]['off-canvas-control']) { - var key = off_canvas_control_data.val[ db_id ]['off-canvas-control']; - control_type = ' (' + off_canvas_control_data.controls[ key ] + ')'; + if ( ocsNavControl.val[ db_id ]['off-canvas-control']) { + var key = ocsNavControl.val[ db_id ]['off-canvas-control']; + control_type = ' (' + ocsNavControl.controls[ key ] + ')'; } - $this.find('.menu-item-bar .item-type').html( off_canvas_control_data.strings.menu_item_type + control_type ); + $this.find('.menu-item-bar .item-type').html( ocsNavControl.strings.menu_item_type + control_type ); }); - /* change menu type label on selecting a controller */ + /* Change menu type label on selecting a controller */ $(document).on( 'change', '.field-off-canvas-control input', function() { var key = $(this).val(); - var control_type = ' (' + off_canvas_control_data.controls[ key ] + ')'; - $(this).parents('.menu-item').find('.menu-item-bar .item-type').html( off_canvas_control_data.strings.menu_item_type + control_type ); + var control_type = ' (' + ocsNavControl.controls[ key ] + ')'; + $(this).parents('.menu-item').find('.menu-item-bar .item-type').html( ocsNavControl.strings.menu_item_type + control_type ); } ); - /* init/change menu item options */ + /* Init/change menu item options */ $('#update-nav-menu').bind( 'click load', function(e) { if ( e.target && e.target.className && -1 < e.target.className.indexOf('item-edit')) { $('input[value="#off_canvas_control"][type=text]').parent().parent().parent().each( function() { var $this = $(this), item = $this.attr('id').substring(19), - strings = off_canvas_control_data.strings, - controls = off_canvas_control_data.controls, + strings = ocsNavControl.strings, + controls = ocsNavControl.controls, option; - $this.children('p.field-url, p.field-link-target, .field-xfn, .field-description').remove(); // remove default fields we don't need + // Remove default fields we don't need + $this.children('p.field-url, p.field-link-target, .field-xfn, .field-description').remove(); + // Change description width. $this.children('p.field-css-classes').removeClass('description-thin').addClass('description-wide'); - /*option = $('').attr({ - type: 'hidden', - id: 'edit-menu-item-title-'+item, - name: 'menu-item-title['+item+']', - value: off_canvas_control_data.title - }); - $this.append(option);*/ - option = $('').attr( { type: 'hidden', id: 'edit-menu-item-url-' + item, @@ -81,7 +77,7 @@ jQuery( document ).ready( function( $ ) { continue; } var checked = ''; - if (( 'undefined' !== typeof off_canvas_control_data.val[ item ] && key === off_canvas_control_data.val[ item ]['off-canvas-control'] ) ) { + if (( 'undefined' !== typeof ocsNavControl.val[ item ] && key === ocsNavControl.val[ item ]['off-canvas-control'] ) ) { checked = ' checked="checked"'; } o += ' '; @@ -95,25 +91,7 @@ jQuery( document ).ready( function( $ ) { if ( 1 === $field_control_options.length ) { $field_control_options.prop('checked', true); } - /* - ids = Array('menu_item_type'); // reverse order - // add the fields - for(var i = 0; i < ids.length; i++) { - p = $('

').attr('class', 'description'); - $(this).prepend(p); - label = $('