joomla 给 HTMLEditor 提供了一个很好的功能,选择图像,并插入到 HTMLEditor 里。
我也想用这个功能,但不是插到编辑器里,而是要得到选择的图片的地址。但是 joomla 并不支持这样做。
咋办呢?自己动手写个 又太麻烦。
用 Firebug 可以看到 joomla 提供的是这样的:
<a rel="{handler: 'iframe', size: {x: 570, y: 400}}" href="/component/media/?view=images&tmpl=component&e_name=text" title="插入图片" class="modal-button">插入图片</a>
看这个 href:
index.php?option=com_media&view=images&tmpl=component&e_name=text
没有 layout 参数,默认的 layout 就是 default, 对应的就是:
administrator\components\com_media\views\images\tmpl\default.php
看了一下这个 component 的 controller 文件,没有强制 layout 参数,这真是太好了,把这个 default.php 复制为 customselect.php
并在它开头的那个 <script>加点东西:
String.prototype.trim = function () { return this.replace(/(^\s*)|(\s*$)/g, ""); }
String.prototype.queryString = function (key) {
var sValue = this.match(new RegExp("[\?\&]" + key + "=([^\&]*)(\&?)", "i"));
return sValue ? sValue[1] : sValue
}
function onok(){
var fun = location.href.queryString("fun").trim();
var url = $("f_url").getValue();
if(parent.window[fun] instanceof parent.Function && url.trim() != ''){
parent[fun]( "/" + url );
}
}
找到这个文件的HTML里的:ImageManager.onok() 改为 onok() ,就是调用上面这个刚写的函数。
调用就简单了:
<a class="modal-button" title="选择图片" href="/component/media/?view=images&tmpl=component&layout=customselect&fun=getUrl&e_name=text" rel="{handler: 'iframe', size: {x: 570, y: 400}}">选择图片</a>
<script>
function getUrl( url ){
$("synopsisPic").value = url;
}
</script>
看这个 href:
index.php?option=com_media&view=images&tmpl=component&layout=customselect&fun=getUrl&e_name=text
注意,我多加了两个参数到这个 href 里,一个是 layout=customselect, 加上它,就可以打开刚 customselect.php 了。
另外一个是 fun=getUrl
基本思路是:iframe 里的页面(customselect.php) 从当前地址里取得 fun,并判断 parent 存在不存在这个 fun 所指的函数,如果存在,就调用。
这里需要指出一下:
if(parent.window[fun] instanceof parent.Function && url.trim() != ''){
parent[fun]( "/" + url );
}
这个 fun 其实是个字符串,是写在 iframe 的 src 里的。
window["document"] 其实就是 window.document
parent.window[fun] 你该明白是什么意思了吧,就是 parent.window.getUrl 这个函数。
另外 Function 其实也是 window 下面的一个对象。
一开始我这样写:
parent.window[fun] instanceof Function
结果是 false,这让郁闷了一小会,后来一想, parent.window.getUrl 是 parent.window.Function 的一个实例,parent.window 不等于 window ,所以 parent.window.Function 和 Function 跟本就不相等。
我也想用这个功能,但不是插到编辑器里,而是要得到选择的图片的地址。但是 joomla 并不支持这样做。
咋办呢?自己动手写个 又太麻烦。
用 Firebug 可以看到 joomla 提供的是这样的:
<a rel="{handler: 'iframe', size: {x: 570, y: 400}}" href="/component/media/?view=images&tmpl=component&e_name=text" title="插入图片" class="modal-button">插入图片</a>
看这个 href:
index.php?option=com_media&view=images&tmpl=component&e_name=text
没有 layout 参数,默认的 layout 就是 default, 对应的就是:
administrator\components\com_media\views\images\tmpl\default.php
看了一下这个 component 的 controller 文件,没有强制 layout 参数,这真是太好了,把这个 default.php 复制为 customselect.php
并在它开头的那个 <script>加点东西:
String.prototype.trim = function () { return this.replace(/(^\s*)|(\s*$)/g, ""); }
String.prototype.queryString = function (key) {
var sValue = this.match(new RegExp("[\?\&]" + key + "=([^\&]*)(\&?)", "i"));
return sValue ? sValue[1] : sValue
}
function onok(){
var fun = location.href.queryString("fun").trim();
var url = $("f_url").getValue();
if(parent.window[fun] instanceof parent.Function && url.trim() != ''){
parent[fun]( "/" + url );
}
}
找到这个文件的HTML里的:ImageManager.onok() 改为 onok() ,就是调用上面这个刚写的函数。
调用就简单了:
<a class="modal-button" title="选择图片" href="/component/media/?view=images&tmpl=component&layout=customselect&fun=getUrl&e_name=text" rel="{handler: 'iframe', size: {x: 570, y: 400}}">选择图片</a>
<script>
function getUrl( url ){
$("synopsisPic").value = url;
}
</script>
看这个 href:
index.php?option=com_media&view=images&tmpl=component&layout=customselect&fun=getUrl&e_name=text
注意,我多加了两个参数到这个 href 里,一个是 layout=customselect, 加上它,就可以打开刚 customselect.php 了。
另外一个是 fun=getUrl
基本思路是:iframe 里的页面(customselect.php) 从当前地址里取得 fun,并判断 parent 存在不存在这个 fun 所指的函数,如果存在,就调用。
这里需要指出一下:
if(parent.window[fun] instanceof parent.Function && url.trim() != ''){
parent[fun]( "/" + url );
}
这个 fun 其实是个字符串,是写在 iframe 的 src 里的。
window["document"] 其实就是 window.document
parent.window[fun] 你该明白是什么意思了吧,就是 parent.window.getUrl 这个函数。
另外 Function 其实也是 window 下面的一个对象。
一开始我这样写:
parent.window[fun] instanceof Function
结果是 false,这让郁闷了一小会,后来一想, parent.window.getUrl 是 parent.window.Function 的一个实例,parent.window 不等于 window ,所以 parent.window.Function 和 Function 跟本就不相等。
| Next > |
|---|
Last Updated ( Friday, 16 April 2010 23:53 )



