On Sun, 18 Apr 2010 02:19:34 +0300, Hannes Papenberg
<
hack...@googlemail.com> wrote:
> Hi Elin,
> hi Artyom,
> I know that we are talking about the formfield, but since com_media
> currently practically only supports images, the question is solved
> easily for me. Create one named media, that we can extend in 1.7 with
> options to show images, documents or similar stuff. This one for now
> wont do more than the image field, that also uses com_media and shows
> just the images to be selected in the modal.
Ok, Hannes, I will rename my "modalExternalImage" field to "media" and put
the classes to the bugtracker.
> A bigger issue is, that com_media currently always creates a complete
> image tag and not just returns the imagepath.
To solve such problems each JFormFieldModal's subclass has a partner
javascript class. End a js-class has preInsType and postInsType methods.
This methods executed on each value insertion. When we define a
PHP-subclass of JFormFieldModal we can easily extend or add new js-methods
just using getJsMethodXXX() php-methods:
= = =
class JFormFieldMedia extends JFormFieldModalExternal
{
protected $link =
'index.php?option=com_media&view=images&tmpl=component&e_name={FIELD_ID}';
static protected function getJsMethodPreInsType()
{
$script = <<<EOF
imgTag = this.value;
re = /src=['"]([^'"]+)['"]/;
re.test(imgTag);
this.value = RegExp.$1;
EOF;
return $script;
}
= = =
Above definition will lead to creation of this js-class:
= = =
var mediaField = new Class({
Extends: modalExternalField,
preInsType: function() {
imgTag = this.value;
re = /src=['"]([^'"]+)['"]/;
re.test(imgTag);
this.value = RegExp.$1;
}
});
// And then a js-object will be created for each form field on the client
side.
...
= = =
Here imageField::preInsType() javascript method makes image paths from
image tags.
One problem is solved. But com_media knows nothing about our javascript
field objects.
Don't worry, solution exists.
> We will have to change it
> to call a function that each button/formfield can define on its own
> instead of hardcoding that into the media manager itself.
When we will rewrite com_media then it simply can use a reference for the
active field object which JFormFieldModal creates for us.
Also we can make a new parameter for com_media, say, "function" and pass
to it a string with js-object name and js-method name. With this approach
we also can pass ordinary function names to com_media.
Now I resolved this problem with javascript proxy functions:
We should define a PHP-class with "getJsProxyFunc()" method:
= = =
class JFormFieldMedia extends JFormFieldModalExternal
{
protected $link =
'index.php?option=com_media&view=images&tmpl=component&e_name={FIELD_ID}';
static protected function getJsProxyFunc()
{
$name = 'jInsertEditorText';
$args = array('value', 'id');
$fields = self::$jsFieldStorage;
$insert = self::$jsInsMethod;
$body = <<<EOF
// Proxy function for the image field objects.
// In the feature, com_media should be rewriten to support field
objects.
{$fields}[id].$insert(value);
EOF;
return array($name, $body, $args);
}
}
= = =
Above definition will lead to creation of this js-code:
= = =
var mediaField = new Class({
Extends: modalExternalField
});
function jInsertEditorText(value, id) {
// Proxy function for the image field objects.
// In the feature, com_media should be rewriten to support field objects.
formFields[id].insert(value);
}
= = =
The proxy function should find needed javascript field object and execute
it's "insert" method with needed arguments.