XForms/Outline Editor

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Motivation[edit | edit source]

Sometimes you want to allow a user to be able to add properties to a list that change the appearance of the item in the list. In this example, the level of indentation is controlled by an attribute in a list called the "level" which is the indentation level in an outline view.

Note: This program was based on an example Kurt Cagle posted on the Mozilla XForms developer news group. This example posted with his permission.

Screen Image[edit | edit source]

Outline editor

Link to working Program[edit | edit source]

Tree Editor

Sample Program[edit | edit source]

Here is a sample outline-editor program. If you want to test this, be sure to use the edit view to copy the text. This file includes several tab characters that do not display correctly in the view mode.

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/2007/xhtml"
       <!-- based on example posted by Kurt Cagle on Mozilla XForms Newsgroup in Feb. 2007 -->
       <style type="text/css">
       #itemEntry .xf-value {
       #content {
          border:inset 2px blue;
          background: silver;
      <xf:model id="data_model">
          <xf:instance id="document_data" src="item-tree.xml" xmlns=""/>
          <xf:bind nodeset="//delete-trigger-enabled" relevant="count(//item) &gt; 1" />
          <xf:action ev:event="xforms-ready">
              <xf:setfocus control="itemEntry" />
          <xf:action ev:event="addItemEvent">
              <xf:insert nodeset="//data//item[position() =
                  index('list')]" at="index('list')" position="after" />
              <xf:setvalue ref="//data//item[position() =
                  index('list')]" value="" />
               <xf:setvalue ref="//data//item[position() =
                  index('list')]/@level" value="//data//item[position() =
                  index('list') - 1]/@level" />
                <xf:setfocus control="itemEntry" />
          <xf:action ev:event="removeItemEvent">
              <xf:delete nodeset="//data//item[position() =
                  index('list')]" at="index('list')" />
               <xf:setindex repeat="list" index="index('list')-1" />
               <xf:setfocus control="itemEntry" />
             <xf:action ev:event="promoteItemEvent">
                  <xf:setvalue ref="//data//item[position() =
                  index('list')]/@level" value="if(number(.) - 1 &lt; 1,1, number(.) - 1)" />
                  <xf:setfocus control="itemEntry" />
             <xf:action ev:event="demoteItemEvent">
                  <xf:setvalue ref="//data//item[position() =
                     index('list')]/@level" value="if(index('list') != 1,
                     if (number(.) &gt; //data//item[position() =
                         index('list')-1]/@level,(//data//item[position() =
                         index('list')-1]/@level) + 1,number(.) + 1),1)" />
                  <xf:setfocus control="itemEntry" />
             <xf:submission id="save" method="put" action="item-tree.xml" instance="document_data"/>
       <xf:group ref="/container/data">
               <xf:trigger id="addItem">
                      <xf:action ev:event="DOMActivate">
                           <xf:dispatch name="addItemEvent" target="data_model" />
               <xf:trigger id="removeItem">
                      <xf:action ev:event="DOMActivate">
                           <xf:dispatch name="removeItemEvent" target="data_model" />
               <xf:trigger id="promote">
                      <xf:label>Promote &lt;</xf:label>
                      <xf:action ev:event="DOMActivate">
                           <xf:dispatch name="promoteItemEvent" target="data_model" />
               <xf:trigger id="demote">
                      <xf:label>Demote &gt;</xf:label>
                      <xf:action ev:event="DOMActivate">
                           <xf:dispatch name="demoteItemEvent" target="data_model" />
               <div id="content">
                  <xf:repeat nodeset="items/item" id="list">
                         <xf:input ref="." id="itemEntry" incremental="true">
                                <xf:output value="concat(substring('&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;',
1, 3*(number(@level) - 1)),'* ')" />
                              <xf:action ev:event="DOMActivate">
                                   <xf:dispatch name="addItemEvent" target="data_model" />
        <xf:submit submission="save">

Sample Instance File "item-tree.xml"[edit | edit source]

<?xml version="1.0" encoding="UTF-8"?>
         <item level="1">none</item>
         <item level="2">Fiction</item>
         <item level="3">Fantasy</item>
         <item level="3">Mystery</item>
         <item level="3">Religion</item>
         <item level="3">Romance</item>
         <item level="3">Science Fiction</item>
         <item level="2">Non-Fiction</item>
         <item level="3">Cooking</item>
         <item level="3">History</item>
         <item level="3">Biographies</item>
         <item level="3">Computers</item>
         <item level="4">Software</item>
         <item level="5">XForms Tutorial and Cookbook</item>
         <item level="3">Internet</item>
         <item level="3">Nature</item>
         <item level="3">Science</item>
         <item level="1">Electronics</item>
         <item level="1">Music</item>

Discussion[edit | edit source]

The structure of this program uses custom events. Note that in the model there are a series of actions, each with their own event name:

<xf: model id="data_model">
   <xf:action ev:event="addItemEvent">

These events are called directly inside of a trigger like this:

   <xf:dispatch name="addItemEvent" target="data_model" />

Note that the target attribute is the name of the model that the events are located.

Although actions can be tucked directly into a trigger, sometimes action are more logically grouped with instance data and should be placed directly in the model. This also allows other events need to reuse these actions, so it is a good ideal to put reuseable actions directly in a model

References[edit | edit source]

A version of this program was initially posted by Kurt Cagle on the Mozilla XForms newsgroup.

Next Page: Synonym Set Editor | Previous Page: Hierarchical Bookmarks
Home: XForms