;(function($){ if ($.ssdialog !== undefined) return; // // User's API // $.ssdialog = { "VERSION": "0.1.0", "getClass": function(){ return SSDialog; }, "create": function(message){ return new SSDialog(message); }, "alert": function(/* arguments */){ return this.createAlert.apply(this, arguments).open(); }, "createAlert": function(message, options){ var opts = $.extend({ okLabel: "OK", showing: null, hiding: null }, options || {}); var dialog = new SSDialog(message); if (opts.showing) dialog.setShowing(opts.showing); if (opts.hiding) dialog.setHiding(opts.hiding); dialog.addButton("ok", opts.okLabel); dialog.preRender(); return dialog; }, "confirm": function(/* arguments */){ return this.createConfirm.apply(this, arguments).open(); }, "createConfirm": function(message, options){ var opts = $.extend({ okLabel: "OK", cancelLabel: "Cancel", showing: null, hiding: null }, options || {}); var dialog = new SSDialog(message); if (opts.showing) dialog.setShowing(opts.showing); if (opts.hiding) dialog.setHiding(opts.hiding); dialog.addButton("cancel", opts.cancelLabel); dialog.addButton("ok", opts.okLabel); dialog.preRender(); return dialog; } }; // // SSDialog Class // function SSDialog (/* arguments */) { this._initialize.apply(this, arguments); } // Class Properties $.extend(SSDialog, { CSS_CLASS_NAME_PREFIX: "ssdialog", _isJQuery: function(obj){ return obj instanceof $ && obj.jquery !== undefined; }, // e.g. ({x:1, y:2}) -> ["x", "y"] _keys: function(obj){ var k, keys = []; for (k in obj) { if (obj.hasOwnProperty(k)) keys.push(k); } return keys; }, // e.g. ("foo", "bar") -> "ssdialog-foo-bar" _createClassName: function(/* arguments */){ var classNames = Array.prototype.slice.apply(arguments); classNames.unshift(this.CSS_CLASS_NAME_PREFIX); return classNames.join("-"); }, // Author: https://github.com/epeli/underscore.string _escapeHTML: function(str) { if (str === null) return ''; var escapeChars = { "<": "lt", ">": "gt", "\"": "quot", "&": "amp", "'": "apos" }; return String(str).replace(/[&<>"']/g, function(m){ return '&' + escapeChars[m] + ';'; }); }, _nl2br: function(str){ return str.replace(/(?:\r\n|\n|\r)/g, '
'); } }); // Instance Properties $.extend(SSDialog.prototype, { _initialize: function(message){ // string or jQuery object this._message = message; this._buttons = {}; // Resolve at close dialog this._deferred = $.Deferred(); this.$dialog = null; this.$cover = null; // Animation of showing dialog, default is no-animation this._showing = function(){ this.$cover.show(); this.$dialog.show(); }; // Animation of hiding dialog, default is no-animation: // `buttonId` is clicked button ID. // It is necessary to return deferred object. this._hiding = function(buttonId){ this.$dialog.remove(); this.$cover.remove(); return $.Deferred().resolve(); }; }, // buttonId is string // label is string // callback is function or default=undefined addButton: function(buttonId, label, callback){ callback = callback || null; this._buttons[buttonId] = { buttonId: buttonId, label: label, callback: callback, sortOrder: this._getButtonCount() }; }, setShowing: function(v){ this._showing = v; }, setHiding: function(v){ this._hiding = v; }, _getButtonCount: function(){ return SSDialog._keys(this._buttons).length; }, // // For example, dialog is an HTML like this: // //
//
// Are you OK? //
// //
// _createElements: function(){ var $dialog = $('
').addClass(SSDialog._createClassName()); var $message = $('
').addClass(SSDialog._createClassName("message")); var $buttons = $('