diff --git a/components/ionPopup/ionPopup.html b/components/ionPopup/ionPopup.html index 3854826..ff242c2 100644 --- a/components/ionPopup/ionPopup.html +++ b/components/ionPopup/ionPopup.html @@ -1,22 +1,28 @@ diff --git a/components/ionPopup/ionPopup.js b/components/ionPopup/ionPopup.js index f729b21..504a0cb 100644 --- a/components/ionPopup/ionPopup.js +++ b/components/ionPopup/ionPopup.js @@ -1,21 +1,169 @@ IonPopup = { - show: function () { + show: function (options) { + this.template = Template.ionPopup; + this.buttons = []; + var innerTemplate; + for (var i = 0; i < options.buttons.length; i++) { + var button = options.buttons[i]; + this.buttons.push({ + text: button.text, + type: button.type, + index: i, + onTap: button.onTap + }); + } + + //Figure out if a template or just a html string was passed + if (options.templateName) { + innerTemplate = Template[options.templateName].renderFunction().value; + } else { + innerTemplate = '' + options.template + ''; + } + + var data = { + title: options.title, + subTitle: options.subTitle, + buttons: this.buttons, + template: innerTemplate + }; + + this.view = Blaze.renderWithData(this.template, data, $('.ionic-body').get(0)); + $('body').addClass('popup-open'); + + var $backdrop = $(this.view.firstNode()); + $backdrop.addClass('visible active'); + var $popup = $backdrop.find('.popup-container'); + $popup.addClass('popup-showing active'); }, - hide: function () { - + alert: function (options) { + IonPopup.show({ + title: options.title, + subTitle: options.subTitle, + template: options.template, + templateName: options.templateName, + buttons: [ + { + text: options.okText ? options.okText : 'Ok', + type: options.okType ? options.okType : 'button-positive', + onTap: function(event, template) { + if (options.onOk) options.onOk(event, template); + return true; + } + } + ] + }); }, - alert: function () { - + confirm: function (options) { + IonPopup.show({ + title: options.title, + subTitle: options.subTitle, + template: options.template, + templateName: options.templateName, + buttons: [ + { + text: options.okText ? options.okText : 'Ok', + type: options.okType ? options.okType : 'button-positive', + onTap: function (event, template) { + if (options.onOk) options.onOk(event, template); + return true; + } + }, + { + text: options.cancelText ? options.cancelText : 'Cancel', + type: options.cancelType ? options.cancelType : 'button-default', + onTap: function (event, template) { + if (options.onCancel) options.onCancel(event, template); + return true; + } + } + ] + }); }, - confirm: function () { + prompt: function (options) { + if (options.templateName) { + template = Template[options.templateName].renderFunction().value; + } else { + template = '' + options.template + ''; + } + + template += ''; + + IonPopup.show({ + title: options.title, + subTitle: options.subTitle, + template: template, + buttons: [ + { + text: options.okText ? options.okText : 'Ok', + type: options.okType ? options.okType : 'button-positive', + onTap: function (event, template) { + var inputVal = $(template.firstNode).find('[name=prompt]').val(); + if (options.onOk) options.onOk(event, inputVal); + return true; + } + }, + { + text: options.cancelText ? options.cancelText : 'Cancel', + type: options.cancelType ? options.cancelType : 'button-default', + onTap: function (event, template) { + if (options.onCancel) options.onCancel(event, template); + return true; + } + } + ] + }); }, - prompt: function () { + close: function () { + var $backdrop = $(this.view.firstNode()); + var $popup = $backdrop.find('.popup-container'); + $popup.addClass('popup-hidden').removeClass('active'); + setTimeout(function () { + $('body').removeClass('popup-open'); + Blaze.remove(this.view); + }.bind(this), 100); + }, + + buttonClicked: function (index, event, template) { + var callback = this.buttons[index].onTap; + if(callback){ + if (callback(event, template) === true) { + IonPopup.close(); + } + } } }; + +Template.ionPopup.rendered = function () { + $(window).on('keyup.ionPopup', function(event) { + if (event.which == 27) { + IonPopup.close(); + } + }); +}; + +Template.ionPopup.destroyed = function () { + $(window).off('keyup.ionPopup'); +}; + +Template.ionPopup.events({ + // Handle clicking the backdrop + 'click': function (event, template) { + if ($(event.target).hasClass('popup-container')) { + IonPopup.close(); + } + }, + + 'click [data-index]': function (event, template) { + var index = $(event.target).data('index'); + IonPopup.buttonClicked(index, event, template); + } + +});