jQuery(function($) { $(".lead-button").live('click', function(){ article_id = this.getAttribute("article_id"); $(this).toggleClass('icon-add').toggleClass('icon-remove'); $(article_id).slideToggle(); return false; }) $("#body-button").click(function(){ $(this).toggleClass('icon-add').toggleClass('icon-remove'); $('#article-body-field').slideToggle(); return false; }) $("#textile-quickref-show").click(function(){ $('#textile-quickref-hide').show(); $(this).hide(); $('#textile-quickref').slideToggle(); return false; }) $("#textile-quickref-hide").click(function(){ $('#textile-quickref-show').show(); $(this).hide(); $('#textile-quickref').slideToggle(); return false; }) var button_add = $('.text-editor-sidebar meta[name=button.add]').attr('value'); var button_zoom = $('.text-editor-sidebar meta[name=button.zoom]').attr('value'); function add_to_text_button(item) { return '' + button_add + ''; } function zoom_button(item) { return '' + button_zoom + '/span>'; } function list_items(items, selector) { var html_for_items = ''; var images = []; var files = []; var errors = []; $.each(items, function(i, item) { if (item.error) { errors.push(item); return; } if (item.content_type && item.content_type.match(/^image/)) { images.push(item); } else { files.push(item); } }); $.each(images, function(i, item) { html_for_items += '
' + add_to_text_button(item) + zoom_button(item) + '
'; }); $.each(files, function(i, item) { html_for_items += '
' + item.title + '
' + add_to_text_button(item) + '
'; }); $.each(errors, function(i, item) { html_for_items += '
' + item.error + '
'; }); $(selector).html(html_for_items); $(selector).find('.controls a.icon-add').click(function() { var $item = $(this).closest('.item'); var html_selector = $item.attr('data-item'); insert_item_in_text($item.find(html_selector)); return false; }); $(selector).find('.controls a.icon-zoom').click(function() { var $item = $(this).closest('.item'); var html_selector = $item.attr('data-item'); var img = $item.find(html_selector).find('img').attr('src'); $.colorbox({ html: '', maxWidth: '640px', maxHeight: '640px', scrolling: false }); return false; }); } // FIXME the user may also want to add the item to the abstract textarea! var text_field = 'article_body'; function insert_item_in_text($wrapper) { if (window.tinymce) { var html = $wrapper.html(); var editor = tinymce.get(text_field); editor.setContent(editor.getContent() + html); } else { // simple text editor var text = $('#' + text_field).val(); var $item = $wrapper.children().first(); if ($item.attr('src')) { $('#article_body').val(text + '!' + $item.attr('src') + '!'); } if ($item.attr('href')) { $('#article_body').val(text + $item.attr('href')); } } } $('#media-search-button').click(function() { var query = '*' + $('#media-search-query').val() + '*'; var $button = $(this); $('#media-search-box .header').toggleClass('icon-loading'); $.get($(this).parent().attr('action'), { 'q': query }, function(data) { list_items(data, '#media-search-results .items'); if (data.length && data.length > 0) { $('#media-search-results').slideDown(); } $('#media-search-box .header').toggleClass('icon-loading'); }); return false; }); $('#media-upload-form form').ajaxForm({ dataType: 'json', resetForm: true, beforeSubmit: function() { $('#media-upload-form').slideUp(); $('#media-upload-box .header').toggleClass('icon-loading'); }, success: function(data) { list_items(data, '#media-upload-results .items'); if (data.length && data.length > 0) { $('#media-upload-results').slideDown(); } $('#media-upload-box .header').toggleClass('icon-loading'); } }); $('#media-upload-more-files').click(function() { $('#media-upload-results').hide(); $('#media-upload-form').show(); }); });