Преглед на файлове

Updated Frames for mor consistency -> PUT/POST, frames now allows for retrieving default values that are passed onto an url e.g. fill out form data

Manuel Gall преди 3 години
родител
ревизия
c47e60187c
променени са 4 файла, в които са добавени 93 реда и са изтрити 45 реда
  1. 32 21
      frames
  2. 37 19
      frames.xml
  3. 1 0
      template/css/frames.css
  4. 23 5
      template/js/ui.js

+ 32 - 21
frames

@@ -40,11 +40,10 @@ class Get < Riddl::Implementation
   end
 end
 
-class Put < Riddl::Implementation
+class InitFrame < Riddl::Implementation
   def response
     Dir.mkdir(File.join('data',@r.last)) rescue nil
     
-    
     if @p[0].value.to_s.strip.empty?
       File.write(File.join('data',@r.last,'style.url'), "../css/frames.css")
     else
@@ -83,29 +82,30 @@ class String
 end
 
 
-class NewFrame < Riddl::Implementation
+class NewFrameSet < Riddl::Implementation
   def response    
+  
+    
     path = File.join('data',@r.last,'frames.json')
     file = File.read(path)
     data_hash = JSON::parse(file)
     
     #check if new frame overlaps others if it does, delete overlapped frames
     data_hash["data"].each do | c |
-      if doOverlap(c['lx'], c['ly'], c['rx'], c['ry'], @p[0].value.to_i, @p[1].value.to_i, (@p[0].value.to_i + @p[2].value.to_i - 1), (@p[1].value.to_i + @p[3].value.to_i - 1))
+      if doOverlap(c['lx'], c['ly'], c['rx'], c['ry'], @p[1].value.to_i, @p[2].value.to_i, (@p[1].value.to_i + @p[3].value.to_i - 1), (@p[2].value.to_i + @p[4].value.to_i - 1))
         data_hash["data"].delete(c)
       end
     end
     
-    urls = JSON::parse(@p[4].value);
-    
-    hash = {lx: @p[0].value.to_i, ly: @p[1].value.to_i, rx: (@p[0].value.to_i + @p[2].value.to_i - 1), ry: (@p[1].value.to_i + @p[3].value.to_i - 1), url: urls, showbutton: @p[5].value.to_bool};
+    urls = JSON::parse(@p[6].value);
+    hash = {lx: @p[1].value.to_i, ly: @p[2].value.to_i, rx: (@p[1].value.to_i + @p[3].value.to_i - 1), ry: (@p[2].value.to_i + @p[4].value.to_i - 1), url: urls, showbutton: @p[5].value, default: JSON::parse(@p[7].value), callback: @h['CPEE_CALLBACK']};
     data_hash["data"].push(hash)
     File.write(path, JSON.dump(data_hash))
     
     #only send active url to client
     infofile = File.join('data',@r.last,'info.json')
     infojson = JSON::parse(File.read(infofile))
-    hash["url"] = urls[infojson["lang"]]
+    hash["url"] = urls.find{ |h| h['lang'] == infojson["lang"]}['url']
 
     @a[0].send(JSON.dump(hash))
     nil
@@ -115,7 +115,7 @@ end
 
 
 
-class NewFramePut < Riddl::Implementation
+class NewFrameWait < Riddl::Implementation
   def response    
     path = File.join('data',@r.last,'frames.json')
     file = File.read(path)
@@ -123,27 +123,30 @@ class NewFramePut < Riddl::Implementation
     
     #check if new frame overlaps others if it does, delete overlapped frames
     data_hash["data"].each do | c |
-      if doOverlap(c['lx'], c['ly'], c['rx'], c['ry'], @p[0].value.to_i, @p[1].value.to_i, (@p[0].value.to_i + @p[2].value.to_i - 1), (@p[1].value.to_i + @p[3].value.to_i - 1))
+      if doOverlap(c['lx'], c['ly'], c['rx'], c['ry'], @p[1].value.to_i, @p[2].value.to_i, (@p[1].value.to_i + @p[3].value.to_i - 1), (@p[2].value.to_i + @p[4].value.to_i - 1))
         data_hash["data"].delete(c)
       end
     end
     
-    urls = JSON::parse(@p[4].value);
-    
-    hash = {lx: @p[0].value.to_i, ly: @p[1].value.to_i, rx: (@p[0].value.to_i + @p[2].value.to_i - 1), ry: (@p[1].value.to_i + @p[3].value.to_i - 1), url: urls, showbutton: @p[5].value.to_bool, callback: @h['CPEE_CALLBACK']};
+    urls = JSON::parse(@p[6].value);
+    if @p[7].value == ""
+      puts "AAA";
+      hash = {lx: @p[1].value.to_i, ly: @p[2].value.to_i, rx: (@p[1].value.to_i + @p[3].value.to_i - 1), ry: (@p[2].value.to_i + @p[4].value.to_i - 1), url: urls, showbutton: @p[5].value, default: "{}", callback: @h['CPEE_CALLBACK']};
+    else
+      puts "BBB";
+      hash = {lx: @p[1].value.to_i, ly: @p[2].value.to_i, rx: (@p[1].value.to_i + @p[3].value.to_i - 1), ry: (@p[2].value.to_i + @p[4].value.to_i - 1), url: urls, showbutton: @p[5].value, default: JSON::parse(@p[7].value), callback: @h['CPEE_CALLBACK']};
+    end
     data_hash["data"].push(hash)
     File.write(path, JSON.dump(data_hash))
     
     #only send active url to client
     infofile = File.join('data',@r.last,'info.json')
     infojson = JSON::parse(File.read(infofile))
-    hash["url"] = urls[infojson["lang"]]
+    hash["url"] = urls.find{ |h| h['lang'] == infojson["lang"]}['url']
 
     @a[0].send(JSON.dump(hash))
     nil
- 
   end
-  
   def headers
     Riddl::Header.new('CPEE-CALLBACK', 'true')
   end
@@ -178,6 +181,9 @@ def doOverlap(l1x, l1y, r1x, r1y, l2x, l2y, r2x, r2y)
 end
 
 
+
+
+
 class Delete < Riddl::Implementation
   def response
     if cbu = File.read(File.join('data',@r.last,'callback'))
@@ -213,8 +219,9 @@ class GetFrames < Riddl::Implementation #{{{
       
       #remove not used languages
       file = JSON::parse(File.read(fname))
-      file["data"].each do |child|      
-        child["url"] = child["url"][infojson["lang"]]
+      
+      file["data"].each do |child|
+        child["url"] = child["url"].find{ |h| h['lang'] == infojson["lang"]}['url']
       end
       
       Riddl::Parameter::Complex.new('value','application/json',JSON.dump(file))
@@ -463,15 +470,19 @@ server = Riddl::Server.new(File.join(__dir__,'/frames.xml'), :host => 'localhost
       opts[:signals2]["handler"] ||= Signaling.new
 
       run Get, "test" if get
-      run Put, opts[:signals][idx] if put 'input'
+      run InitFrame, opts[:signals][idx] if post 'input'
       
-      run NewFrame, opts[:signals][idx] if post 'frame'
-      run NewFramePut, opts[:signals][idx] if put 'frame'
+      run NewFrameSet, opts[:signals][idx] if put 'sframe'
+      run NewFrameWait, opts[:signals][idx] if put 'wframe'
       
       run DeleteFrame, opts[:signals][idx] if post 'deleteframe'
       
       
       
+      
+      
+      
+      
       on resource 'handler' do
         run Handler, opts[:signals2]["handler"] if post 
         on resource 'sse' do

+ 37 - 19
frames.xml

@@ -24,13 +24,25 @@
     <parameter name="y_amount" type="string"/>
     <parameter name="lang" type="string"/>
   </message>
-  <message name="frame">
+  <message name="sframe">
+    <parameter name="type" fixed="set"/>
     <parameter name="lx" type="string"/>
-    <parameter name="ly" type="string"/>
-    <parameter name="x_amount" type="string"/>
-    <parameter name="y_amount" type="string"/>
-    <parameter name="url" type="string"/>
+    <parameter name="ly" type="nonNegativeInteger"/>
+    <parameter name="x_amount" type="nonNegativeInteger"/>
+    <parameter name="y_amount" type="nonNegativeInteger"/>
+    <parameter name="button" type="string"/>
+    <parameter name="urls" type="string"/>
+    <parameter name="default" type="string"/>
+  </message>
+  <message name="wframe">
+    <parameter name="type" fixed="wait"/>
+    <parameter name="lx" type="nonNegativeInteger"/>
+    <parameter name="ly" type="nonNegativeInteger"/>
+    <parameter name="x_amount" type="nonNegativeInteger"/>
+    <parameter name="y_amount" type="nonNegativeInteger"/>
     <parameter name="button" type="string"/>
+    <parameter name="urls" type="string"/>
+    <parameter name="default" type="string"/>
   </message>
   <message name="deleteframe">
     <parameter name="lx" type="string"/>
@@ -59,7 +71,7 @@
   <message name="json">
     <parameter name="value" mimetype="application/json"/>
   </message>
-  
+
   <message name="string"> <!--{{{-->
     <parameter name="value" type="string"/>
   </message> <!--}}}-->
@@ -67,15 +79,21 @@
   <resource>
     <resource>
       <get out="ui"/>
-      <put in='input'/>
-      
-      <put in='frame' out="callback"/>
-      <post in='frame'/>
+
+      <!-- create new Frame -->
+      <post in='input'/>
+      <!-- Set Frame Data with callback -->
+      <put in='wframe' out="callback"/>
+      <!-- Set Frame Data without callback -->
+      <put in='sframe'/>
+
+
+
       <post in='deleteframe'/>
-      
-      
-      
-      
+
+
+
+
       <delete in='opa'/>
       <delete in='opb'/>
       <resource relative='sse'>
@@ -101,16 +119,16 @@
         <get out="json"/>
         <post />
       </resource>
-      
-      
+
+
       <resource relative="test">
         <put in="string"/>
       </resource>
-        
-      
+
+
       <resource relative='handler'>
         <post />
-        
+
         <resource relative='sse'>
           <sse/>
         </resource>

+ 1 - 0
template/css/frames.css

@@ -18,6 +18,7 @@
   grid-template-columns: repeat(var(--grid-cols), 1fr);
   width: 100vw;
   height: 100vh;
+  overflow: hidden;
 }
 
 .grid-item {

+ 23 - 5
template/js/ui.js

@@ -15,7 +15,7 @@ function doOverlap(l1x, l1y, r1x, r1y, l2x, l2y, r2x, r2y) {
 }
 
 
-function makeFrame(lx, ly, rx, ry, content = "", id = "", showbutton=false) {
+function makeFrame(lx, ly, rx, ry, content = "", id = "", defaultpara = "", showbutton = "") {
   //check if rects overlap if they do remove old ones
   for (i = 0; i < window.storage.length; i++) {
     if(doOverlap(window.storage[i].lx, window.storage[i].ly, window.storage[i].rx, window.storage[i].ry, lx, ly, rx, ry)){
@@ -57,10 +57,26 @@ function makeFrame(lx, ly, rx, ry, content = "", id = "", showbutton=false) {
   //$(".item" + lx + "-" + ly).css({"display": "block", "border-style": "solid", "border-color": "blue", "grid-column": (lx+1) + " / span " + (rx-lx+1),  "grid-row": ly+1 + " / span " + (ry-ly+1)});
   
   if(content != null && content != ""){
-    $(".item" + lx + "-" + ly).html("<iframe width=100% height=100% name='" + id +"' id='" + id +"' src='" + content + "' title='' frameBorder='0' ></iframe>");
+  
+    var fullurl = content + "?";
+    //encode default parameter in URL
+    if(defaultpara != "{}"){
+      for (var key in defaultpara) {
+        if (defaultpara.hasOwnProperty(key)) {
+          fullurl = fullurl + key + "=" +  defaultpara[key] + "&";
+         }
+      }
+    }
+    
+    
+    //defaultpara
+    console.log(defaultpara);
+    
+    
+    $(".item" + lx + "-" + ly).html("<iframe width=100% height=100% name='" + id +"' id='" + id +"' src='" + fullurl + "' title='' frameBorder='0' ></iframe>");
             
     if(showbutton){
-      $(".item" + lx + "-" + ly).append('<button class="formbutton" type="button" onclick="sendForm(\'' + '.item' + lx + '-' + ly +'\', \'' + encodeURIComponent(id) + '\', \'' + lx  + '\', \'' + ly  + '\')">Submit</button>')
+      $(".item" + lx + "-" + ly).append('<button class="formbutton" type="button" onclick="sendForm(\'' + '.item' + lx + '-' + ly +'\', \'' + encodeURIComponent(id) + '\', \'' + lx  + '\', \'' + ly  + '\')">' + showbutton + '</button>')
     }
     
     //hideRectangel(lx, ly, rx, ry)
@@ -97,6 +113,7 @@ function sendForm(menuitem, cpeecallback,lx,ly){
   
   //Its a design question if removing the frame should be done within centurio, do have more controll, or automatic within code?
   //close frame
+  
   $(menuitem).remove();
   //remove frame from Server
   $.ajax({
@@ -107,6 +124,7 @@ function sendForm(menuitem, cpeecallback,lx,ly){
     success: function (data) {      
     }
   });
+  
 }
 
 function sendData(dataelement, datavalue){
@@ -161,7 +179,7 @@ function showDocument() {
         url: 'frames.json',
         success: function(ret2) {
           for (i in ret2.data) {
-            makeFrame(ret2.data[i].lx,ret2.data[i].ly,ret2.data[i].rx,ret2.data[i].ry, ret2.data[i].url, ret2.data[i].callback, ret2.data[i].showbutton);
+            makeFrame(ret2.data[i].lx,ret2.data[i].ly,ret2.data[i].rx,ret2.data[i].ry, ret2.data[i].url, ret2.data[i].callback, ret2.data[i].default, ret2.data[i].showbutton);
           }
         }
       });
@@ -289,7 +307,7 @@ function init() {
         try {
           //alert(e.data)
           var frd = JSON.parse(e.data)
-          makeFrame(frd.lx,frd.ly,frd.rx,frd.ry, frd.url, frd.callback, frd.showbutton);
+          makeFrame(frd.lx,frd.ly,frd.rx,frd.ry, frd.url, frd.callback, frd.default, frd.showbutton);
         }
         catch (e) {
         }