Commit b1e82570 authored by JacobSingh's avatar JacobSingh

Added dependency on dialog_api, got format form in alpha state. Has a major...

Added dependency on dialog_api, got format form in alpha state.  Has a major bug though, try adding two images
parent 444c2307
Drupal.media = Drupal.media || {};
(function ($) {
Drupal.behaviors.mediaFormatForm = {
attach: function (context, settings) {
// Create the jQuery UI dialog box to handle all dialog events.
$("#media-format-form").bind('submit', Drupal.media.mediaFormatSelected);
}
};
Drupal.media.mediaFormatSelected = function (e) {
var formatType = $("input[@name='format']:checked", this).val();
var formattedMedia = {type: formatType, html: Drupal.settings.media.formatFormFormats[formatType]};
$(this).parents('#dialog').trigger('mediaSelected', [formattedMedia]);
$(this).parents('#dialog').dialog('close');
return false;
}
})(jQuery);
Drupal.media = Drupal.media || {};
(function ($) {
Drupal.wysiwyg.plugins.media = {
......@@ -8,19 +10,34 @@
if (data.format == 'html') {
$().mediaBrowser( function (mediaFiles) {
var mediaFile = mediaFiles[0];
//@todo: turn this into a non-anonymous bind
Drupal.media.formatForm.launch(mediaFile, function(formattedMedia) {
Drupal.wysiwyg.plugins.media.insertMediaFile(mediaFile, formattedMedia, Drupal.wysiwyg.instances[instanceId]);
debug.debug(instanceId);
// This is totally not the indended solution to this. A very
// temporary hack to see if the dialog API is a good match
// and if so, I don't know how else to integrate it :(
element_settings = {};
element_settings.url = Drupal.settings.media.formatFormUrl.replace('-media-', mediaFile.fid);
element_settings.event = 'go';
var launcher = $('<div id ="wysiwyg-launcher"></div>');
Drupal.ajax['wysiwyg-launcher'] = new Drupal.ajax('wysiwyg-launcher', launcher, element_settings);
Drupal.ajax.prototype.commands.dialog_loading();
Drupal.dialog.bind('mediaSelected', function(event, formattedMedia) {
if (formattedMedia) {
debug.debug(formattedMedia);
Drupal.wysiwyg.plugins.media.insertMediaFile(mediaFile, formattedMedia.type, formattedMedia.html, Drupal.wysiwyg.instances[instanceId]);
}
});
launcher.trigger('go');
return;
});
}
},
insertMediaFile: function(mediaFile, formattedMedia, wysiwygInstance) {
insertMediaFile: function(mediaFile, viewMode, formattedMedia, wysiwygInstance) {
// Hack to allow for use of .html()
var embeddedMedia = $('<div>' + formattedMedia + '</div>');
// add the fid attribute to the image
$('img', embeddedMedia).attr('fid', mediaFile.fid);
$('img', embeddedMedia).attr('view_mode', viewMode);
tagContent = Drupal.wysiwyg.plugins.media.createTag(embeddedMedia);
// When tagmap is defined such as node/edit, block/configure
if(Drupal.settings.tagmap) {
......@@ -31,7 +48,7 @@
Drupal.settings.tagmap = { };
Drupal.settings.tagmap[tagContent] = Drupal.wysiwyg.plugins.media.addWrapper(embeddedMedia.html());
}
wysiwygInstance.insert(Drupal.wysiwyg.plugins.media.addWrapper(embeddedMedia.html()));
wysiwygInstance.insert(Drupal.wysiwyg.plugins.media.addWrapper(embeddedMedia.html()) + "&nbsp;");
},
/**
......@@ -94,45 +111,11 @@
tagContent = {
"type": 'media',
//@todo: This will be selected from the format form
"view_mode": 'media_original',
"view_mode": imgNode.attr('view_mode'),
"fid" : imgNode.attr('fid'),
"attributes": attribs
};
return '[[' + JSON.stringify(tagContent) + ']]';
}
};
Drupal.media = Drupal.media || {};
Drupal.media.formatForm = {
launch: function(mediaFile, callback) {
// Really, we should be doing what's happening below.
// For now, we're just retutning the preview
callback(mediaFile.preview);
return;
$('<div id="format_form"></div>')
.load(Drupal.settings.basePath + 'media/' + mediaFile.fid + '/format-form')
.dialog({
buttons: {
"Ok": function() {
callback(mediaFile.preview);
}
},
modal: true,
draggable: true,
resizable: true,
minWidth: 600,
width: 800,
height:500,
position: 'top',
overlay: {
backgroundColor: '#000000',
opacity: 0.4
}
});
}
}
})(jQuery);
......@@ -69,6 +69,6 @@
?>
<div id="media-browser-page-wrapper"><div id="media-browser-page">
<?php if ($title): ?><h1 class="title" id="page-title"><?php print $title; ?></h1><?php endif; ?>
<?php if (!empty($title)): ?><h1 class="title" id="page-title"><?php print $title; ?></h1><?php endif; ?>
<?php print render($page['content']); ?>
</div></div> <!-- /#page, /#page-wrapper -->
......@@ -114,61 +114,3 @@ function media_browser_list($conditions = array()) {
print drupal_json_encode($medias);
die();
}
/**
*
* @param unknown_type $form
* @param unknown_type $media
* @return unknown_type
*/
function media_browser_format_form($form, $form_state, $media) {
$form = array();
$options = array();
foreach (media_field_view_modes('media') as $key => $mode) {
//@TODO: Get the file formatter, and display that here too.
$options[$key] = $mode['label'];
$formats[$key] = field_view_field('media', $media, 'file', $key);
$formats[$key]['#prefix'] = '<div id="' . $key . '"/>';
$formats[$key]['#suffix'] = '</div>';
}
$form['preview'] = array(
'#type' => 'markup',
'#title' => basename($media->uri),
'#markup' => drupal_render(field_view_field('media', $media, 'file', 'media_preview'))
);
$form['formats'] = array(
'#type' => 'radios',
'#title' => 'Format',
'#options' => $options,
);
$form['formats_rendered'] = array(
'#prefix' => '<div id="formats-rendered" style="display:none">',
'#suffix' => '</div>',
);
$form['formats_rendered'] += $formats;
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'select',
'#ajax' => array(
'callback' => 'media_browser_format_select',
),
);
return $form;
}
function media_browser_format_select($form, &$form_state) {
dpr($form_state);
return "blah blah";
}
function media_browser_format($media) {
print drupal_render(drupal_get_form('media_browser_format_form', $media));
die();
}
<?php
// $Id$
/**
* @file
......@@ -167,19 +168,19 @@ function media_build_browser_form($form_state, $registration_ids, $node_type, $f
/*
$uri = $form_state['values']['media_files'];
$file_extension = pathinfo($uri, PATHINFO_EXTENSION);
// Get the file creator for this item.
$file_creator = media_get_registered_modules(array($registration_id));
// Get the formaters for this node type.
$formatters = media_active_fields_for_node_type($node_type, 'formatter');
// Get the registrations.
$registrations = media_get_registered_modules($formatters[$field]);
// Remove any non-applying registrations.
$registrations = media_get_applicable_formatters($registrations, $file_extension);
// Get all the formatting forms.
$formatter_options = array();
$forms = array();
......
<?php
/**
* @file
* Functions related to the WYSIWYG editor.
*/
/**
* Implementation of hook_wysiwyg_include_directory()
......@@ -71,8 +75,21 @@ function _media_markup($match) {
*/
function media_process_form(&$form, $form_state = array()) {
static $libraries_attached;
foreach(element_children($form) as $item) {
if(isset($form[$item]['#text_format'])) {
if (!$libraries_attached) {
$form['#attached']['library'][] = array('dialog', 'dialog');
$attached = TRUE;
$settings = array(
'media' => array(
'formatFormUrl' => url('media/-media-/format-form/nojs/'),
),
);
drupal_add_js($settings, 'setting');
$libraries_attached = TRUE;
}
if(isset($form[$item]['#default_value'])) {
$text = $form[$item]['#default_value'];
$tagmap = _media_generate_tagMap($text);
......@@ -88,9 +105,6 @@ function media_process_form(&$form, $form_state = array()) {
return $form;
}
function media_wysiwyg_attach_alter(&$element) {
}
/**
* Generates an array of [inline tags] => <html> to be used in filter
* replacement and to add the mapping to JS.
......@@ -125,3 +139,83 @@ function _media_generate_tagMap($text) {
}
return $tagmap;
}
/**
*
* @param unknown_type $form
* @param unknown_type $media
* @return unknown_type
*/
function media_format_form($form, $form_state, $media) {
$form = array();
$options = array();
foreach (media_field_view_modes('media') as $key => $mode) {
//@TODO: Get the file formatter, and display that here too.
$options[$key] = $mode['label'];
$formats[$key] = drupal_render(field_view_field('media', $media, 'file', $key));
}
// This totally doesn't work because the dialog API doesn't support it.
$settings = array();
$settings['media'] = array('formatFormFormats' => $formats);
drupal_add_js($settings, 'setting');
$path = drupal_get_path('module', 'media');
$form['#attached']['js'][] = $path . '/javascript/media-format-form.js';
drupal_add_js($path . '/javascript/media-format-form.js');
$form['preview'] = array(
'#type' => 'markup',
'#title' => basename($media->uri),
'#markup' => drupal_render(field_view_field('media', $media, 'file', 'media_preview'))
);
$form['format'] = array(
'#type' => 'radios',
'#title' => 'Format',
'#options' => $options,
'#default_value' => 'media_original',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'select',
// This currently does nothing, because we really don't need a form submit here.
// '#ajax' => array(
// 'callback' => 'media_format_select',
// ),
);
return $form;
}
function media_format_form_ajax($media, $js) {
if (!$media) {
drupal_set_message('Error getting format form, media not provided', 'error');
return;
}
if ($js) {
dialog_display(TRUE);
// Could be jQuery dialog options.
$options = array();
$content = drupal_get_form('media_format_form', $media);
$output[] = dialog_command_display($content, array('title' => 'Choose a format'), $options);
ajax_render($output);
}
else {
drupal_set_message('You must have Javascript enable to use this module');
drupal_goto('media/you/need/javascript');
}
}
/**
* Creates a Drupal AJAX command to open the modal with a loading animation.
*/
function media_command_format_select() {
return array(
'command' => 'media_format_select',
);
}
......@@ -16,3 +16,4 @@ files[] = test/media.types.test
dependencies[] = file
dependencies[] = image
dependencies[] = file_styles
dependencies[] = dialog_api
......@@ -138,6 +138,17 @@ function media_menu() {
'file' => 'media.browser.inc',
);
$items['media/%media/format-form/%dialog_js'] = array(
'title' => 'Format form',
'description' => 'Used as an AJAX callback',
'page callback' => 'media_format_form_ajax',
'page arguments' => array(1, 3),
'access arguments' => array('administer media'),
'weight' => 0,
'type' => MENU_CALLBACK,
'file' => 'media.filter.inc',
);
$items['media'] = array(
'title' => 'Media',
'page callback' => 'media_page_default',
......@@ -173,17 +184,6 @@ function media_menu() {
'file' => 'media.pages.inc',
);
$items['media/%media/format-form'] = array(
'title' => 'Format form',
'description' => 'Used as an AJAX callback',
'page callback' => 'media_browser_format',
'page arguments' => array(1),
'access arguments' => array('administer media'),
'weight' => 0,
'type' => MENU_CALLBACK,
'file' => 'media.admin.inc',
);
return $items;
}
......@@ -499,6 +499,20 @@ if (!function_exists('file_uri_to_object')) {
}
}
/**
* Implement hook_ajax_render_ALTER
*/
function media_ajax_render_alter(&$commands) {
// This is probably a hack
// But having a lot of trouble passing settings
// on the format-form (see media.filter.inc.
foreach ($commands as &$command) {
if ($command['command'] == 'settings') {
$command['merge'] = TRUE;
}
}
}
/**
* Returns the files attached to administration pages.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment