###* * Ultimate Flash 0.7.1 - Ruby on Rails oriented jQuery plugin for smart notifications * Copyright 2011-2012 Karpunin Dmitry (KODer) / Evrone.com * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php * ### ###* * $.fn.ultimateFlash() invoke Ultimate Flash functionality at first call on jQuery object * for the first element in the set of matched elements. * Subsequent calls forwarding on view methods or return view property. * If last argument {Boolean} true, then returns {Flash}. * @usage * standart actions: * construction .ultimateFlash([Object options = {}]) : {jQuery} jContainer * updateOptions .pluginName({Object} options) : {Object} view options * get options .pluginName('options') : {Object} view options * show .ultimateFlash('show', String type, String text) : {jQuery} jFlash | {Boolean} false * notice .ultimateFlash('notice', String text) : {jQuery} jFlash | {Boolean} false * alert .ultimateFlash('alert', String text) : {jQuery} jFlash | {Boolean} false * extended actions: * auto .ultimateFlash('auto', {ArrayOrObject} obj) : {Array} ajFlashes | {Boolean} false * ajaxSuccess .ultimateFlash('ajaxSuccess'[, Arguments successArgs = []]) * ajaxError .ultimateFlash('ajaxError'[, String text = translations.defaultErrorText][, Arguments errorArgs = []]) ### # TODO improve English # TODO jGrowl features #= require ultimate/jquery-plugin-class #= require ultimate/jquery-plugin-adapter Ultimate.Plugins ||= {} class Ultimate.Plugins.Flash extends Ultimate.Plugin el: '.l-page__flashes' @defaultLocales = en: defaultErrorText: 'Error' defaultThrownError: 'server connection error' formFieldsError: 'Form filled with errors' ru: defaultErrorText: 'Ошибка' defaultThrownError: 'ошибка соединения с сервером' formFieldsError: 'Форма заполнена с ошибками' flashClass: 'flash' # css-class of message container showAjaxErrors: true # catch global jQuery.ajaxErrors(), try detect message and show it showAjaxSuccesses: true # catch global jQuery.ajaxSuccessess(), try detect message and show it preventUndefinedResponses: true # prevent error responses with status code < 100, often 0 detectFormErrors: true # can be function (parsedJSON) detectPlainTextMaxLength: 200 # if response has plain text and its length fits, show it (-1 for disable) productionMode: true slideTime: 200 # show and hide animate duration showTime: 3600 # base time for show flash message showTimePerChar: 30 # show time per char of flash message hideOnClick: true # click on notice fire hide() removeOnHide: true # remove notice DOM-element on hide forceAddDotsAfterLastWord: false forceRemoveDotsAfterLastWord: false regExpLastWordWithoutDot: /[\wа-яёА-ЯЁ]{3,}$/ regExpLastWordWithDot: /([\wа-яёА-ЯЁ]{3,})\.$/ events: -> _events = {} _events["click .#{@flashClass}:not(:animated)"] = 'closeFlashClick' _events initialize: (options) -> # init flashes come from server in page @jFlashes().each (index, flash) => jFlash = $(flash) jFlash.html @_prepareText(jFlash.html(), jFlash) @_setTimeout jFlash # binding hook ajaxError handler @$el.ajaxError => if @showAjaxErrors a = @_ajaxParseArguments(arguments) @ajaxError a.data, a.jqXHR # binding hook ajaxSuccess handler @$el.ajaxSuccess => if @showAjaxSuccesses a = @_ajaxParseArguments(arguments) @ajaxSuccess a.data, a.jqXHR # delegate event for hide on click closeFlashClick: (event) -> jFlash = $(event.currentTarget) if @_getOptionOverFlash('hideOnClick', jFlash) @hide jFlash false jFlashes: (filterSelector) -> _jFlashes = @$(".#{@flashClass}") if filterSelector then _jFlashes.filter(filterSelector) else _jFlashes _getOptionOverFlash: (optionName, jFlashOrOptions = {}) -> option = if jFlashOrOptions instanceof jQuery then jFlashOrOptions.data(optionName) else jFlashOrOptions[optionName] option ? @[optionName] _prepareText: (text, jFlashOrOptions) -> text = _.string.clean(text) # Add dot after last word (if word has minimum 3 characters) if @_getOptionOverFlash('forceAddDotsAfterLastWord', jFlashOrOptions) and @_getOptionOverFlash('regExpLastWordWithoutDot', jFlashOrOptions).test(text) text += '.' # Remove dot after last word (if word has minimum 3 characters) if @_getOptionOverFlash('forceRemoveDotsAfterLastWord', jFlashOrOptions) text = text.replace(@_getOptionOverFlash('regExpLastWordWithDot', jFlashOrOptions), '$1') text _setTimeout: (jFlash, timeout) -> timeout ?= @_getOptionOverFlash('showTime', jFlash) + jFlash.text().length * @_getOptionOverFlash('showTimePerChar', jFlash) if timeout jFlash.data 'timeoutId', setTimeout => jFlash.removeData 'timeoutId' @hide jFlash , timeout hide: (jFlashes = @jFlashes()) -> jFlashes.each (index, element) => jFlash = $(element) clearTimeout jFlash.data('timeoutId') jFlash.slideUp @_getOptionOverFlash('slideTime', jFlash), => jFlash.remove() if @_getOptionOverFlash('removeOnHide', jFlash) show: (type, text, timeout = null, perFlashOptions = null) -> text = @_prepareText(text, perFlashOptions) return false if not _.isString(text) or _.string.isBlank(text) jFlash = $("
(.+?)<\/pre>/) thrownError = raiseMatches[1] else # try detect short text message as error if not _.string.isBlank(jqXHR.responseText) and jqXHR.responseText.length <= @detectPlainTextMaxLength thrownError = jqXHR.responseText else if _.string.isBlank(thrownError) thrownError = @t('defaultThrownError') text += ': ' if text text += "#{thrownError} [#{jqXHR.status}]" @alert(text) Ultimate.createJQueryPlugin 'ultimateFlash', Ultimate.Plugins.Flash