sco.message.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* ==========================================================
  2. * sco.message.js
  3. * http://github.com/terebentina/sco.js
  4. * ==========================================================
  5. * Copyright 2013 Dan Caragea.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ========================================================== */
  19. /*jshint laxcomma:true, sub:true, browser:true, jquery:true, eqeqeq: false */
  20. ;(function($, undefined) {
  21. "use strict";
  22. var pluginName = 'scojs_message';
  23. $[pluginName] = function(message, type) {
  24. clearTimeout($[pluginName].timeout);
  25. var $selector = $('#' + $[pluginName].options.id);
  26. if (!$selector.length) {
  27. $selector = $('<div/>', {id: $[pluginName].options.id}).appendTo($[pluginName].options.appendTo);
  28. }
  29. if ($[pluginName].options.animate) {
  30. $selector.addClass('page_mess_animate');
  31. } else {
  32. $selector.removeClass('page_mess_animate');
  33. }
  34. $selector.html(message);
  35. if (type === undefined || type == $[pluginName].TYPE_ERROR) {
  36. $selector.removeClass($[pluginName].options.okClass).addClass($[pluginName].options.errClass);
  37. } else if (type == $[pluginName].TYPE_OK) {
  38. $selector.removeClass($[pluginName].options.errClass).addClass($[pluginName].options.okClass);
  39. }
  40. $selector.slideDown('fast', function() {
  41. $[pluginName].timeout = setTimeout(function() { $selector.slideUp('fast'); }, $[pluginName].options.delay);
  42. });
  43. };
  44. $.extend($[pluginName], {
  45. options: {
  46. id: 'page_message'
  47. ,okClass: 'page_mess_ok'
  48. ,errClass: 'page_mess_error'
  49. ,animate: true
  50. ,delay: 4000
  51. ,appendTo: 'body' // where should the modal be appended to (default to document.body). Added for unit tests, not really needed in real life.
  52. },
  53. TYPE_ERROR: 1,
  54. TYPE_OK: 2
  55. });
  56. })(jQuery);