﻿<extension>
  <title>Topsan Grouping widget</title>
  <description> This extension allows you to create new groups. It does so by creating a new page via the api and tagging it with 'define:newgroupname' and 'groupdefinition'. </description>
  <namespace>topsan</namespace>
  <function>
    <name>creategroups</name>
    <param name="groupNames" type="list">list of all group proteins names</param>
    <param name="pageuri" type="uri">page uri where the new group page will be created at</param>
    <return>
      <html xmlns:eval="http://mindtouch.com/2007/dekiscript">
        <head>
          <script type="text/javascript">
            var pageapi = <eval:js>args.pageuri</eval:js>;
            var groupnames = new Array();
            groupnames = <eval:js>args.groupNames</eval:js>
          //<![CDATA[         
            function CreateGroup(newgroupValue) {                 
                 var exists = -1;
                 var update = 0;
                 
                 for (var k=0; k<groupnames.length;k++)
                 {   
                    if (groupnames[k] == undefined)
                        continue;
                    if (groupnames[k].toLowerCase() == newgroupValue.toLowerCase())
                    {   
                       exists = k;
                    }
                 }   
                 if (exists == -1 && newgroupValue != "") 
                 {  //make sure there is a new group to add   
                   update = 1;
                 }
                 if (exists > -1)
                 {
                    alert("There is already a group named: '"+newgroupValue+"'");
                 }
                 
                if (update == 1)
                {
                     //first create the page.
                     var newpageapi = pageapi + newgroupValue;
                     var messagepage = "<h4>Topsan Members</h4><p>{{ template.Group{ } }}</p><h4 id="topsanSummary">Summary</h4>";
                     DekiWiki.$.ajax({
                        type: "POST",
                        url: newpageapi + "/contents",
                        data: messagepage,
                        dataType: "xml",
                        contentType: "text/plain",
                        async: false,
                        success: function(r) {
                          alert("creating page: '"+newpageapi+"'");
                          }
                     }); 
                     
                     //next, create tags for the new group.
                     var message  = "<tags count=\"2\" href=\"" + newpageapi + "/tags\">";                     
                     message += "<tag value=\"define:" + newgroupValue + "\"><type>text</type>" + "<uri></uri>" + "<title>" + newgroupValue + "</title>" + "</tag>";
                     //message += "<tag value=\"groupDefinition\"><type>text</type><uri></uri></tag>";
                     
                     message += "</tags>";                
                     
                     DekiWiki.$.ajax({
                        type: "POST",
                        url: newpageapi + "/tags?dream.in.verb=PUT",
                        data: message,
                        dataType: "xml",
                        contentType: "text/xml",
                        async: false,
                        success: function(r) {
                          }
                     });                    
                }
            }
            // ]]>
          </script>
        </head>

        <body>        
          <div id="groups">
            <strong>
              <form name="groupform" action="javascript:void(0)" onSubmit="CreateGroup(groupname.value);">
                <div align="center">
                  <input type="text" name="groupname"></input>                  
                  <input type="submit" name="createpageBtn" value="Create Group" onclick="document.groupform.submit();"/>
                  </div>
              </form>
            </strong>
          </div>

        </body>
      </html>
    </return>
  </function>
</extension>
