组件运行时效果
组件有两种实现方式:xslt和java方式
一、Xslt方式
1、 建立组件目录UI/x5demo/components/resizeTextarea在我们的应用下建立components目录,专门用来存放自己开发的组件,在components目录下建立组件目录,用来存放组件的相关文件。 2、 建立组件描述文件UI/x5demo/components/resizeTextarea.xbl.xml 这个文件是必须有的,它描述了组件的摸样,定义了组件的方法。下面是一个空的组件描述文件。
- <?xml version="1.0" encoding="UTF-8"?>
- <xbl:xbl xmlns="http://www.w3.org/ns/xbl"
- xmlns:xbl="http://www.w3.org/ns/xbl"
- xmlns:xhtml="http://www.w3.org/1999/xhtml"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="2.0"
- xmlns:xs="http://www.w3.org/2001/XMLSchema"
- xmlns:xforms="http://www.justep.com/xforms"
- xmlns:ev="http://www.w3.org/2001/xml-events"
- script-type="text/javascript">
- <xbl:binding id="resizeTextarea">
- <xbl:template template-type="xslt">
- <xsl:template match="/*" xmlns:xforms="http://www.justep.com/xforms">
- <root>
- </root>
- </xsl:template>
- </xbl:template>
- <xbl:implementation>
- </xbl:implementation>
- </xbl:binding>
- </xbl:xbl>
复制代码完整代码如下 - <?xml version="1.0" encoding="UTF-8"?>
- <xbl:xbl xmlns="http://www.w3.org/ns/xbl"
- xmlns:xbl="http://www.w3.org/ns/xbl"
- xmlns:xhtml="http://www.w3.org/1999/xhtml"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="2.0"
- xmlns:xs="http://www.w3.org/2001/XMLSchema"
- xmlns:xforms="http://www.justep.com/xforms"
- xmlns:ev="http://www.w3.org/2001/xml-events"
- script-type="text/javascript">
- <!--增加js文件的引用-->
- <xbl:script type="text/javascript"
- src="/UI/x5demo/components/resizeTextarea/resizeTextarea.js" />
-
- <xbl:binding id="resizeTextarea">
- <xbl:template template-type="xslt">
- <xsl:template match="/*" xmlns:xforms="http://www.justep.com/xforms">
- <!--定义变量-->
- <xsl:variable name="id" select="if(@id) then @id else generate-global-id()" />
- <xsl:variable name="textarea-id" select="concat('_textarea_',$id)" />
- <root>
- <!--加html标签或X5平台组件,放了一个文本框和两个按钮,实现组件在浏览器中的运行效果-->
- <xhtml:span xblid="attribute"
- xbl-obj-id="{$id}"
- textarea-id="{$textarea-id}"
- style="display:none"/>
- <xforms:textarea id="{$textarea-id}" style="float:left">
- <xsl:if test="@ref!=''">
- <xsl:attribute name="ref" select="@ref"/>
- </xsl:if>
- </xforms:textarea>
- <xforms:trigger id="tr1" style="float:top">
- <xforms:label>变高</xforms:label>
- <xforms:action ev:event="DOMActivate">
- <xforms:script>
- <![CDATA[justep.XBLObject.getXBLObject(event.target).higher();]]>
- </xforms:script>
- </xforms:action>
- </xforms:trigger>
- <xforms:trigger id="tr2">
- <xforms:label>变矮</xforms:label>
- <xforms:action ev:event="DOMActivate">
- <xforms:script>
- <![CDATA[justep.XBLObject.getXBLObject(event.target).lower();]]>
- </xforms:script>
- </xforms:action>
- </xforms:trigger>
- </root>
- </xsl:template>
- </xbl:template>
-
- <xbl:implementation>
- <!—-定义组件的方法,其中initXBL是默认方法,系统初始化组件时会自动调用-->
- <![CDATA[
- ({
- "initXBL" : function() {
- this.__attribute_node = this.getElementByXblID('attribute');
- justep.Object.extend(this, new justep.resizeTextarea(this));
- },
- "__getAttributeValue" : function(name){
- return this.__attribute_node?this.__attribute_node.getAttribute(name):'';
- }
- })
- ]]></xbl:implementation>
- </xbl:binding>
- </xbl:xbl>
复制代码
3、 建立组件js文件UI/x5demo/components/resizeTextarea/resizeTextarea.js 在resizeTextarea.xbl.xml中引用,实现resizeTextarea.xbl.xml中调用的3个函数。其中包括了调用组件事件。完整代码如下
- justep.resizeTextarea = function(xblObject) {
- this._create(xblObject);
- };
- justep.resizeTextarea.prototype = {
- _create : function(xblObject){
- if(!xblObject) return;
- this.textareaId = xblObject.__getAttributeValue('textarea-id');
-
- this.onAfterChange = xblObject.domNode.getAttribute('onAfterChange');
- if (this.onAfterChange)
- this.onAfterChange = justep.Function.get(this.onAfterChange);
- else
- this.onAfterChange = null;
- },
- higher : function() {
- var m = this.textareaId;
- var h = parseFloat(justep.xbl(m).input.style.height) + 50;
- justep.xbl(m).input.style.height = h + "px";
- var ret = true;
- if (this.onAfterChange)
- ret = this.onAfterChange({
- 'source' : this
- });
- },
- lower : function() {
- var m = this.textareaId;
- var h = parseFloat(justep.xbl(m).input.style.height) - 50;
- $("#"+m)[0].style.height = h + "px";
- justep.xbl(m).input.style.height = h + "px";
- }
- };
复制代码
4、 说明
在resizeTextarea.xbl.xml中组件的属性,可通过@属性名直接访问,例如@ref |