/* toggles the visibility of an element */ function timezonecalculator_toggle_element(link, element) { if (jQuery('#'+element).css('display')=='none') { jQuery('#'+element).show(); jQuery(link).attr('title', 'hide details'); jQuery(link).removeClass('dashicons-arrow-down').addClass('dashicons-arrow-up'); } else { jQuery('#'+element).hide(); jQuery(link).attr('title', 'show details'); jQuery(link).removeClass('dashicons-arrow-up').addClass('dashicons-arrow-down'); } } /* moves an element in a list one position up */ function timezonecalculator_move_element_up(row) { /* move the element up */ var current=jQuery('#timezonecalculator_timezone_'+row); current.prev().before(current); /* update the lists */ timezonecalculator_update_lists(); } /* moves an element in a list one position down */ function timezonecalculator_move_element_down(row) { /* move the element down */ var current=jQuery('#timezonecalculator_timezone_'+row); current.next().after(current); /* update the lists */ timezonecalculator_update_lists(); } /* moves an element to the other list */ function timezonecalculator_move_element(row) { /* move the element to other list */ var current=jQuery('#timezonecalculator_timezone_'+row); if (current.parent('ul').attr('id')=='timezonecalculator_list_selected') current.appendTo('#timezonecalculator_list_available'); else if (current.parent('ul').attr('id') =='timezonecalculator_list_available') current.appendTo('#timezonecalculator_list_selected'); /* update the lists */ timezonecalculator_update_lists(); } /* initializes the drag and drop lists */ function timezonecalculator_initialize_drag_and_drop() { /* add sortable to both lists */ jQuery(function() { jQuery('#timezonecalculator_list_selected, #timezonecalculator_list_available').sortable({ connectWith: '.timezonecalculator_sortablelist' }).disableSelection(); }); /* add event handlers to watch update on list_selected - stop - receive - remove */ jQuery('#timezonecalculator_list_selected').on('sortstop sortreceive sortremove', function() { timezonecalculator_update_lists(); }); } /* returns the sorted ids of a timezones list */ function timezonecalculator_get_sorted_ids(list) { /* get current timezones order */ var maybe_sorted_ids=jQuery('#timezonecalculator_list_'+list).sortable('toArray'); var sorted_ids=[-1]; /* if we got at least one element in the list, retrieve the sorted_ids */ if (maybe_sorted_ids && maybe_sorted_ids.length>0) { var ret_sorted_ids=[]; /* loop through all ids and filter out empty elements */ for (var i=0; i-1) { ret_sorted_ids.push(maybe_sorted_ids[i]); } } if (ret_sorted_ids.length>0) sorted_ids=ret_sorted_ids; } return sorted_ids; } /* sets list height of timezones list according to the number of elements */ function timezonecalculator_set_list_height(list, sorted_ids) { var element_height=34; /* calculate new list height of list_selected */ var list_length=element_height; if (sorted_ids.length>1) list_length=sorted_ids.length*element_height; /* set new list height */ jQuery('#timezonecalculator_list_'+list).height(list_length); } /* timezones lists update function updates timezones textarea */ function timezonecalculator_update_lists() { /* get sorted ids of list selected */ var selected_sorted_ids=timezonecalculator_get_sorted_ids('selected'); /* clear current entries in textarea */ jQuery('#timezonecalculator_timezones').val(''); /* loop through all selected_sorted_ids and append them to the textarea */ for (var i=0; i