Permalink
Browse files

Merge pull request #1105 from benjaminpick/media_preview

Show Media preview
  • Loading branch information...
2 parents d8e0299 + 4643c4b commit 5fe4bd6ea4ee9cfd9850515e5376fcdf63340469 @LouisLandry LouisLandry committed Apr 19, 2012
Showing with 90 additions and 1 deletion.
  1. +90 −1 libraries/legacy/form/field/media.php
@@ -67,12 +67,38 @@ protected function getInput()
$script[] = ' var elem = document.id(id);';
$script[] = ' elem.value = value;';
$script[] = ' elem.fireEvent("change");';
- $script[] = ' if (typeof(elem.onchange) === \'function\') {';
+ $script[] = ' if (typeof(elem.onchange) === "function") {';
$script[] = ' elem.onchange();';
$script[] = ' }';
+ $script[] = ' jMediaRefreshPreview(id);';
$script[] = ' }';
$script[] = ' }';
+ $script[] = ' function jMediaRefreshPreview(id) {';
+ $script[] = ' var value = document.id(id).value;';
+ $script[] = ' var img = document.id(id + "_preview");';
+ $script[] = ' if (img) {';
+ $script[] = ' if (value) {';
+ $script[] = ' img.src = "' . JURI::root() . '" + value;';
+ $script[] = ' document.id(id + "_preview_empty").setStyle("display", "none");';
+ $script[] = ' document.id(id + "_preview_img").setStyle("display", "");';
+ $script[] = ' } else { ';
+ $script[] = ' img.src = ""';
+ $script[] = ' document.id(id + "_preview_empty").setStyle("display", "");';
+ $script[] = ' document.id(id + "_preview_img").setStyle("display", "none");';
+ $script[] = ' } ';
+ $script[] = ' } ';
+ $script[] = ' }';
+
+ $script[] = ' function jMediaRefreshPreviewTip(tip)';
+ $script[] = ' {';
+ $script[] = ' tip.setStyle("display", "block");';
+ $script[] = ' var img = tip.getElement("img.media-preview");';
+ $script[] = ' var id = img.getProperty("id");';
+ $script[] = ' id = id.substring(0, id.length - "_preview".length);';
+ $script[] = ' jMediaRefreshPreview(id);';
+ $script[] = ' }';
+
// Add the script to the document head.
JFactory::getDocument()->addScriptDeclaration(implode("\n", $script));
@@ -138,6 +164,69 @@ protected function getInput()
$html[] = '</div>';
}
+ // The Preview.
+ $preview = (string) $this->element['preview'];
+ $showPreview = true;
+ $showAsTooltip = false;
+ switch ($preview)
+ {
+ case 'no':
+ case 'none':
+ $showPreview = false;
+ break;
+ case 'yes':
+ case 'show':
+ break;
+ case 'tooltip':
+ default:
+ $showAsTooltip = true;
+ $options = array(
+ 'onShow' => 'jMediaRefreshPreviewTip',
+ );
+ JHtml::_('behavior.tooltip', '.hasTipPreview', $options);
+ break;
+ }
+
+ if ($showPreview)
+ {
+ if ($this->value && file_exists(JPATH_ROOT . '/' . $this->value))
+ {
+ $src = JURI::root() . $this->value;
+ }
+ else
+ {
+ $src = '';
+ }
+
+ $attr = array(
+ 'id' => $this->id . '_preview',
+ 'class' => 'media-preview',
+ 'style' => 'max-width:160px; max-height:100px;'
+ );
+ $img = JHtml::image($src, JText::_('JLIB_FORM_MEDIA_PREVIEW_ALT'), $attr);
+ $previewImg = '<div id="' . $this->id . '_preview_img"' . ($src ? '' : ' style="display:none"') . '>' . $img . '</div>';
+ $previewImgEmpty = '<div id="' . $this->id . '_preview_empty"' . ($src ? ' style="display:none"' : '') . '>'
+ . JText::_('JLIB_FORM_MEDIA_PREVIEW_EMPTY') . '</div>';
+
+ $html[] = '<div class="media-preview fltlft">';
+ if ($showAsTooltip)
+ {
+ $tooltip = $previewImgEmpty . $previewImg;
+ $options = array(
+ 'title' => JText::_('JLIB_FORM_MEDIA_PREVIEW_SELECTED_IMAGE'),
+ 'text' => JText::_('JLIB_FORM_MEDIA_PREVIEW_TIP_TITLE'),
+ 'class' => 'hasTipPreview'
+ );
+ $html[] = JHtml::tooltip($tooltip, $options);
+ }
+ else
+ {
+ $html[] = ' ' . $previewImgEmpty;
+ $html[] = ' ' . $previewImg;
+ }
+ $html[] = '</div>';
+ }
+
return implode("\n", $html);
}
}

0 comments on commit 5fe4bd6

Please sign in to comment.