sco.message.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. $selector.html(message);
  30. if (type === undefined || type == $[pluginName].TYPE_ERROR) {
  31. $selector.removeClass($[pluginName].options.okClass).addClass($[pluginName].options.errClass);
  32. } else if (type == $[pluginName].TYPE_OK) {
  33. $selector.removeClass($[pluginName].options.errClass).addClass($[pluginName].options.okClass);
  34. }
  35. $selector.slideDown('fast', function() {
  36. $[pluginName].timeout = setTimeout(function() { $selector.slideUp('fast'); }, $[pluginName].options.delay);
  37. });
  38. };
  39. $.extend($[pluginName], {
  40. options: {
  41. id: 'page_message'
  42. ,okClass: 'page_mess_ok'
  43. ,errClass: 'page_mess_error'
  44. ,animate: true
  45. ,delay: 4000
  46. ,appendTo: 'body' // where should the modal be appended to (default to document.body). Added for unit tests, not really needed in real life.
  47. },
  48. TYPE_ERROR: 1,
  49. TYPE_OK: 2
  50. });
  51. })(jQuery);