+ return $elem;
+ };
+
+ /*
+ * Dialogs
+ */
+ function GenericDialog(element) {
+ if(!element) return;
+
+ var self = this;
+
+ self.$elem = $(element);
+
+ if(!self.$elem.attr('data-ui-initialized')) {
+ console.log("Initializing dialog", this);
+ self.initialize();
+ self.$elem.attr('data-ui-initialized', true);
+ }
+
+ self.show();
+ };
+
+ GenericDialog.prototype = {
+
+ /*
+ * Steps to follow when the dialog in first loaded on page.
+ */
+ initialize: function(){
+ var self = this;
+
+ /* bind buttons */
+ $('button[data-ui-action]', self.$elem).click(function(event) {
+ event.preventDefault();
+
+ var action = $(this).attr('data-ui-action');
+ console.log("Button pressed, action: ", action);
+
+ try {
+ self[action + "Action"].call(self);
+ } catch(e) {
+ console.log("Action failed:", e);
+ // always hide on cancel
+ if(action == 'cancel')
+ self.hide();
+ }
+ });
+ },
+
+ /*
+ * Prepare dialog for user. Clear any unnessary data.
+ */
+ show: function() {
+ $.blockUI({
+ message: this.$elem,
+ css: {
+ 'top': '25%',
+ 'left': '25%',
+ 'width': '50%'
+ }
+ });
+ },
+
+ hide: function(){
+ $.unblockUI();
+ },
+
+ cancelAction: function() {
+ this.hide();
+ },
+
+ doneAction: function() {
+ this.hide();
+ },
+
+ clearForm: function() {
+ $("*[data-ui-error-for]", this.$elem).text('');
+ },
+
+ reportErrors: function(errors) {
+ var global = $("*[data-ui-error-for='__all__']", this.$elem);
+ var unassigned = [];
+
+ for (var field_name in errors)
+ {
+ var span = $("*[data-ui-error-for='"+field_name+"']", this.$elem);
+
+ if(!span.length) {
+ unassigned.push(field_name);
+ continue;
+ }
+
+ span.text(errors[field_name].join(' '));
+ }
+
+ if(unassigned.length > 0)
+ global.text( global.text() + 'W formularzu wystąpiły błędy');
+ }
+ };
+
+ $.wiki.cls.GenericDialog = GenericDialog;
+
+ $.wiki.showDialog = function(selector, options) {
+ var elem = $(selector);
+
+ if(elem.length != 1) {
+ console.log("Failed to show dialog:", selector, elem);
+ return false;
+ }
+
+ try {
+ var klass = elem.attr('data-ui-jsclass');
+ return new $.wiki.cls[klass](elem, options);
+ } catch(e) {
+ console.log("Failed to show dialog", selector, klass, e);
+ return false;
+ }