[silva.ui][Sylvain Viollon] Add a form with template base class.

[email protected] Thu, 31 Oct 2013 15:55:25 +0100
Newsgroups gmane.comp.web.zope.silva.cvs
Message-ID <[email protected]>
author:    Sylvain Viollon
date:      Thu Oct 31 15:55:21 2013 +0100
revision:  673:442604c6d4d5 in silva.ui
branch:    
details:   https://hg.infrae.com/silva.ui?cmd=changeset;node=442604c6d4d5
modified:  src/silva/ui/martiansupport/page.py src/silva/ui/rest/__init__.py src/silva/ui/rest/base.py src/silva/ui/static/js/smi.form.js src/silva/ui/static/js/smi.form.min.js
added:     
removed:   
log:       Add a form with template base class.


diffstat:

 src/silva/ui/martiansupport/page.py    |  12 ++++++++++++
 src/silva/ui/rest/__init__.py          |   3 ++-
 src/silva/ui/rest/base.py              |  22 ++++++++++++++++++++++
 src/silva/ui/static/js/smi.form.js     |  17 +++++++++++------
 src/silva/ui/static/js/smi.form.min.js |   2 +-
 5 files changed, 48 insertions(+), 8 deletions(-)

diffs (134 lines):

diff -r e18f5c0b93f4 -r 442604c6d4d5 src/silva/ui/martiansupport/page.py
--- a/src/silva/ui/martiansupport/page.py	Wed Oct 30 17:44:31 2013 +0100
+++ b/src/silva/ui/martiansupport/page.py	Thu Oct 31 15:55:21 2013 +0100
@@ -5,6 +5,7 @@
 import martian
 from grokcore.view.meta.views import TemplateGrokker
 from silva.ui.rest.base import PageWithTemplateREST
+from silva.ui.rest.base import FormWithTemplateREST
 
 
 class PageTemplateGrokker(TemplateGrokker):
@@ -16,3 +17,14 @@
     def has_no_render(self, factory):
         # always has a render method
         return False
+
+
+class FormTemplateGrokker(TemplateGrokker):
+    martian.component(FormWithTemplateREST)
+
+    def has_render(self, factory):
+        return factory.payload != FormWithTemplateREST.payload
+
+    def has_no_render(self, factory):
+        # always has a render method
+        return False
diff -r e18f5c0b93f4 -r 442604c6d4d5 src/silva/ui/rest/__init__.py
--- a/src/silva/ui/rest/__init__.py	Wed Oct 30 17:44:31 2013 +0100
+++ b/src/silva/ui/rest/__init__.py	Thu Oct 31 15:55:21 2013 +0100
@@ -7,6 +7,7 @@
 
 from silva.ui.rest.base import Screen, UIREST, PageREST, PageWithTemplateREST
 from silva.ui.rest.base import PageWithLayoutREST, SMITransaction
+from silva.ui.rest.base import FormWithTemplateREST
 from silva.ui.rest.exceptions import RedirectToPage, RedirectToUrl
 from silva.ui.rest.exceptions import RedirectToPreview
 from silva.ui.rest.exceptions import PageResult, ActionResult, RESTResult
@@ -14,4 +15,4 @@
 __all__ = ['Screen', 'REST', 'UIREST', 'PageREST', 'PageWithTemplateREST',
            'RedirectToPage', 'RedirectToUrl', 'IRESTMethodPublishedEvent',
            'RedirectToPreview', 'PageWithLayoutREST', 'SMITransaction',
-           'PageResult', 'ActionResult', 'RESTResult']
+           'PageResult', 'ActionResult', 'RESTResult', 'FormWithTemplateREST']
diff -r e18f5c0b93f4 -r 442604c6d4d5 src/silva/ui/rest/base.py
--- a/src/silva/ui/rest/base.py	Wed Oct 30 17:44:31 2013 +0100
+++ b/src/silva/ui/rest/base.py	Thu Oct 31 15:55:21 2013 +0100
@@ -170,6 +170,7 @@
 class PageWithTemplateREST(PageREST):
     grok.baseclass()
 
+
     def default_namespace(self):
         return {}
 
@@ -188,6 +189,27 @@
                 'html': self.template.render(self)}
 
 
+class FormWithTemplateREST(PageREST):
+    grok.baseclass()
+
+    def default_namespace(self):
+        return {}
+
+    def namespace(self):
+        return {'rest': self,
+                'context': self.context,
+                'request': self.request,
+                'target_language': self.language}
+
+    def update(self):
+        pass
+
+    def payload(self):
+        self.update()
+        return {'ifaces': ['form'],
+                'forms': self.template.render(self)}
+
+
 class PageWithLayoutREST(rest.REST):
     grok.implements(IPage)
 
diff -r e18f5c0b93f4 -r 442604c6d4d5 src/silva/ui/static/js/smi.form.js
--- a/src/silva/ui/static/js/smi.form.js	Wed Oct 30 17:44:31 2013 +0100
+++ b/src/silva/ui/static/js/smi.form.js	Thu Oct 31 15:55:21 2013 +0100
@@ -181,7 +181,12 @@
             return {
                 template_data: true,
                 render: function() {
+                    if (!data.forms) {
+                        return;
+                    };
+
                     $container = $('<div class="forms form-content"></div>');
+                    $container.html(data.forms);
                     $content.append($container);
 
                     // Add content
@@ -191,8 +196,8 @@
                         $container.addClass("forms-two-columns");
                         $content.append($portlets);
                     };
+
                     // Find forms
-                    $container.html(data.forms);
                     smi.shortcuts.create('form', $container, true);
 
                     // Initialize each form.
@@ -333,12 +338,12 @@
                     // Send cleanup event
                     if ($container !== null) {
                         $container.trigger('clean-smiform', {form: $form, container: $container});
+
+                        // Cleanup
+                        smi.shortcuts.remove('form');
+                        $content.undelegate('.form-section', 'focusin');
+                        $content.undelegate('.form-section', 'click');
                     };
-                    // Cleanup
-                    smi.shortcuts.remove('form');
-                    $content.undelegate('.form-section', 'focusin');
-                    $content.undelegate('.form-section', 'click');
-                    $content.removeClass('form-content');
                     $content.empty();
                 }
             };
diff -r e18f5c0b93f4 -r 442604c6d4d5 src/silva/ui/static/js/smi.form.min.js
--- a/src/silva/ui/static/js/smi.form.min.js	Wed Oct 30 17:44:31 2013 +0100
+++ b/src/silva/ui/static/js/smi.form.min.js	Thu Oct 31 15:55:21 2013 +0100
@@ -1,1 +1,1 @@
-(function($,infrae){var scroll_field_into_view=function($content,$field){var $section=$field.closest(".form-section");if($section.length){var section_top=$section.position().top,section_height=$section.outerHeight(),section_bottom=section_top+section_height,content_top=$content.scrollTop(),content_height=$content.innerHeight(),content_bottom=content_top+content_height;content_top>section_top-25?infrae.ui.scroll($content,"fast","absolute",section_top-25):section_bottom+25>content_bottom&&infrae.ui.scroll($content,"fast","absolute",section_bottom-content_height+25)}},unfocus_form_fields=function($content){$content.find(".form-section").removeClass("form-focus")},focus_form_field=function($content,$field,no_input_focus){var $section=$field.closest(".form-section");$section.is(".form-focus")|
 |(unfocus_form_fields($content),$section.addClass("form-focus"),no_input_focus||$section.find(".field:first").focus())},focus_first_form_field=function($content){var $field=$content.find(".f
 orm-error:first").find(".field:first");$field.length||($field=$content.find(".field-required:first"),$field.length||($field=$content.find(".field:first"))),$field.length&&(focus_form_field($content,$field),scroll_field_into_view($content,$field))},focus_next_form_field=function($content){var $next_body,$next_form,$focused=$content.find(".form-focus"),$field=$focused.next();$field.length||($next_body=$focused.closest(".form-body").nextAll(".form-body"),$next_body.length?$field=$next_body.find(".form-section:first"):($next_form=$focused.closest("form").nextAll("form"),$field=$next_form.length?$next_form.find(".form-section:first"):$content.find(".form-section:first"))),focus_form_field($content,$field),scroll_field_into_view($content,$field)},focus_previous_form_field=function($content){var
  $prev_body,$prev_form,$focused=$content.find(".form-focus"),$field=$focused.prev();$field.length||($prev_body=$focused.closest(".form-body").prevAll(".form-body"),$prev_body.length?$field=$
 prev_body.find(".form-section:last"):($prev_form=$focused.closest("form").prevAll("form"),$field=$prev_form.length?$prev_form.find(".form-section:last"):$content.find(".form-section:last"))),focus_form_field($content,$field),scroll_field_into_view($content,$field)};$(document).on("load-smiform",".form-content",function(event,data){var $container=data.container||$(this),$content=data.$content||$container,$form=data.form||$container.find("form"),focus_form_event=function(event){var $target=$(event.target),$section=$target.closest(".form-section"),is_cancelable=$target.is("input")||$target.is("a");focus_form_field($content,$section,is_cancelable),is_cancelable||event.stopPropagation()};$container.delegate(".form-section","click",focus_form_event),$container.delegate(".form-section","focusin"
 ,focus_form_event),$container.delegate(".form-error-detail","click",function(){$(this).fadeOut().promise().done(function(){$(this).remove()})}),focus_first_form_field($content),$form.trigger
 ("loadwidget-smiform",data)}),infrae.views.view({iface:"form",name:"content",factory:function($content,data,smi){var $container=null,$form=$([]),objection=null;return{template_data:!0,render:function(){if($container=$('<div class="forms form-content"></div>'),$content.append($container),data.portlets){var $portlets=$('<div class="portlets form-content"></div>');$portlets.html(data.portlets),$container.addClass("forms-two-columns"),$content.append($portlets)}$container.html(data.forms),smi.shortcuts.create("form",$container,!0),$form=$container.find("form"),$form.each(function(){var $form=$(this),form_prefix=$form.attr("name"),submit=function($control){return function(){var queue=$.Deferred(),submission=queue,have_control=void 0!==$control&&$control.length,message=void 0,is_link=!1,seriali
 ze=function(){var values;return $form.trigger("serialize-smiform",{form:$form,container:$container}),values=$form.serializeArray(),have_control&&values.push({name:$control.attr("name"),value
 :$control.text()}),values};if(have_control&&(message=$control.attr("data-confirmation"),is_link=$control.hasClass("link-control")),!is_link){if(void 0!==message){var title=$control.attr("data-confirmation-title");title||(title="Please confirm"),queue=queue.then(function(){return infrae.ui.ConfirmationDialog({title:title,message:message})})}return queue.then(function(){if(null!==objection){var result=objection();if(result)return result.done(function(){objection=null})}return $.Deferred().resolve()},function(){return $.Deferred().reject()}).then(function(){return smi.ajax.send_to_opened(serialize())}),submission.resolve(),!1}$control.attr("href",[smi.get_screen_url(),"?",$.param(serialize())].join(""))}};$form.bind("submit",submit($form.find(".form-controls a.default-form-control"))),$form.
 find(".form-controls a.form-control").each(function(){var $control=$(this),shortcut=$control.attr("data-form-shortcut"),handler=submit($control);$control.bind("click",handler),shortcut&&smi.
 shortcuts.bind("form",null,[shortcut],handler)}),$form.find(".form-controls a.form-button").each(function(){var $control=$(this),shortcut=$control.attr("data-form-shortcut");shortcut&&smi.shortcuts.bind("form",null,[shortcut],function(){return $control.click(),!1})}),$form.delegate(".form-controls a.form-popup","click",function(){var $control=$(this),handler=submit();return $control.SMIFormPopup().done(function(data){data.extra&&data.extra.refresh==form_prefix&&handler()}),!1}),$form.tipsy({delegate:"a.form-control"}),$form.tipsy({delegate:"a.form-button"}),$form.attr("data-form-url",smi.get_screen_url())}),$container.trigger("load-smiform",{form:$form,container:$container,$content:$content,set_objection:function(deferred){objection=deferred}}),smi.shortcuts.bind("form",null,["ctrl+down",
 "ctrl+shift+down"],function(){return focus_next_form_field($content),!1}),smi.shortcuts.bind("form",null,["ctrl+up","ctrl+shift+up"],function(){return focus_previous_form_field($content),!1}
 )},cleanup:function(){null!==$container&&$container.trigger("clean-smiform",{form:$form,container:$container}),smi.shortcuts.remove("form"),$content.undelegate(".form-section","focusin"),$content.undelegate(".form-section","click"),$content.removeClass("form-content"),$content.empty()}}}})})(jQuery,infrae);
\ No newline at end of file
+(function($,infrae){var scroll_field_into_view=function($content,$field){var $section=$field.closest(".form-section");if($section.length){var section_top=$section.position().top,section_height=$section.outerHeight(),section_bottom=section_top+section_height,content_top=$content.scrollTop(),content_height=$content.innerHeight(),content_bottom=content_top+content_height;content_top>section_top-25?infrae.ui.scroll($content,"fast","absolute",section_top-25):section_bottom+25>content_bottom&&infrae.ui.scroll($content,"fast","absolute",section_bottom-content_height+25)}},unfocus_form_fields=function($content){$content.find(".form-section").removeClass("form-focus")},focus_form_field=function($content,$field,no_input_focus){var $section=$field.closest(".form-section");$section.is(".form-focus")|
 |(unfocus_form_fields($content),$section.addClass("form-focus"),no_input_focus||$section.find(".field:first").focus())},focus_first_form_field=function($content){var $field=$content.find(".f
 orm-error:first").find(".field:first");$field.length||($field=$content.find(".field-required:first"),$field.length||($field=$content.find(".field:first"))),$field.length&&(focus_form_field($content,$field),scroll_field_into_view($content,$field))},focus_next_form_field=function($content){var $next_body,$next_form,$focused=$content.find(".form-focus"),$field=$focused.next();$field.length||($next_body=$focused.closest(".form-body").nextAll(".form-body"),$next_body.length?$field=$next_body.find(".form-section:first"):($next_form=$focused.closest("form").nextAll("form"),$field=$next_form.length?$next_form.find(".form-section:first"):$content.find(".form-section:first"))),focus_form_field($content,$field),scroll_field_into_view($content,$field)},focus_previous_form_field=function($content){var
  $prev_body,$prev_form,$focused=$content.find(".form-focus"),$field=$focused.prev();$field.length||($prev_body=$focused.closest(".form-body").prevAll(".form-body"),$prev_body.length?$field=$
 prev_body.find(".form-section:last"):($prev_form=$focused.closest("form").prevAll("form"),$field=$prev_form.length?$prev_form.find(".form-section:last"):$content.find(".form-section:last"))),focus_form_field($content,$field),scroll_field_into_view($content,$field)};$(document).on("load-smiform",".form-content",function(event,data){var $container=data.container||$(this),$content=data.$content||$container,$form=data.form||$container.find("form"),focus_form_event=function(event){var $target=$(event.target),$section=$target.closest(".form-section"),is_cancelable=$target.is("input")||$target.is("a");focus_form_field($content,$section,is_cancelable),is_cancelable||event.stopPropagation()};$container.delegate(".form-section","click",focus_form_event),$container.delegate(".form-section","focusin"
 ,focus_form_event),$container.delegate(".form-error-detail","click",function(){$(this).fadeOut().promise().done(function(){$(this).remove()})}),focus_first_form_field($content),$form.trigger
 ("loadwidget-smiform",data)}),infrae.views.view({iface:"form",name:"content",factory:function($content,data,smi){var $container=null,$form=$([]),objection=null;return{template_data:!0,render:function(){if(data.forms){if($container=$('<div class="forms form-content"></div>'),$container.html(data.forms),$content.append($container),data.portlets){var $portlets=$('<div class="portlets form-content"></div>');$portlets.html(data.portlets),$container.addClass("forms-two-columns"),$content.append($portlets)}smi.shortcuts.create("form",$container,!0),$form=$container.find("form"),$form.each(function(){var $form=$(this),form_prefix=$form.attr("name"),submit=function($control){return function(){var queue=$.Deferred(),submission=queue,have_control=void 0!==$control&&$control.length,message=void 0,is_
 link=!1,serialize=function(){var values;return $form.trigger("serialize-smiform",{form:$form,container:$container}),values=$form.serializeArray(),have_control&&values.push({name:$control.att
 r("name"),value:$control.text()}),values};if(have_control&&(message=$control.attr("data-confirmation"),is_link=$control.hasClass("link-control")),!is_link){if(void 0!==message){var title=$control.attr("data-confirmation-title");title||(title="Please confirm"),queue=queue.then(function(){return infrae.ui.ConfirmationDialog({title:title,message:message})})}return queue.then(function(){if(null!==objection){var result=objection();if(result)return result.done(function(){objection=null})}return $.Deferred().resolve()},function(){return $.Deferred().reject()}).then(function(){return smi.ajax.send_to_opened(serialize())}),submission.resolve(),!1}$control.attr("href",[smi.get_screen_url(),"?",$.param(serialize())].join(""))}};$form.bind("submit",submit($form.find(".form-controls a.default-form-con
 trol"))),$form.find(".form-controls a.form-control").each(function(){var $control=$(this),shortcut=$control.attr("data-form-shortcut"),handler=submit($control);$control.bind("click",handler)
 ,shortcut&&smi.shortcuts.bind("form",null,[shortcut],handler)}),$form.find(".form-controls a.form-button").each(function(){var $control=$(this),shortcut=$control.attr("data-form-shortcut");shortcut&&smi.shortcuts.bind("form",null,[shortcut],function(){return $control.click(),!1})}),$form.delegate(".form-controls a.form-popup","click",function(){var $control=$(this),handler=submit();return $control.SMIFormPopup().done(function(data){data.extra&&data.extra.refresh==form_prefix&&handler()}),!1}),$form.tipsy({delegate:"a.form-control"}),$form.tipsy({delegate:"a.form-button"}),$form.attr("data-form-url",smi.get_screen_url())}),$container.trigger("load-smiform",{form:$form,container:$container,$content:$content,set_objection:function(deferred){objection=deferred}}),smi.shortcuts.bind("form",nul
 l,["ctrl+down","ctrl+shift+down"],function(){return focus_next_form_field($content),!1}),smi.shortcuts.bind("form",null,["ctrl+up","ctrl+shift+up"],function(){return focus_previous_form_fiel
 d($content),!1})}},cleanup:function(){null!==$container&&($container.trigger("clean-smiform",{form:$form,container:$container}),smi.shortcuts.remove("form"),$content.undelegate(".form-section","focusin"),$content.undelegate(".form-section","click")),$content.empty()}}}})})(jQuery,infrae);
\ No newline at end of file