web/wp-content/plugins/slideshow-jquery-image-gallery/js/SlideshowPluginSlideInserter/slide-inserter.js
changeset 204 09a1c134465b
parent 203 f507feede89a
child 205 a4f7897e21a9
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
     1 jQuery(document).ready(function(){
       
     2 	// Index first
       
     3 	slideshowSlideInserterIndexSlidesOrder();
       
     4 
       
     5 	// Make list items in the sortables list sortable, exclude elements with cancel option.
       
     6 	jQuery('.sortable-slides-list').sortable({
       
     7 		revert: true,
       
     8 		stop: function(event, ui){
       
     9 			slideshowSlideInserterIndexSlidesOrder();
       
    10 		},
       
    11 		cancel: 'input, select, p'
       
    12 	});
       
    13 
       
    14 	// Make the black background stretch all the way down the document
       
    15 	jQuery('#slideshow-slide-inserter-popup-background').height(jQuery(document).outerHeight(true));
       
    16 
       
    17 	// Center the popup in the window
       
    18 	jQuery('#slideshow-slide-inserter-popup').css({
       
    19 		'top': parseInt((jQuery(window).height() / 2) - (jQuery('#slideshow-slide-inserter-popup').outerHeight(true) / 2), 10),
       
    20 		'left': parseInt((jQuery(window).width() / 2) - (jQuery('#slideshow-slide-inserter-popup').outerWidth(true) / 2), 10)
       
    21 	});
       
    22 
       
    23 	// Focus on search bar
       
    24 	jQuery('#slideshow-slide-inserter-popup #search').focus();
       
    25 
       
    26 	// Preload attachments
       
    27 	slideshowSlideInserterGetSearchResults();
       
    28 
       
    29 	/**
       
    30 	 * Close popup when clicked on cross
       
    31 	 */
       
    32 	jQuery('#slideshow-slide-inserter-popup #close').click(function(){
       
    33 		slideshowSlideInserterClosePopup();
       
    34 	});
       
    35 
       
    36 	/**
       
    37 	 * Close popup when clicked on background
       
    38 	 */
       
    39 	jQuery('#slideshow-slide-inserter-popup-background').click(function(){
       
    40 		slideshowSlideInserterClosePopup();
       
    41 	});
       
    42 
       
    43 	/**
       
    44 	 * Send ajax request on click of the search button
       
    45 	 */
       
    46 	jQuery('#slideshow-slide-inserter-popup #search-submit').click(function(){
       
    47 		slideshowSlideInserterGetSearchResults();
       
    48 	});
       
    49 
       
    50 	/**
       
    51 	 * Make the 'enter' key do the same as the search button
       
    52 	 */
       
    53 	jQuery('#slideshow-slide-inserter-popup #search').keypress(function(event){
       
    54 		if(event.which == 13){
       
    55 			event.preventDefault();
       
    56 			slideshowSlideInserterGetSearchResults();
       
    57 		}
       
    58 	});
       
    59 
       
    60 	/**
       
    61 	 * Open popup by click on button
       
    62 	 */
       
    63 	jQuery('#slideshow-insert-image-slide').click(function(){
       
    64 		jQuery('#slideshow-slide-inserter-popup, #slideshow-slide-inserter-popup-background').css({ display: 'block' });
       
    65 	});
       
    66 
       
    67 	/**
       
    68 	 * Insert text slide into the sortable list when the Insert Text Slide button is clicked
       
    69 	 */
       
    70 	jQuery('#slideshow-insert-text-slide').click(function(){
       
    71 		slideshowSlideInserterInsertTextSlide();
       
    72 	});
       
    73 
       
    74 	/**
       
    75 	 * Insert video slide into the sortable list when the Insert Video Slide button is clicked
       
    76 	 */
       
    77 	jQuery('#slideshow-insert-video-slide').click(function(){
       
    78 		slideshowSlideInserterInsertVideoSlide();
       
    79 	});
       
    80 
       
    81 	/**
       
    82 	 * Ajax deletes a slide from the slides list and from the database
       
    83 	 */
       
    84 	jQuery('.slideshow-delete-slide').click(function(){
       
    85 		var confirmMessage = 'Are you sure you want to delete this slide?';
       
    86 		if(typeof SlideInserterTranslations !== undefined)
       
    87 			confirmMessage = SlideInserterTranslations.confirmMessage;
       
    88 
       
    89 		var deleteSlide = confirm(confirmMessage);
       
    90 		if(!deleteSlide)
       
    91 			return;
       
    92 
       
    93 		// Get postId from url
       
    94 		var postId = -1;
       
    95 		jQuery.each(location.search.replace('?', '').split('&'), function(key, value){
       
    96 			var splitValue = value.split('=');
       
    97 			if(splitValue[0] == 'post')
       
    98 				postId = splitValue[1];
       
    99 		});
       
   100 
       
   101 		// Get slideId
       
   102 		var slideId = jQuery(this).find('span').attr('class');
       
   103 
       
   104 
       
   105 		if(postId == -1 || slideId == 'undefined')
       
   106 			return;
       
   107 
       
   108 		// Remove slide from DOM
       
   109 		jQuery(this).parent().remove();
       
   110 
       
   111 		// Remove slide by AJAX.
       
   112 		jQuery.post(
       
   113 			ajaxurl,
       
   114 			{
       
   115 				action: 'slideshow_delete_slide',
       
   116 				postId: postId,
       
   117 				slideId: slideId
       
   118 			}
       
   119 		);
       
   120 	});
       
   121 
       
   122 	/**
       
   123 	 * Loop through list items, fill hidden field with loop id
       
   124 	 */
       
   125 	function slideshowSlideInserterIndexSlidesOrder(){
       
   126 		jQuery.each(jQuery('.sortable-slides-list').find('li'), function(key, value){
       
   127 			jQuery(value).find('.slide_order').attr('value', key + 1);
       
   128 		});
       
   129 	}
       
   130 
       
   131 	/**
       
   132 	 * Sends an ajax post request with the search query and print
       
   133 	 * retrieved html to the results table.
       
   134 	 *
       
   135 	 * If offset is set, append data to data that is already there
       
   136 	 *
       
   137 	 * @param offset (optional, defaults to 0)
       
   138 	 */
       
   139 	function slideshowSlideInserterGetSearchResults(offset){
       
   140 		if(!offset){
       
   141 			offset = 0;
       
   142 			jQuery('#slideshow-slide-inserter-popup #results').html('');
       
   143 		}
       
   144 
       
   145 		jQuery.post(
       
   146 			ajaxurl,
       
   147 			{
       
   148 				action: 'slideshow_slide_inserter_search_query',
       
   149 				search: jQuery('#slideshow-slide-inserter-popup #search').attr('value'),
       
   150 				offset: offset
       
   151 			},
       
   152 			function(response){
       
   153 				// Fill table
       
   154 				jQuery('#slideshow-slide-inserter-popup #results').append(response);
       
   155 
       
   156 				// Apply insert to slideshow script
       
   157 				jQuery('#slideshow-slide-inserter-popup #results .insert-attachment').click(function(){
       
   158 					var tr = jQuery(this).closest('tr');
       
   159 					slideshowSlideInserterInsertImageSlide(
       
   160 						jQuery(this).attr('id'),
       
   161 						jQuery(tr).find('.title').text(),
       
   162 						jQuery(tr).find('.description').text(),
       
   163 						jQuery(tr).find('.image img').attr('src')
       
   164 					);
       
   165 				});
       
   166 
       
   167 				// Load more results on click of the 'Load more results' button
       
   168 				if(jQuery('.load-more-results')){
       
   169 					jQuery('.load-more-results').click(function(){
       
   170 						// Get offset
       
   171 						var previousOffset = jQuery(this).attr('class').split(' ')[2];
       
   172 
       
   173 						// Load ajax results
       
   174 						slideshowSlideInserterGetSearchResults(previousOffset);
       
   175 
       
   176 						// Remove button row
       
   177 						jQuery(this).closest('tr').remove();
       
   178 					});
       
   179 				}
       
   180 			}
       
   181 		);
       
   182 	}
       
   183 
       
   184 	/**
       
   185 	 * Inserts image slide into the slides list
       
   186 	 *
       
   187 	 * @param id
       
   188 	 * @param title
       
   189 	 * @param description
       
   190 	 * @param src
       
   191 	 */
       
   192 	function slideshowSlideInserterInsertImageSlide(id, title, description, src){
       
   193 		if(slideshowHighestSlideId == 'undefined')
       
   194 			return;
       
   195 
       
   196 		slideshowHighestSlideId++;
       
   197 		var imageSlide = jQuery('.image-slide-template').find('li').clone();
       
   198 
       
   199 		// Fill slide with data
       
   200 		imageSlide.find('.attachment').attr('src', src);
       
   201 		imageSlide.find('.attachment').attr('title', title);
       
   202 		imageSlide.find('.attachment').attr('alt', title);
       
   203 		imageSlide.find('.title').html(title);
       
   204 		imageSlide.find('.description').html(description);
       
   205 		imageSlide.find('.postId').attr('value', id);
       
   206 
       
   207 		// Set names to be saved to the database
       
   208 		imageSlide.find('.url').attr('name', 'slide_' + slideshowHighestSlideId + '_url');
       
   209 		imageSlide.find('.type').attr('name', 'slide_' + slideshowHighestSlideId + '_type');
       
   210 		imageSlide.find('.postId').attr('name', 'slide_' + slideshowHighestSlideId + '_postId');
       
   211 		imageSlide.find('.slide_order').attr('name', 'slide_' + slideshowHighestSlideId + '_order');
       
   212 
       
   213 		// Register delete link (only needs to delete from DOM)
       
   214 		imageSlide.find('.slideshow-delete-new-slide').click(function(){
       
   215 			var deleteSlide = confirm('Are you sure you want to delete this slide?');
       
   216 			if(!deleteSlide)
       
   217 				return;
       
   218 
       
   219 			jQuery(this).closest('li').remove();
       
   220 		});
       
   221 
       
   222 		// Put slide in the sortables list.
       
   223 		jQuery('.sortable-slides-list').prepend(imageSlide);
       
   224 
       
   225 		// Reindex
       
   226 		slideshowSlideInserterIndexSlidesOrder();
       
   227 	}
       
   228 
       
   229 	/**
       
   230 	 * Inserts text slide into the slides list
       
   231 	 */
       
   232 	function slideshowSlideInserterInsertTextSlide(){
       
   233 		if(slideshowHighestSlideId == 'undefined')
       
   234 			return;
       
   235 
       
   236 		slideshowHighestSlideId++;
       
   237 		var textSlide = jQuery('.text-slide-template').find('li').clone();
       
   238 
       
   239 		// Set names to be saved to the database
       
   240 		textSlide.find('.title').attr('name', 'slide_' + slideshowHighestSlideId + '_title');
       
   241 		textSlide.find('.description').attr('name', 'slide_' + slideshowHighestSlideId + '_description');
       
   242 		textSlide.find('.color').attr('name', 'slide_' + slideshowHighestSlideId + '_color');
       
   243 		textSlide.find('.url').attr('name', 'slide_' + slideshowHighestSlideId + '_url');
       
   244 		textSlide.find('.type').attr('name', 'slide_' + slideshowHighestSlideId + '_type');
       
   245 		textSlide.find('.slide_order').attr('name', 'slide_' + slideshowHighestSlideId + '_order');
       
   246 
       
   247 		// Register delete link (only needs to delete from DOM)
       
   248 		textSlide.find('.slideshow-delete-new-slide').click(function(){
       
   249 			var deleteSlide = confirm('Are you sure you want to delete this slide?');
       
   250 			if(!deleteSlide)
       
   251 				return;
       
   252 
       
   253 			jQuery(this).closest('li').remove();
       
   254 		});
       
   255 
       
   256 		// Put slide in the sortables list.
       
   257 		jQuery('.sortable-slides-list').prepend(textSlide);
       
   258 
       
   259 		// Reindex slide orders
       
   260 		slideshowSlideInserterIndexSlidesOrder();
       
   261 	}
       
   262 
       
   263 	/**
       
   264 	 * Inserts video slide into the slides list
       
   265 	 */
       
   266 	function slideshowSlideInserterInsertVideoSlide(){
       
   267 		if(slideshowHighestSlideId == 'undefined')
       
   268 			return;
       
   269 
       
   270 		slideshowHighestSlideId++;
       
   271 		var videoSlide = jQuery('.video-slide-template').find('li').clone();
       
   272 
       
   273 		// Set names to be saved to the database
       
   274 		videoSlide.find('.videoId').attr('name', 'slide_' + slideshowHighestSlideId + '_videoId');
       
   275 		videoSlide.find('.type').attr('name', 'slide_' + slideshowHighestSlideId + '_type');
       
   276 		videoSlide.find('.slide_order').attr('name', 'slide_' + slideshowHighestSlideId + '_order');
       
   277 
       
   278 		// Register delete link (only needs to delete from DOM)
       
   279 		videoSlide.find('.slideshow-delete-new-slide').click(function(){
       
   280 			var deleteSlide = confirm('Are you sure you want to delete this slide?');
       
   281 			if(!deleteSlide)
       
   282 				return;
       
   283 
       
   284 			jQuery(this).closest('li').remove();
       
   285 		});
       
   286 
       
   287 		// Put slide in the sortables list.
       
   288 		jQuery('.sortable-slides-list').prepend(videoSlide);
       
   289 
       
   290 		// Reindex slide orders
       
   291 		slideshowSlideInserterIndexSlidesOrder();
       
   292 	}
       
   293 
       
   294 	/**
       
   295 	 * Closes popup
       
   296 	 */
       
   297 	function slideshowSlideInserterClosePopup(){
       
   298 		jQuery('#slideshow-slide-inserter-popup, #slideshow-slide-inserter-popup-background').css({ display: 'none' });
       
   299 	}
       
   300 });