|
| 1 | +# |
| 2 | +# Usage example: new App.Dialog('.dialog', {width: 500}) |
| 3 | +# Now every element that has class "dialog" will launch a new dialog. |
| 4 | +# You must also set href and title to elements to get dialog work properly |
| 5 | +# |
| 6 | +# dialog |
| 7 | +class App.Dialog |
| 8 | + # element to be bound with, jquery ui dialog options, loader class |
| 9 | + constructor: (@element, options, @loader, @errorizers) -> |
| 10 | + |
| 11 | + settings = { |
| 12 | + headerAttribute: 'title' |
| 13 | + refreshDialogOnOpening: false |
| 14 | + } |
| 15 | + $.extend(settings, options); |
| 16 | + |
| 17 | + basedialog = $('<div></div>') |
| 18 | + if !loader |
| 19 | + loader = new App.AjaxLoader.Default() |
| 20 | + |
| 21 | + if !errorizers or !errorizers.length |
| 22 | + @errorizers = [new App.FatalErrorizer.Default] |
| 23 | + else |
| 24 | + @errorizers.push(new App.FatalErrorizer.Default) |
| 25 | + |
| 26 | + self = @ |
| 27 | + $(@element).live('click', (event) -> |
| 28 | + dialog = basedialog.clone() |
| 29 | + loader.start() |
| 30 | + |
| 31 | + settings.title = self.getHeader($(this), settings) |
| 32 | + settings.close = () => |
| 33 | + self.onClose(dialog, settings, this) |
| 34 | + |
| 35 | + if $(this).data('dialog-binded') |
| 36 | + self.reopenDialog($('#'+$(this).data('dialog-binded'))) |
| 37 | + loader.stop() |
| 38 | + else |
| 39 | + $(dialog).load(self.getUri($(this)), (response, status, xhr) => |
| 40 | + loader.stop() |
| 41 | + if !self.checkForErrors(response, status, xhr, dialog) |
| 42 | + self.loadComplete(dialog, this) |
| 43 | + self.bindDialogToElement(dialog, this) |
| 44 | + |
| 45 | + ).dialog(settings) |
| 46 | + return false |
| 47 | + ) |
| 48 | + |
| 49 | + checkForErrors: (response, status, xhr, dialog) => |
| 50 | + if status == "error" |
| 51 | + this.displayErrors($(dialog), {'error':{'xhr': xhr}}) |
| 52 | + else if response == '' or (typeof(response) == 'string' and response.search(/Fatal error:/i) != -1) # you really don't want to get empty string as response |
| 53 | + this.displayErrors($(dialog), response) #errorizer should know what empty or fatal error response means |
| 54 | + else |
| 55 | + return false |
| 56 | + |
| 57 | + return true |
| 58 | + |
| 59 | + # Calls errorizers to show errors |
| 60 | + displayErrors: ($element, response) => |
| 61 | + for errorizer in @errorizers |
| 62 | + if errorizer.errorize $element, response |
| 63 | + return true |
| 64 | + |
| 65 | + loadComplete: (dialog, element) -> |
| 66 | + |
| 67 | + onClose: (dialog, settings, element) -> |
| 68 | + if(settings.refreshDialogOnOpening) #remove old dialogs if we want to load new content |
| 69 | + $(dialog).remove() |
| 70 | + if $(element).data('dialog-binded') |
| 71 | + $(element).removeData('dialog-binded') |
| 72 | + |
| 73 | + getUri: (element) -> |
| 74 | + $(element).attr('href') |
| 75 | + |
| 76 | + getHeader: (element, settings) -> |
| 77 | + $(element).attr(settings.headerAttribute) |
| 78 | + |
| 79 | + bindDialogToElement: (dialog, element) -> |
| 80 | + uniqId = 'dialog-'+ new Date().getTime() |
| 81 | + $(element).data('dialog-binded', uniqId) |
| 82 | + $(dialog).attr('id', uniqId) |
| 83 | + |
| 84 | + reopenDialog: (dialogContent) -> |
| 85 | + dialogContent.closest('.ui-dialog').show() |
| 86 | + |
| 87 | + |
| 88 | +# |
| 89 | +# Usage example: new App.ConfirmDialog('.confirm-dialog', { width: 300, modal: true}) |
| 90 | +# You must also set href, data-href and title to elements to get dialog work properly |
| 91 | +# |
| 92 | +# - data-href should point to your service that provides confirmation message |
| 93 | +# - href should point to place where data is really being deleted |
| 94 | +# - title is used as dialog header |
| 95 | +# |
| 96 | +# dialog |
| 97 | +class App.ConfirmDialog extends App.Dialog |
| 98 | + |
| 99 | + # uses data-href so user can remove objects if if javascript is not being used |
| 100 | + getUri: (element) -> |
| 101 | + $(element).data('href') |
| 102 | + |
| 103 | + loadComplete: (dialog, element) -> |
| 104 | + $confirmBtn = $('[data-id|="confirm-delete"] > [data-id|="confirm"]') |
| 105 | + $confirmBtn.attr('href', $(element).attr('href')) |
| 106 | + |
| 107 | + $('[data-id|="confirm-delete"] > [data-id|="cancel"]').live('click', => |
| 108 | + $(dialog).dialog('close') |
| 109 | + return false |
| 110 | + ) |
| 111 | + |
| 112 | + |
0 commit comments