Author: rgardler
Date: Wed Mar 16 22:57:43 2011
New Revision: 1082336

URL: http://svn.apache.org/viewvc?rev=1082336&view=rev
Log:
A template to dynamic settings handling. Simply define your settings in the 
SettingsService.getGroups and SettingsService.getSettings methods and this will 
auto construct your settings forms for you.

Added:
    incubator/wookie/trunk/widgets/widget-template/settings/
    incubator/wookie/trunk/widgets/widget-template/settings/build.xml
    incubator/wookie/trunk/widgets/widget-template/settings/config.xml
    incubator/wookie/trunk/widgets/widget-template/settings/images/
    incubator/wookie/trunk/widgets/widget-template/settings/images/icon.png   
(with props)
    
incubator/wookie/trunk/widgets/widget-template/settings/images/icon_16x16.png   
(with props)
    
incubator/wookie/trunk/widgets/widget-template/settings/images/icon_32x32.png   
(with props)
    incubator/wookie/trunk/widgets/widget-template/settings/index.html
    incubator/wookie/trunk/widgets/widget-template/settings/legal/
    incubator/wookie/trunk/widgets/widget-template/settings/lib/
    incubator/wookie/trunk/widgets/widget-template/settings/scripts/
    
incubator/wookie/trunk/widgets/widget-template/settings/scripts/controller.js
    incubator/wookie/trunk/widgets/widget-template/settings/scripts/settings.js
    incubator/wookie/trunk/widgets/widget-template/settings/style/
    incubator/wookie/trunk/widgets/widget-template/settings/style/screen.css

Added: incubator/wookie/trunk/widgets/widget-template/settings/build.xml
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/widget-template/settings/build.xml?rev=1082336&view=auto
==============================================================================
--- incubator/wookie/trunk/widgets/widget-template/settings/build.xml (added)
+++ incubator/wookie/trunk/widgets/widget-template/settings/build.xml Wed Mar 
16 22:57:43 2011
@@ -0,0 +1,23 @@
+<?xml version="1.0"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<project default="build-widget" basedir="." name="widget build file">
+       <property name="wookie.widgets.dir" location="../"/>
+       <property name="widget.shortname" value="@widget.shortname@"/>
+       
+       <import file="../build.xml"/>
+</project>
\ No newline at end of file

Added: incubator/wookie/trunk/widgets/widget-template/settings/config.xml
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/widget-template/settings/config.xml?rev=1082336&view=auto
==============================================================================
--- incubator/wookie/trunk/widgets/widget-template/settings/config.xml (added)
+++ incubator/wookie/trunk/widgets/widget-template/settings/config.xml Wed Mar 
16 22:57:43 2011
@@ -0,0 +1,46 @@
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<widget xmlns="http://www.w3.org/ns/widgets";
+               id="http://wookie.apache.org/widgets/@widget.shortname@";
+               version="0.1"
+        width="@widget.width@"
+        height="@widget.height@"
+        >
+  <name>@widget.shortname@</name>
+  <description>@widget.description@</description>
+  <content src="index.html"/>
+  <icon src="images/icon.png"/>
+  <author>Apache Wookie (Incubating) Team</author>
+  <licence>Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.</licence>
+
+</widget>
+
+
+

Added: incubator/wookie/trunk/widgets/widget-template/settings/images/icon.png
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/widget-template/settings/images/icon.png?rev=1082336&view=auto
==============================================================================
Binary file - no diff available.

Propchange: 
incubator/wookie/trunk/widgets/widget-template/settings/images/icon.png
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: 
incubator/wookie/trunk/widgets/widget-template/settings/images/icon_16x16.png
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/widget-template/settings/images/icon_16x16.png?rev=1082336&view=auto
==============================================================================
Binary file - no diff available.

Propchange: 
incubator/wookie/trunk/widgets/widget-template/settings/images/icon_16x16.png
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: 
incubator/wookie/trunk/widgets/widget-template/settings/images/icon_32x32.png
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/widget-template/settings/images/icon_32x32.png?rev=1082336&view=auto
==============================================================================
Binary file - no diff available.

Propchange: 
incubator/wookie/trunk/widgets/widget-template/settings/images/icon_32x32.png
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/wookie/trunk/widgets/widget-template/settings/index.html
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/widget-template/settings/index.html?rev=1082336&view=auto
==============================================================================
--- incubator/wookie/trunk/widgets/widget-template/settings/index.html (added)
+++ incubator/wookie/trunk/widgets/widget-template/settings/index.html Wed Mar 
16 22:57:43 2011
@@ -0,0 +1,84 @@
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
+<html xmlns="http://www.w3.org/1999/xhtml";>
+       <head>
+        <meta http-equiv="pragma" content="no-cache"/>
+        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+        <link rel="stylesheet" type="text/css" href="style/screen.css" />
+        <script type="text/javascript" src="scripts/controller.js" 
charset="utf-8"></script>  
+        <script type="text/javascript" src="scripts/settings.js" 
charset="utf-8"></script>  
+               <title>@widget.shortname@</title>
+       </head>
+       <body>
+         <div id="widget">
+               <div id="toolbar">
+                 <img id="widget-icon"
+                      src="images/icon_16x16.png" 
+                      onclick="Controller.showSettings()" 
+                      alt="@widget.shortname@ Icon"/>
+                      @widget.shortname@
+               </div>
+               
+               <div id="content">
+                 <h2>@widget.shortname@</h2>
+                 <p>@widget.description@</p>
+                
+                 <div class="docs">
+                         <p>This widget was built using the "settings" 
template from Apache Wookie (Incubating).</p>
+                        
+                         <p>It demonstrates settings handling in a widget using
+                            <a 
href="http://diveintohtml5.org/forms.html";>HTML5 forms</a>.
+                            The settings form is auto generated in the 
+                            settingsService.getsettingDefinitions(group) 
method.
+                            To create your own settings simply edit this 
method.
+                            If your widget needs a large number of different 
settings you can
+                            group settings together. Simply pass in the 
required group parameter to
+                            retrieve the desired group of settings.</p>
+                            
+                          <p>Groups can be used to describe related sets of 
settings. When the
+                         settings page is requested the user will be presented 
with a
+                         list of links to individual settings pages for each 
group. If
+                         no groups are defined then all settings will be 
displayed on a 
+                         single settings page. Groups are defined in the 
+                         <code>SettingsService.getGroups()</code> method.</p>
+                          
+                          <p>To view the settings page click the widget icon 
+                            in the top left of the header, or call the 
+                            <a 
href="javascript:Controller.showSettings()">Controller.showSettings()</a>
+                            method. If you are using groups and you want to 
create a link directly to 
+                            a specific group of settings pass the group name 
as a parameter to the 
+                            <a 
href="javascript:Controller.showSettings('contact')">Controller.showSettings("contact")</a>
+                            method.</p>
+                    </div>
+               </div>
+               
+               <div id="settings">
+             <p>This paragraph will be replaced by the settings form which is 
created
+                dynamically by SettingsService.createForm(group)</p>
+           </div>
+           
+               <div id="footer">Powered by Apache Wookie (Incubating)</div>
+         </div>
+         
+         
+       </body>
+<!-- fix forIE cashing - must go here even though its in the wrong place
+<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
+</HEAD>
+-->
+</html>
\ No newline at end of file

Added: 
incubator/wookie/trunk/widgets/widget-template/settings/scripts/controller.js
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/widget-template/settings/scripts/controller.js?rev=1082336&view=auto
==============================================================================
--- 
incubator/wookie/trunk/widgets/widget-template/settings/scripts/controller.js 
(added)
+++ 
incubator/wookie/trunk/widgets/widget-template/settings/scripts/controller.js 
Wed Mar 16 22:57:43 2011
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+/**
+ * The Controller manages user interactions and widget responses.
+ */
+var Controller = {
+       
+       /**
+        * Build and show the settings page.
+        */
+       showSettings: function(group) {
+               var elemContent = document.getElementById("content");
+               var elemSettings = document.getElementById("settings");
+               SettingsService.createForm(group);
+               elemContent.style.display="none";
+               elemSettings.style.display="block";
+       },
+
+       /**
+        * Show the main page of the widget.
+        */
+       showMain: function() {
+               var elemContent = document.getElementById("content");
+               var elemSettings = document.getElementById("settings");
+               elemContent.style.display="block";
+               elemSettings.style.display="none";      
+       },
+
+}
\ No newline at end of file

Added: 
incubator/wookie/trunk/widgets/widget-template/settings/scripts/settings.js
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/widget-template/settings/scripts/settings.js?rev=1082336&view=auto
==============================================================================
--- incubator/wookie/trunk/widgets/widget-template/settings/scripts/settings.js 
(added)
+++ incubator/wookie/trunk/widgets/widget-template/settings/scripts/settings.js 
Wed Mar 16 22:57:43 2011
@@ -0,0 +1,183 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * A setting to represent in the settings form.
+ * 
+ * @param id is used to identiy the setting and the form element created for 
it.
+ * @param label is the text to place in the label for the form element.
+ * @param placeholder is the text that will appear in the form element if the 
setting has not been set
+ * @param type is the type of data to be stored in this setting, it is used to 
define the form element to use
+ */
+function Setting(id, label, placeholder, type){
+       this.id = id;
+    this.label = label;
+    this.placeholder = placeholder
+    this.type = type;
+}
+
+/**
+ * The SettingService is where we retrieve and store details about the 
Settings in
+ * the widget.
+ */
+var SettingsService = {
+       /**
+        * Get the names of the available settings groups.
+        * Groups are used to describe related sets of settings. When the
+        * settings page is requested the user will be presented with a
+        * list of links to individual settings pages for each group. If
+        * no groups are defined then all settings will be displayed on a 
+        * single settings page.
+        * 
+        * @return an array setting group names or an empty array if no groups 
are defined.
+        */
+       getGroups: function() {
+               var groups = [];
+               groups[0] = "about";
+               groups[1] = "contact";
+               return groups;
+    },
+           
+       /**
+        * Get the settings in a given group. Note that the group names here 
must correspond
+        * to the group names returned by SettingsService.getSettings()
+        * 
+        * @return an array containing a number of Setting objects
+        */
+       getSettings: function(group) {
+               var settings = [];
+               if (group == undefined || group == "about") {
+                       settings[0] = new Setting("name", "Name", "The name you 
wish to be known by.", "text");
+                       settings[1] = new Setting("description", "Description", 
"A short description that will be shown to people viewing your details.", 
"textarea");
+               } else if (group == "contact") {
+                       settings[0] = new Setting("homepageURL", "Homepage 
URL", "Your homepage URL.", "email");
+                       settings[1] = new Setting("email", "EMail", "Your email 
address.", "email");
+               }
+               return settings;
+    },
+    
+       /**
+        * Process a settings form that has been submitted by storing all the 
settings
+        * in widget preferences. If settings groups are being used then we 
only process
+        * settings in that group.
+        * 
+        * @param group the name of the group to process if groups are being 
used
+        */
+       submitSettingsForm: function(group) {
+         var settings = SettingsService.getSettings(group);
+         var value;
+         for (key in settings) {
+           value = document.getElementById(settings[key].id).value;
+               Widget.preferences.setItem(settings[key].id, value);
+      };
+      Controller.showMain();
+       },
+       
+       /**
+        * Create the settings form and populate <div id="settings"> with it.
+        * 
+        * Groups are used to describe related sets of settings. When the
+        * settings page is requested the user will be presented with a
+        * list of links to individual settings pages for each group. If
+        * no groups are defined then all settings will be displayed on a 
+        * single settings page.
+        * 
+        * @param group the name of the group of settings we need a form to 
represent
+        */
+       createForm: function(group) {
+               if (group == undefined) {
+                       SettingsService.createGroupsIndex();
+               } else {
+                       var settings = SettingsService.getSettings(group);
+                       
+                       var form = document.createElement("form");
+                       if (group == undefined) {
+                               form.setAttribute("action", 
"javascript:SettingsService.submitSettingsForm()");
+                       } else {
+                               form.setAttribute("action", 
"javascript:SettingsService.submitSettingsForm('" + group + "')");
+                       }
+                       
+                       var title = document.createElement("h3");
+                       if (group == undefined) {
+                         title.innerHTML = "Settings";
+                       } else {
+                         title.innerHTML = "Settings: " + group;
+                       }
+                       
+                       for (key in settings) {
+                         var label = document.createElement("label");
+                         label.setAttribute("for", settings[key].id);
+                         label.innerHTML = settings[key].label;
+                         form.appendChild(label);
+                         
+                         var input = document.createElement("input");
+                         input.type = settings[key].type;
+                         input.id = settings[key].id;
+                         input.setAttribute("placeholder", 
settings[key].placeholder)
+                         var value = 
Widget.preferences.getItem(settings[key].id);
+                         if (value != undefined) {
+                           input.value = value;
+                         }
+                         form.appendChild(input);
+       
+                         form.appendChild(document.createElement("br"));
+                       };
+                       
+                       var submit = document.createElement("input");
+                       submit.type = "submit";
+                       submit.value = "Save";
+                       submit.setAttribute("class", "form-button");
+                       form.appendChild(submit);
+                       
+                       var settingsDiv = document.getElementById("settings");
+                       while (settingsDiv.hasChildNodes()) {
+                           settingsDiv.removeChild(settingsDiv.lastChild);
+                       }
+                       settingsDiv.appendChild(title);
+                       settingsDiv.appendChild(form);
+               }
+       },
+       
+       /**
+        * Populate the <div id="settings" page with an index to the various 
settings
+        * groups.
+        */
+       createGroupsIndex: function() {
+               var groups = SettingsService.getGroups();
+               
+               var title = document.createElement("h3");
+           title.innerHTML = "Settings Groups";
+           
+               var list = document.createElement("ol");
+           for (key in groups) {
+               var item = document.createElement("li");
+               var groupLink = document.createElement("a");
+               groupLink.href = "javascript:Controller.showSettings('" + 
groups[key] + "')"
+               groupLink.innerHTML = groups[key];
+               item.appendChild(groupLink);
+               list.appendChild(item);
+           }
+           
+           var settingsDiv = document.getElementById("settings");
+               while (settingsDiv.hasChildNodes()) {
+                   settingsDiv.removeChild(settingsDiv.lastChild);
+               }
+           
+           settingsDiv.appendChild(title);
+           settingsDiv.appendChild(list);
+       }
+}
\ No newline at end of file

Added: incubator/wookie/trunk/widgets/widget-template/settings/style/screen.css
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/widget-template/settings/style/screen.css?rev=1082336&view=auto
==============================================================================
--- incubator/wookie/trunk/widgets/widget-template/settings/style/screen.css 
(added)
+++ incubator/wookie/trunk/widgets/widget-template/settings/style/screen.css 
Wed Mar 16 22:57:43 2011
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+html, body {
+  height:100%; 
+  max-height:100%; 
+  padding:0; 
+  margin:0; 
+  border:0; 
+  font-family: "trebuchet ms", tahoma, verdana, arial, sans-serif;
+}
+
+#widget {
+       min-height:100%;
+    position:relative;
+}
+
+#toolbar {
+       background-color:#C0C0C0;
+    text-align:center;
+    font-size:75%;
+    height:16px;
+}
+
+#widget-icon {
+       float:left;
+}
+
+#content {
+    padding-bottom:1em;   /* Height of the footer */
+}
+
+#settings {
+       display:none;
+}
+
+#footer {
+       background-color:#C0C0C0;
+       position:absolute;
+    bottom:0;
+    width:100%;
+    font-size:75%;
+    text-align:center;
+    height:1.5em;   /* Height of the footer */
+}
+
+input.form-button {
+   font-size:9px;
+   font-family:Verdana,sans-serif;
+   font-weight:bold;
+   color:#fcfcfc;
+   background-color:#000000;
+   border-style:solid;
+   border-color:#cccccc;
+   border-width:1px;
+}
+
+.docs {
+       background-color:silver;
+}
\ No newline at end of file


Reply via email to