// ========================================================================== // Project: SproutCore - JavaScript Application Framework // Copyright: ©2006-2011 Strobe Inc. and contributors. // Portions ©2008-2011 Apple Inc. All rights reserved. // License: Licensed under MIT license (see license.js) // ========================================================================== sc_require('panes/panel'); sc_require('views/button'); /** Passed to delegate when alert pane is dismissed by pressing button 1 @static @type String @default 'button1' */ SC.BUTTON1_STATUS = 'button1'; /** Passed to delegate when alert pane is dismissed by pressing button 2 @static @type String @default 'button2' */ SC.BUTTON2_STATUS = 'button2'; /** Passed to delegate when alert pane is dismissed by pressing button 3 @static @type String @default 'button3' */ SC.BUTTON3_STATUS = 'button3'; /** @class Displays a preformatted modal alert pane. Alert panes are a simple way to provide modal messaging that otherwise blocks the user's interaction with your application. Alert panes are useful for showing important error messages and confirmation dialogs. They provide a better user experience than using the OS-level alert dialogs. ## Displaying an Alert Pane The easiest way to display an alert pane is to use one of the various class methods defined on `SC.AlertPane`, passing the message and an optional detailed description and caption. There are four variations of this method can you can invoke: - `warn()` -- displays an alert pane with a warning icon to the left. - `error()` -- displays an alert with an error icon to the left - `info()` -- displays an alert with an info icon to the left - `plain()` -- displays an alert w/o any icon - `show()` -- displays an alert with a customizable icon to the left In addition to passing a message, description and caption, you can also customize the title of the button 1 (OK) and add an optional button 2 and 3 (Cancel or Extra). - button1 -- 1st button from the right. default:OK - button2 -- 2nd button from the right. Optional. Could be Cancel or 2nd action. - button3 -- 1st button from the left. Optional. Could be Cancel or alternative option. Additionally, you can define a delegate object. This delegate's `alertPaneDidDismiss()` method will be called when the pane is dismissed, passing the pane instance and a key indicating which button was pressed. ## Examples Show a simple AlertPane with an OK button: SC.AlertPane.warn({ message: "Could not load calendar", description: "Your internet connection may be unavailable or our servers may be down.", caption: "Try again in a few minutes." }); Show an AlertPane with a customized OK title (title will be 'Try Again') and custom action: SC.AlertPane.warn({ message: "Could not load calendar", description: "Your internet connection may be unavailable or our servers may be down.", caption: "Try again in a few minutes.", buttons: [ { title: "Try Again" } ] }); Show an AlertPane with a custom OK, a Cancel button and an Extra button, each with custom titles. Also, pass a delegate that will be invoked when the user's dismisses the dialog. MyApp.calendarController = SC.Object.create({ alertPaneDidDismiss: function(pane, status) { switch(status) { case SC.BUTTON1_STATUS: this.tryAgain(); break; case SC.BUTTON2_STATUS: // do nothing break; case SC.BUTTON3_STATUS: this.showMoreInfo(); break; } }, ... }); SC.AlertPane.warn({ message: "Could not load calendar", description: "Your internet connection may be unavailable or our servers may be down.", caption: "Try again in a few minutes.", delegate: MyApp.calendarController, buttons: [ { title: "Try Again" }, { title: "Cancel" }, { title: "More Info…" } ] }); Instead of using the delegate pattern above, you can also specify a target and an action, similar to SC.ButtonView. SC.AlertPane.warn({ message: "Could not load calendar", description: "Your internet connection may be unavailable or our servers may be down.", caption: "Try again in a few minutes.", buttons: [ { title: "OK", action: "didClickOK", target: MyApp.calendarController } ] }); Also note that in addition to passing the action as a string of the method name that will be invoked, you can also give a function reference as the action. @extends SC.PanelPane @since SproutCore 1.0 */ SC.AlertPane = SC.PanelPane.extend( /** @scope SC.AlertPane.prototype */{ /** @type Array @default ['sc-alert'] @see SC.View#classNames */ classNames: ['sc-alert'], /** The WAI-ARIA role for alert pane. @type String @default 'alertdialog' @constant */ ariaRole: 'alertdialog', /** If defined, the delegate is notified when the pane is dismissed. If you have set specific button actions, they will be called on the delegate object The method to be called on your delegate will be: alertPaneDidDismiss: function(pane, status) {} The status will be one of `SC.BUTTON1_STATUS`, `SC.BUTTON2_STATUS` or `SC.BUTTON3_STATUS` depending on which button was clicked. @type Object @default null */ delegate: null, /** The icon URL or class name. If you do not set this, an alert icon will be shown instead. @type String @default 'sc-icon-alert-48' */ icon: 'sc-icon-alert-48', /** The primary message to display. This message will appear in large bold type at the top of the alert. @type String @default "" */ message: "", /** The ARIA label for the alert is the message, by default. @field {String} */ ariaLabel: function() { return this.get('message'); }.property('message').cacheable(), /** An optional detailed description. Use this string to provide further explanation of the condition and, optionally, ways the user can resolve the problem. @type String @default "" */ description: "", /** An escaped and formatted version of the description property. @field @type String @observes description */ displayDescription: function() { var desc = this.get('description'); if (!desc || desc.length === 0) return desc ; desc = SC.RenderContext.escapeHTML(desc); // remove HTML return '
' + desc.split('\n').join('
') + '
'; }.property('description').cacheable(), /** An optional detailed caption. Use this string to provide further fine print explanation of the condition and, optionally, ways the user can resolve the problem. @type String @default "" */ caption: "", /** An escaped and formatted version of the caption property. @field @type String @observes caption */ displayCaption: function() { var caption = this.get('caption'); if (!caption || caption.length === 0) return caption ; caption = SC.RenderContext.escapeHTML(caption); // remove HTML return '' + caption.split('\n').join('
') + '
'; }.property('caption').cacheable(), /** The button view for button 1 (OK). @type SC.ButtonView */ button1: SC.outlet('contentView.childViews.1.childViews.1'), /** The button view for the button 2 (Cancel). @type SC.ButtonView */ button2: SC.outlet('contentView.childViews.1.childViews.0'), /** The button view for the button 3 (Extra). @type SC.ButtonView */ button3: SC.outlet('contentView.childViews.2.childViews.0'), /** The view for the button 3 (Extra) wrapper. @type SC.View */ buttonThreeWrapper: SC.outlet('contentView.childViews.2'), /** @type Hash @default { top : 0.3, centerX: 0, width: 500 } @see SC.View#layout */ layout: { top : 0.3, centerX: 0, width: 500 }, /** @private - internal view that is actually displayed */ contentView: SC.View.extend({ useStaticLayout: YES, layout: { left: 0, right: 0, top: 0, height: "auto" }, childViews: [ SC.View.extend(SC.StaticLayout, { classNames: ['info'], /** @private */ render: function(context, firstTime) { var pane = this.get('pane'); if(pane.get('icon') == 'blank') context.addClass('plain'); context.push('