Commit 07d5634a authored by Nicolas Dumazet's avatar Nicolas Dumazet

2010-08-18

* Refactor/simplify in order to extend jQuery with a erp5_popup function that can be re-used in other scripts/projects.



In this way anyone who has loaded the .js can invoke a popup:

myelement.erp5_popup({
dialog: { // usual jquery.ui.dialog parameters here
title: "a popup to make UI snappier",
},
load: { // specify what url should be loaded inside the popup
url: "/erp5/my_module",
params: ....,
},
}

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@37875 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 67857de9
...@@ -2,10 +2,7 @@ ...@@ -2,10 +2,7 @@
<ZopeData> <ZopeData>
<record id="1" aka="AAAAAAAAAAE="> <record id="1" aka="AAAAAAAAAAE=">
<pickle> <pickle>
<tuple> <global name="Folder" module="OFS.Folder"/>
<global name="Folder" module="OFS.Folder"/>
<tuple/>
</tuple>
</pickle> </pickle>
<pickle> <pickle>
<dictionary> <dictionary>
......
...@@ -2,10 +2,7 @@ ...@@ -2,10 +2,7 @@
<ZopeData> <ZopeData>
<record id="1" aka="AAAAAAAAAAE="> <record id="1" aka="AAAAAAAAAAE=">
<pickle> <pickle>
<tuple> <global name="DTMLMethod" module="OFS.DTMLMethod"/>
<global name="DTMLMethod" module="OFS.DTMLMethod"/>
<tuple/>
</tuple>
</pickle> </pickle>
<pickle> <pickle>
<dictionary> <dictionary>
...@@ -66,151 +63,182 @@ If you want to use this feature, you need to load additional files in global_def ...@@ -66,151 +63,182 @@ If you want to use this feature, you need to load additional files in global_def
The first two lines are required for loading jQuery and jQuery UI. The last line is for this file.\n The first two lines are required for loading jQuery and jQuery UI. The last line is for this file.\n
*/\n */\n
\n \n
jQuery(function() {\n $(function() {\n
// XXX It is necessary to keep a reference to a dialog, because jQuery / jQuery UI does not keep information\n /*\n
// in elements of DOM unfortunately. This is not a big problem at the moment, because this implementation assumes\n * generic dialog to display another ERP5 page on top of the page.\n
// that a dialog is modal.\n *\n
var dialog = null;\n * Parameters:\n
\n * \'dialog\': object to pass as argument to $.ui.dialog on creation.\n
var load = function(url, query, method_name) {\n * erp5_dialog has generic defaults, and everything you will\n
//dialog.empty();\n * pass will override those defaults.\n
\n * \'load\' :\n
// Some bogus animations for having the user to feel easier.\n * - url: url to load in the popup\n
var animate = function() {\n * - params: parameters to give to the ajax call. can be omitted\n
var element = jQuery(\'p.loading\', dialog);\n * - method: default $.post, you can change it to $.get\n
if (element.length != 0) {\n *\n
//element.animate({opacity: 1}, 2000, \'linear\');\n * Example:\n
//element.animate({opacity: 0}, 2000, \'linear\', animate);\n * $(\'<div id="jquery_erp5_dialog" />\').appendTo(\'body\').erp5_popup({\n
element.animate({color: "white"}, 2000, \'linear\');\n * dialog: {title: \'It works\', },\n
element.animate({color: "black"}, 2000, \'linear\', animate);\n * load: {url: \'/erp5/some_module/someobject\'},\n
}\n * )};\n
};\n */\n
jQuery(\'<div class="loading" style="background-color: #AAAAAA; opacity: 0.5; position: absolute; left: 0%; width: 100%; top: 0%; height: 100%; transparent;"><p class="loading" style="position: absolute; left: 0%; width: 100%; top: 30%; height: 40%; text-align: center; color: black; font-size: 32pt;">Loading...</p></div>\').appendTo(dialog);\n $.fn.erp5_popup = function(params) {\n
animate();\n dialog = $(this);\n
\n \n
// define fallback values\n var default_dialog_parameters = {\n
if (method_name == undefined)\n modal: true,\n
method_name = \'post\';\n width: $(window).width() * 0.8,\n
if (method_name == \'post\')\n height: $(window).height() * 0.8,\n
var caller = jQuery.post;\n title: \'ERP5 dialog\',\n
if (method_name == \'get\')\n close: function() {\n
var caller = jQuery.get;\n dialog.dialog(\'destroy\');\n
caller(url, query, function(data, textStatus, XMLHttpRequest) {\n dialog.empty();\n
if (textStatus == \'success\' || textStatus == \'notmodified\') {\n },\n
// Stop the animations above.\n }\n
dialog.empty();\n // initalize jQuery dialog\n
//jQuery(\'div.loading\', dialog).remove();\n dialog.dialog($.extend({}, default_dialog_parameters, params.dialog));\n
\n \n
dialog.html(jQuery(\'<div />\').append(data.replace(/<script(.|\\s)*?\\/script>/g, "")).find(\'form\'));\n var load = function(url, query, ajax_method) {\n
\n if (!query) query = {};\n
// XXX Get rid of unneeded stuff in JavaScript for now.\n if (!ajax_method) ajax_method = $.post;\n
jQuery(\'.bars, .breadcrumb, .logged_in_as\', dialog).remove();\n //dialog.empty();\n
jQuery(\'[id]\', dialog).removeAttr(\'id\');\n \n
// XXX Get rid of unneeded KM stuff in JavaScript for now.\n // Some bogus animations for having the user to feel easier.\n
jQuery(\'.wrapper\', dialog).remove();\n var animate = function() {\n
\n var element = $(\'p.loading\', dialog);\n
// Insert the same buttons as at the bottom into near the top.\n if (element.length != 0) {\n
jQuery(\'div.bottom_actions\', dialog).clone().insertAfter(jQuery(\'div.dialog_box\', dialog)).css(\'margin-bottom\', \'1em\');\n //element.animate({opacity: 1}, 2000, \'linear\');\n
\n //element.animate({opacity: 0}, 2000, \'linear\', animate);\n
jQuery(\'input[type="image"], button.sort_button, .dialog_selector > button\', dialog).click(function(event) {\n element.animate({color: "white"}, 2000, \'linear\');\n
event.preventDefault();\n element.animate({color: "black"}, 2000, \'linear\', animate);\n
var self = jQuery(this);\n }\n
var form = jQuery(\'form.main_form\', dialog);\n };\n
var params = {};\n $(\'<div class="loading" style="background-color: #AAAAAA; opacity: 0.5; position: absolute; left: 0%; width: 100%; top: 0%; height: 100%; transparent;"><p class="loading" style="position: absolute; left: 0%; width: 100%; top: 30%; height: 40%; text-align: center; color: black; font-size: 32pt;">Loading...</p></div>\').appendTo(dialog);\n
params[self.attr(\'name\')] = self.attr(\'value\');\n animate();\n
load(form.attr(\'action\'), jQuery.param(params) + \'&\' + form.serialize());\n \n
});\n ajax_method(url, query, function(data, textStatus, XMLHttpRequest) {\n
\n if (textStatus == \'success\' || textStatus == \'notmodified\') {\n
// XXX Remove the hardcoded handler.\n // Stop the animations above.\n
jQuery(\'.dialog_selector > select[onchange]\', dialog).removeAttr(\'onchange\');\n dialog.empty();\n
jQuery(\'.dialog_selector > select\', dialog).change(function(event) {\n //$(\'div.loading\', dialog).remove();\n
//event.preventDefault();\n \n
var button = jQuery(\'button\', this.parentNode);\n dialog.html($(\'<div />\').append(data.replace(/<script(.|\\s)*?\\/script>/g, "")).find(\'form\'));\n
var form = jQuery(\'form.main_form\', dialog);\n \n
var params = {};\n // XXX Get rid of unneeded stuff in JavaScript for now.\n
params[button.attr(\'name\')] = button.attr(\'value\');\n $(\'.bars, .breadcrumb, .logged_in_as\', dialog).remove();\n
load(form.attr(\'action\'), jQuery.param(params) + \'&\' + form.serialize()); \n $(\'[id]\', dialog).removeAttr(\'id\');\n
});\n // XXX Get rid of unneeded KM stuff in JavaScript for now.\n
\n $(\'.wrapper\', dialog).remove();\n
// XXX Remove the hardcoded handler.\n \n
jQuery(\'input[type="text"][name="listbox_page_start"][onkeypress]\', dialog).removeAttr(\'onkeypress\');\n // Insert the same buttons as at the bottom into near the top.\n
jQuery(\'input[type="text"][name="listbox_page_start"]\', dialog).keypress(function(event) {\n $(\'div.bottom_actions\', dialog).clone().insertAfter($(\'div.dialog_box\', dialog)).css(\'margin-bottom\', \'1em\');\n
if (event.keyCode == \'13\') {\n \n
$(\'input[type="image"], button.sort_button, .dialog_selector > button\', dialog).click(function(event) {\n
event.preventDefault();\n event.preventDefault();\n
var self = jQuery(this);\n var self = $(this);\n
self.value = self.attr(\'defaultValue\');\n var form = $(\'form.main_form\', dialog);\n
var form = jQuery(\'form.main_form\', dialog);\n var params = {};\n
// XXX no other way but hardcoding the method name.\n params[self.attr(\'name\')] = self.attr(\'value\');\n
load(\'listbox_setPage\', form.serialize()); \n load(form.attr(\'action\'), $.param(params) + \'&\' + form.serialize());\n
}\n });\n
});\n \n
\n // XXX Remove the hardcoded handler.\n
jQuery(\'tr.listbox_search_line input[type="text"]\', dialog).removeAttr(\'onkeypress\').keypress(function(event) {\n $(\'.dialog_selector > select[onchange]\', dialog).removeAttr(\'onchange\');\n
if (event.keyCode == \'13\') {\n $(\'.dialog_selector > select\', dialog).change(function(event) {\n
//event.preventDefault();\n
var button = $(\'button\', this.parentNode);\n
var form = $(\'form.main_form\', dialog);\n
var params = {};\n
params[button.attr(\'name\')] = button.attr(\'value\');\n
load(form.attr(\'action\'), $.param(params) + \'&\' + form.serialize());\n
});\n
\n
// XXX Remove the hardcoded handler.\n
$(\'input[type="text"][name="listbox_page_start"][onkeypress]\', dialog).removeAttr(\'onkeypress\');\n
$(\'input[type="text"][name="listbox_page_start"]\', dialog).keypress(function(event) {\n
if (event.keyCode == \'13\') {\n
event.preventDefault();\n
var self = $(this);\n
self.value = self.attr(\'defaultValue\');\n
var form = $(\'form.main_form\', dialog);\n
// XXX no other way but hardcoding the method name.\n
load(\'listbox_setPage\', form.serialize());\n
}\n
});\n
\n
$(\'tr.listbox_search_line input[type="text"]\', dialog).removeAttr(\'onkeypress\').keypress(function(event) {\n
if (event.keyCode == \'13\') {\n
event.preventDefault();\n
//var self = $(this);\n
//self.value = self.attr(\'defaultValue\');\n
var form = $(\'form.main_form\', dialog);\n
var first_submit_button = $($(\'input[type="submit"]\', form)[0]);\n
var params = {};\n
params[first_submit_button.attr(\'name\')] = first_submit_button.attr(\'value\');\n
load(form.attr(\'action\'), $.param(params) + \'&\' + form.serialize());\n
}\n
});\n
\n
$(\'button.dialog_cancel_button\', dialog).click(function(event) {\n
event.preventDefault();\n event.preventDefault();\n
//var self = jQuery(this);\n dialog.dialog(\'close\');\n
//self.value = self.attr(\'defaultValue\');\n });\n
var form = jQuery(\'form.main_form\', dialog);\n \n
var first_submit_button = jQuery(jQuery(\'input[type="submit"]\', form)[0]);\n $(\'button.dialog_update_button\', dialog).click(function(event) {\n
event.preventDefault();\n
var self = $(this);\n
var form = $(\'form.main_form\', dialog);\n
var params = {};\n var params = {};\n
params[first_submit_button.attr(\'name\')] = first_submit_button.attr(\'value\');\n params[self.attr(\'name\')] = self.attr(\'value\');\n
load(form.attr(\'action\'), jQuery.param(params) + \'&\' + form.serialize()); \n load(form.attr(\'action\'), $.param(params) + \'&\' + form.serialize());\n
}\n });\n
});\n }\n
\n });\n
jQuery(\'button.dialog_cancel_button\', dialog).click(function(event) {\n };\n
event.preventDefault();\n load(params.load.url, params.load.params, params.load.method);\n
dialog.dialog(\'close\');\n
});\n
\n
jQuery(\'button.dialog_update_button\', dialog).click(function(event) {\n
event.preventDefault();\n
var self = jQuery(this);\n
var form = jQuery(\'form.main_form\', dialog);\n
var params = {};\n
params[self.attr(\'name\')] = self.attr(\'value\');\n
load(form.attr(\'action\'), jQuery.param(params) + \'&\' + form.serialize());\n
});\n
}\n
});\n
};\n };\n
});\n
\n
$(function() {\n
// XXX It is necessary to keep a reference to a dialog, because jQuery / jQuery UI does not keep information\n
// in elements of DOM unfortunately. This is not a big problem at the moment, because this implementation assumes\n
// that a dialog is modal.\n
// XXX Nicolas: see $.data() for storage in DOM. I dont think that it matters however. $("#jquery_erp5_dialog") should be enough\n
var dialog = $(\'<div id="jquery_erp5_dialog" />\').appendTo(\'body\');\n
\n
\n
// Those two definitions could be kept in a different file. The jQuery plugin providing an implementation is different than\n
// the places where we use this plugin\n
\n \n
// Make the relation update dialogs as pop-ups.\n // Make the relation update dialogs as pop-ups.\n
jQuery(\'input[value="update..."]\').click(function(event) {\n $(\'input[value="update..."]\').click(function(event) {\n
event.preventDefault();\n event.preventDefault();\n
var self = jQuery(this);\n \n
// Make sure that the dialog is present, and it is empty.\n var self = $(this);\n
if (dialog != null) {\n var form = $(\'form#main_form\');\n
dialog.empty();\n
dialog.dialog(\'destroy\');\n
}\n
dialog = jQuery(\'<div id="dialog" />\').appendTo(\'body\');\n
dialog.dialog({ modal: true,\n
width: jQuery(window).width() * 0.8,\n
height: jQuery(window).height() * 0.8,\n
title: jQuery(\'label\', this.parentNode.parentNode).text()\n
});\n
var form = jQuery(\'form#main_form\');\n
var params = {};\n var params = {};\n
params[self.attr(\'name\')] = self.attr(\'value\');\n params[self.attr(\'name\')] = self.attr(\'value\');\n
load(form.attr(\'action\'), jQuery.param(params) + \'&\' + form.serialize());\n \n
dialog.erp5_popup({\n
dialog: { title: $(\'label\', this.parentNode.parentNode).text() },\n
load: {\n
url: form.attr(\'action\'),\n
params: $.param(params) + \'&\' + form.serialize(),\n
}\n
});\n
});\n });\n
\n \n
// Make the Add gadget dialog work as pop-ups.\n // Make the Add gadget dialog work as pop-ups.\n
jQuery(\'a[id="add-gadgets"]\').click(function(event) {\n $(\'a[id="add-gadgets"]\').click(function(event) {\n
event.preventDefault();\n event.preventDefault();\n
// Make sure that the dialog is present, and it is empty.\n \n
if (dialog != null) {\n dialog.erp5_popup({\n
dialog.empty();\n dialog: { title: $(\'label\', this.parentNode.parentNode).text() },\n
dialog.dialog(\'destroy\');\n load: {\n
}\n url: this.href,\n
dialog = jQuery(\'<div id="dialog" />\').appendTo(\'body\');\n method: $.get,\n
dialog.dialog({ modal: true,\n }\n
width: jQuery(window).width() * 0.8,\n
height: jQuery(window).height() * 0.8,\n
title: jQuery(\'label\', this.parentNode.parentNode).text()\n
});\n });\n
load(this.href, query={}, method_name=\'get\');\n
});\n });\n
\n \n
});\n });\n
......
2010-08-18
* Refactor/simplify in order to extend jQuery with a erp5_popup function that can be re-used in other scripts/projects.
2010-07-01 yo 2010-07-01 yo
* Fix a bug that scripts in every fetched document may be executed. Otherwise, ready callbacks can be executed many times. * Fix a bug that scripts in every fetched document may be executed. Otherwise, ready callbacks can be executed many times.
......
11 12
\ No newline at end of file \ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment