tip.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. define(['jquery'], function($) {
  2. var tip = {};
  3. tip.lang = {
  4. "success": "操作成功",
  5. "error": "操作失败",
  6. "exception": "网络异常",
  7. "processing": "处理中..."
  8. };
  9. $('#tip-msgbox').remove();
  10. $("body", top.window.document).append('<div id="tip-msgbox" class="msgbox"></div>');
  11. window.msgbox = $("#tip-msgbox", top.window.document);
  12. tip.confirm = function(msg, callback, cancel_callback) {
  13. msg = msg.replace(/&lt;/g, "<");
  14. msg = msg.replace(/&gt;/g, ">");
  15. myrequire(['jquery.confirm'], function() {
  16. $.confirm({
  17. title: '提示',
  18. content: msg,
  19. confirmButtonClass: 'btn-primary',
  20. cancelButtonClass: 'btn-default',
  21. confirmButton: '确 定',
  22. cancelButton: '取 消',
  23. animation: 'top',
  24. confirm: function() {
  25. if (callback && typeof(callback) == 'function') {
  26. callback()
  27. }
  28. },
  29. cancel: function() {
  30. if (cancel_callback && typeof(cancel_callback) == 'function') {
  31. cancel_callback()
  32. }
  33. }
  34. })
  35. })
  36. }, tip.prompt = function(msg, options, password) {
  37. var callback = null;
  38. var maxlength = null;
  39. var required = false;
  40. var input_type = password ? 'password' : 'text';
  41. if (typeof options == 'function') {
  42. callback = options
  43. } else if (typeof options == 'object') {
  44. maxlength = options.maxlength || null;
  45. callback = options.callback && typeof options.callback == 'function' ? options.callback : null;
  46. required = options.required || false
  47. }
  48. var inputid = 'prompt_' + (+new Date());
  49. var max = maxlength ? " maxlength='" + maxlength + "' " : '';
  50. myrequire(['jquery.confirm'], function() {
  51. $.alert({
  52. title: '提示',
  53. content: "<p>" + msg + "</p><p><input id='" + inputid + "' type='" + input_type + "' class='form-control' name='confirm' placeholder='" + msg + "' " + max + " /></p>",
  54. confirmButtonClass: 'btn-primary',
  55. confirmButton: '确 定',
  56. closeIcon: true,
  57. animation: 'top',
  58. keyboardEnabled: true,
  59. onOpen: function() {
  60. setTimeout(function() {
  61. $('#' + inputid).focus()
  62. }, 100)
  63. },
  64. confirm: function() {
  65. var value = $('#' + inputid).val();
  66. if ($.trim(value) == '' && required) {
  67. $('#' + inputid).focus();
  68. return false
  69. }
  70. if (callback && typeof(callback) == 'function') {
  71. callback(value)
  72. }
  73. }
  74. })
  75. })
  76. }, tip.promptlive = function(msg, options, password) {
  77. var callback = null;
  78. var maxlength = null;
  79. var required = false;
  80. var input_type = password ? 'password' : 'text';
  81. if (typeof options == 'function') {
  82. callback = options
  83. } else if (typeof options == 'object') {
  84. maxlength = options.maxlength || null;
  85. callback = options.callback && typeof options.callback == 'function' ? options.callback : null;
  86. required = options.required || false
  87. }
  88. var inputid = 'prompt_' + (+new Date());
  89. var max = maxlength ? " maxlength='" + maxlength + "' " : '';
  90. myrequire(['jquery.confirm'], function() {
  91. $.alert({
  92. title: '提示',
  93. content: "<p>" + msg + "</p><p><input id='" + inputid + "' type='" + input_type + "' class='form-control' name='confirm' placeholder='' " + max + " /></p>",
  94. confirmButtonClass: 'btn-primary',
  95. confirmButton: '确 定',
  96. closeIcon: true,
  97. animation: 'top',
  98. keyboardEnabled: true,
  99. onOpen: function() {
  100. setTimeout(function() {
  101. $('#' + inputid).focus()
  102. }, 100)
  103. },
  104. confirm: function() {
  105. var value = $('#' + inputid).val();
  106. if ($.trim(value) == '' && required) {
  107. $('#' + inputid).focus();
  108. return false
  109. }
  110. if (callback && typeof(callback) == 'function') {
  111. callback(value);
  112. return false
  113. }
  114. }
  115. })
  116. })
  117. }, tip.alert = function(msg, callback) {
  118. msg = msg.replace(/&lt;/g, "<");
  119. msg = msg.replace(/&gt;/g, ">");
  120. myrequire(['jquery.confirm'], function() {
  121. $.alert({
  122. title: '提示',
  123. content: msg,
  124. confirmButtonClass: 'btn-primary',
  125. confirmButton: '确 定',
  126. animation: 'top',
  127. confirm: function() {
  128. if (callback && typeof(callback) == 'function') {
  129. callback()
  130. }
  131. }
  132. })
  133. })
  134. }, 1;
  135. var Notify = function(element, options) {
  136. this.$element = $(element);
  137. this.options = $.extend({}, $.fn.notify.defaults, options);
  138. var cls = this.options.type ? "msg-" + this.options.type : "msg-success";
  139. var $note = '<span class="msg ' + cls + '">' + this.options.message + '</span>';
  140. this.$element.html($note);
  141. return this
  142. };
  143. Notify.prototype.show = function() {
  144. this.$element.addClass('in'), this.$element.append(this.$note);
  145. var autoClose = this.options.autoClose || true;
  146. if (autoClose) {
  147. var self = this;
  148. setTimeout(function() {
  149. self.close()
  150. }, this.options.delay || 2000)
  151. }
  152. }, Notify.prototype.close = function() {
  153. var self = this;
  154. self.$element.removeClass('in').transitionEnd(function() {
  155. self.$element.empty();
  156. if (self.options.onClosed) {
  157. self.options.onClosed(self)
  158. }
  159. });
  160. if (self.options.onClose) {
  161. self.options.onClose(self)
  162. }
  163. }, $.fn.notify = function(options) {
  164. return new Notify(this, options)
  165. }, $.fn.notify.defaults = {
  166. type: "success",
  167. delay: 3000,
  168. message: ''
  169. }, tip.msgbox = {
  170. show: function(options) {
  171. if (options.url) {
  172. options.url = options.url.replace(/&amp;/ig, "&");
  173. options.onClose = function() {
  174. redirect(options.url)
  175. }
  176. }
  177. if (options.message && options.message.length > 17) {
  178. tip.alert(options.message, function() {
  179. if (options.url) {
  180. redirect(options.url)
  181. }
  182. });
  183. return
  184. }
  185. notify = window.msgbox.notify(options), notify.show()
  186. },
  187. suc: function(msg, url, onClose, onClosed) {
  188. tip.msgbox.show({
  189. delay: 2000,
  190. type: "success",
  191. message: msg,
  192. url: url,
  193. onClose: onClose,
  194. onClosed: onClosed
  195. })
  196. },
  197. err: function(msg, url, onClose, onClosed) {
  198. tip.msgbox.show({
  199. delay: 2000,
  200. type: "error",
  201. message: msg,
  202. url: url,
  203. onClose: onClose,
  204. onClosed: onClosed
  205. })
  206. }
  207. };
  208. window.tip = tip
  209. });