// bip sound // http://soundbible.com/mp3/A-Tone-His_Self-1266414414.mp3 (function (factory) { // checking for exports avalible if (typeof module !== 'undefined' && module.exports) { // export Collection module.exports = factory } else { // else add to root variable window['naranja'] = factory } })(function () { function setSideUpAnimation (finalNotification) { setTimeout(function () { var notificaciónHeight = finalNotification .querySelector('.naranja-body-notification') .offsetHeight finalNotification.style.height = notificaciónHeight + 'px' }, 0) } function createText (text) { return document.createTextNode(text) } /** * provide a reusable way to create html * elements * @param tag String – html tag name * @param classes Array – html tag classes */ function createElement (tag, classes) { var $HTMLElement = document.createElement(tag) if ( !!classes ) { classes.forEach(function (className) { $HTMLElement.classList.add(className) }) } return $HTMLElement } var $narjContainer = document.querySelector('.naranja-notification-box') if (!$narjContainer) { $narjContainer = createElement('div', ['naranja-notification-box']) $newNotificationsAdvice = createElement('div', ['naranja-notification-advice']) $newNotificationsAdvice.addEventListener('click', function () { $narjContainer.scrollTop = '0' }) $narjContainer.appendChild($newNotificationsAdvice) document.body.appendChild($narjContainer) } $narjContainer.__proto__.unshifElement = function (node) { this.insertBefore(node, this.childNodes[0]) } $narjContainer.addEventListener('scroll', function (e) { if (e.currentTarget.scrollTop < 20) { $newNotificationsAdvice.classList.remove('active') } }) return { log: function (argm) { this.createNotification('log', argm) }, success: function (argm) { this.createNotification('success', argm) }, warn: function (argm) { this.createNotification('warn', argm) }, error: function (argm) { this.createNotification('error', argm) }, /* * Internal methods for * launch notifications */ createNotification: function (type, argm) { this.type = type this.title = argm.title this.text = argm.text this.icon = (argm.icon === undefined) ? true : argm.icon this.buttons = argm.buttons var $notification = this.$createContainer() var $body = $notification.querySelector('div') this.$notification = $notification this.$body = $body // render icon if exists if (this.icon) { var $iconContainer = createElement('div', [ 'naranja-icon', 'narj-icon-' + type ]) $iconContainer.innerHTML = this.chooseIcon[type] $body.appendChild($iconContainer) } var $title = this.createTitle() var $text = this.createText() var $textAndTitleContainer = createElement('div', [ 'naranja-text-and-title' ]) $textAndTitleContainer.appendChild($title) $textAndTitleContainer.appendChild($text) $body.appendChild($textAndTitleContainer) // render buttons fragment if exists if (this.buttons) { var $buttons = this.createButtons($notification, $body) $body .querySelector('.naranja-text-and-title') .appendChild($buttons) } var $close = createElement('div', [ 'naranja-close-icon' ]) $close.addEventListener('click', (function () { this.closeNotification() }).bind(this)) // var $close = document.createElement('div') // $close.classList.add('naranja-close-icon') $close.innerHTML = this.chooseIcon.close $body.appendChild($close) $narjContainer.unshifElement($notification) setSideUpAnimation($notification) if ($narjContainer.scrollTop > 20) { $newNotificationsAdvice.classList.add('active') $newNotificationsAdvice.innerHTML = this.chooseIcon.newNotification } if (argm.timeout !== 'keep') { setTimeout( (function () { this.closeNotification() }).bind(this), argm.timeout || 5000 ) } }, $createContainer: function () { // generate box for notification var $container = createElement('div', [ 'naranja-notification', 'naranja-' + this.type ]) var $innerContainer = createElement('div', [ 'naranja-body-notification', 'narj-' + this.type ]) $container.appendChild($innerContainer) return $container }, createTitle: function () { var $parragraph = createElement('p', [ 'naranja-title' ]) var $tt = createText(this.title) $parragraph.appendChild($tt) return $parragraph }, createText: function () { var $title = createElement('p', [ 'naranja-parragraph' ]) var $tx = document.createTextNode(this.text) $title.appendChild($tx) return $title }, createButtons: function ($notification, $body) { var $buttonsContainer = createElement('div', [ 'naranja-buttons-container' ]) var self = this this.buttons.forEach(function (button) { var $buttonElement = createElement('button') $buttonElement.appendChild(document.createTextNode(button.text)) $buttonElement.addEventListener('click', function (event) { self.removeNotification = true event.preventClose = function () { self.removeNotification = false } event.closeNotification = function () { self.closeNotification() } button.click(event) if (self.removeNotification) self.closeNotification() }) $buttonsContainer.appendChild($buttonElement) }) return $buttonsContainer }, closeNotification: function () { var self = this if ( !this.elementWasRemoved ) { self.$body.style.opacity = '0' setTimeout(function () { self.$body.style.marginTop = '0px' self.$body.style.marginBottom = '0px' self.$body.style.padding = '0px' self.$notification.style.height = 0 + 'px' self.$notification.style.padding = 0 + 'px' setTimeout(function () { self.$notification .parentNode .removeChild( self.$notification ) }, 600); if ($narjContainer.scrollTop < 20) { $newNotificationsAdvice.classList.remove('active') } }, 150) } this.elementWasRemoved = true }, chooseIcon: { log: '', success: '', warn: '', error: '', close: '', newNotification: '' } } })