041.dialogs.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /** This file is part of KCFinder project
  2. *
  3. * @desc Dialog boxes functionality
  4. * @package KCFinder
  5. * @version 3.12
  6. * @author Pavel Tzonkov <sunhater@sunhater.com>
  7. * @copyright 2010-2014 KCFinder Project
  8. * @license http://opensource.org/licenses/GPL-3.0 GPLv3
  9. * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
  10. * @link http://kcfinder.sunhater.com
  11. */
  12. _.alert = function(text, field, options) {
  13. var close = !field
  14. ? function() {}
  15. : ($.isFunction(field)
  16. ? field
  17. : function() { setTimeout(function() {field.focus(); }, 1); }
  18. ),
  19. o = {
  20. close: function() {
  21. close();
  22. if ($(this).hasClass('ui-dialog-content'))
  23. $(this).dialog('destroy').detach();
  24. }
  25. };
  26. $.extend(o, options);
  27. return _.dialog(_.label("Warning"), text.replace("\n", "<br />\n"), o);
  28. };
  29. _.confirm = function(text, callback, options) {
  30. var o = {
  31. buttons: [
  32. {
  33. text: _.label("Yes"),
  34. icons: {primary: "ui-icon-check"},
  35. click: function() {
  36. callback();
  37. $(this).dialog('destroy').detach();
  38. }
  39. },
  40. {
  41. text: _.label("No"),
  42. icons: {primary: "ui-icon-closethick"},
  43. click: function() {
  44. $(this).dialog('destroy').detach();
  45. }
  46. }
  47. ]
  48. };
  49. $.extend(o, options);
  50. return _.dialog(_.label("Confirmation"), text, o);
  51. };
  52. _.dialog = function(title, content, options) {
  53. if (!options) options = {};
  54. var dlg = $('<div></div>');
  55. dlg.hide().attr('title', title).html(content).appendTo('body');
  56. if (dlg.find('form').get(0) && !dlg.find('form [type="submit"]').get(0))
  57. dlg.find('form').append('<button type="submit" style="width:0;height:0;padding:0;margin:0;border:0;visibility:hidden">Submit</button>');
  58. var o = {
  59. resizable: false,
  60. minHeight: false,
  61. modal: true,
  62. width: 351,
  63. buttons: [
  64. {
  65. text: _.label("OK"),
  66. icons: {primary: "ui-icon-check"},
  67. click: function() {
  68. if (typeof options.close != "undefined")
  69. options.close();
  70. if ($(this).hasClass('ui-dialog-content'))
  71. $(this).dialog('destroy').detach();
  72. }
  73. }
  74. ],
  75. close: function() {
  76. if ($(this).hasClass('ui-dialog-content'))
  77. $(this).dialog('destroy').detach();
  78. },
  79. closeText: false,
  80. zindex: 1000000,
  81. alone: false,
  82. blur: false,
  83. legend: false,
  84. nopadding: false,
  85. show: { effect: "fade", duration: 250 },
  86. hide: { effect: "fade", duration: 250 }
  87. };
  88. $.extend(o, options);
  89. if (o.alone)
  90. $('.ui-dialog .ui-dialog-content').dialog('destroy').detach();
  91. dlg.dialog(o);
  92. if (o.nopadding)
  93. dlg.css({padding: 0});
  94. if (o.blur)
  95. dlg.parent().find('.ui-dialog-buttonpane button').first().get(0).blur();
  96. if (o.legend)
  97. dlg.parent().find('.ui-dialog-buttonpane').prepend('<div style="float:left;padding:10px 0 0 10px">' + o.legend + '</div>');
  98. if ($.agent && $.agent.firefox)
  99. dlg.css('overflow-x', "hidden");
  100. return dlg;
  101. };
  102. _.fileNameDialog = function(post, inputName, inputValue, url, labels, callBack, selectAll) {
  103. var html = '<form method="post" action="javascript:;"><input name="' + inputName + '" type="text" /></form>',
  104. submit = function() {
  105. var name = dlg.find('[type="text"]').get(0);
  106. name.value = $.trim(name.value);
  107. if (name.value == "") {
  108. _.alert(_.label(labels.errEmpty), function() {
  109. name.focus();
  110. });
  111. return false;
  112. } else if (/[\/\\]/g.test(name.value)) {
  113. _.alert(_.label(labels.errSlash), function() {
  114. name.focus();
  115. });
  116. return false;
  117. } else if (name.value.substr(0, 1) == ".") {
  118. _.alert(_.label(labels.errDot), function() {
  119. name.focus();
  120. });
  121. return false;
  122. }
  123. post[inputName] = name.value;
  124. $.ajax({
  125. type: "post",
  126. dataType: "json",
  127. url: url,
  128. data: post,
  129. async: false,
  130. success: function(data) {
  131. if (_.check4errors(data, false))
  132. return;
  133. if (callBack) callBack(data);
  134. dlg.dialog("destroy").detach();
  135. },
  136. error: function() {
  137. _.alert(_.label("Unknown error."));
  138. }
  139. });
  140. return false;
  141. },
  142. dlg = _.dialog(_.label(labels.title), html, {
  143. width: 351,
  144. buttons: [
  145. {
  146. text: _.label("OK"),
  147. icons: {primary: "ui-icon-check"},
  148. click: function() {
  149. submit();
  150. }
  151. },
  152. {
  153. text: _.label("Cancel"),
  154. icons: {primary: "ui-icon-closethick"},
  155. click: function() {
  156. $(this).dialog('destroy').detach();
  157. }
  158. }
  159. ]
  160. }),
  161. field = dlg.find('[type="text"]');
  162. field.uniform().attr('value', inputValue).css('width', 310);
  163. dlg.find('form').submit(submit);
  164. if (!selectAll && /^(.+)\.[^\.]+$/ .test(inputValue))
  165. field.selection(0, inputValue.replace(/^(.+)\.[^\.]+$/, "$1").length);
  166. else {
  167. field.get(0).focus();
  168. field.get(0).select();
  169. }
  170. };