/* Set the story priview link. */ function setPreview() { if($('#story').val() == 0) { $('#preview').addClass('hidden'); } else { storyLink = createLink('story', 'view', "storyID=" + $('#story').val()); if(!isonlybody) { var concat = storyLink.indexOf('?') < 0 ? '?' : '&'; storyLink = storyLink + concat + 'onlybody=yes'; } $('#preview').addClass('iframe'); $('#preview').removeClass('hidden'); $('#preview').attr('href', storyLink); } } $(function() { var $searchStories = $('#searchStories'); var lastSearchFn = false; var $searchInput = $('#storySearchInput'); var $searchResult = $('#searchResult'); var $selectedItem; var showSearchModal = function() { $searchStories.modal('show').on('shown.zui.modal', function() { var key = $('#story_chosen .chosen-results > li.no-results > span').text(); if(key) $searchInput.val(key).trigger('change'); $searchInput.focus(); }); }; $(document).on('change', '#story', function() { if($(this).val() === 'showmore') showSearchModal(); }); $(document).on('click', '#priRequiredBox', function() { $('#priSelect').removeClass('required'); }); $(document).on('click', '#story_chosen .chosen-results > li.no-results', showSearchModal); $searchStories.on('hide.zui.modal', function() { var key = ''; var $story = $('#story'); if($selectedItem && $selectedItem.length) { key = $selectedItem.data('key'); if(!$story.children('option[value="' + key + '"]').length) { $story.prepend(''); } } $story.val(key).trigger("chosen:updated"); $selectedItem = null; }); var selectItem = function(item) { $selectedItem = $(item).first(); $searchStories.modal('hide'); }; $searchResult.on('click', 'a', function(){selectItem(this);}).on('mouseenter', 'a', function() { $searchResult.find('a.selected').removeClass('selected'); $(this).addClass('selected'); }).on('mouseleave', 'a', function() { $(this).removeClass('selected'); }); $searchInput.on('paste change keyup', function() { if(lastSearchFn) clearTimeout(lastSearchFn); lastSearchFn = setTimeout(function() { var key = $searchInput.val() || ''; if(key && key != $searchInput.data('lastkey')) { $searchResult.empty().append('