PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/application/protected/extensions/widgets/editor/source/kcfinder/js/browser/misc.js

https://bitbucket.org/dinhtrung/yiicorecms/
JavaScript | 381 lines | 344 code | 27 blank | 10 comment | 75 complexity | 4dfeb5c5d07b0b014a1734b3558b9006 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, CC0-1.0, BSD-2-Clause, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /** This file is part of KCFinder project
  3. *
  4. * @desc Miscellaneous functionality
  5. * @package KCFinder
  6. * @version 2.41
  7. * @author Pavel Tzonkov <pavelc@users.sourceforge.net>
  8. * @copyright 2010, 2011 KCFinder Project
  9. * @license http://www.opensource.org/licenses/gpl-2.0.php GPLv2
  10. * @license http://www.opensource.org/licenses/lgpl-2.1.php LGPLv2
  11. * @link http://kcfinder.sunhater.com
  12. */?>
  13. browser.drag = function(ev, dd) {
  14. var top = dd.offsetY,
  15. left = dd.offsetX;
  16. if (top < 0) top = 0;
  17. if (left < 0) left = 0;
  18. if (top + $(this).outerHeight() > $(window).height())
  19. top = $(window).height() - $(this).outerHeight();
  20. if (left + $(this).outerWidth() > $(window).width())
  21. left = $(window).width() - $(this).outerWidth();
  22. $(this).css({
  23. top: top,
  24. left: left
  25. });
  26. };
  27. browser.showDialog = function(e) {
  28. $('#dialog').css({left: 0, top: 0});
  29. this.shadow();
  30. if ($('#dialog div.box') && !$('#dialog div.title').get(0)) {
  31. var html = $('#dialog div.box').html();
  32. var title = $('#dialog').data('title') ? $('#dialog').data('title') : "";
  33. html = '<div class="title"><span class="close"></span>' + title + '</div>' + html;
  34. $('#dialog div.box').html(html);
  35. $('#dialog div.title span.close').mousedown(function() {
  36. $(this).addClass('clicked');
  37. });
  38. $('#dialog div.title span.close').mouseup(function() {
  39. $(this).removeClass('clicked');
  40. });
  41. $('#dialog div.title span.close').click(function() {
  42. browser.hideDialog();
  43. browser.hideAlert();
  44. });
  45. }
  46. $('#dialog').drag(browser.drag, {handle: '#dialog div.title'});
  47. $('#dialog').css('display', 'block');
  48. if (e) {
  49. var left = e.pageX - parseInt($('#dialog').outerWidth() / 2);
  50. var top = e.pageY - parseInt($('#dialog').outerHeight() / 2);
  51. if (left < 0) left = 0;
  52. if (top < 0) top = 0;
  53. if (($('#dialog').outerWidth() + left) > $(window).width())
  54. left = $(window).width() - $('#dialog').outerWidth();
  55. if (($('#dialog').outerHeight() + top) > $(window).height())
  56. top = $(window).height() - $('#dialog').outerHeight();
  57. $('#dialog').css({
  58. left: left + 'px',
  59. top: top + 'px'
  60. });
  61. } else
  62. $('#dialog').css({
  63. left: parseInt(($(window).width() - $('#dialog').outerWidth()) / 2) + 'px',
  64. top: parseInt(($(window).height() - $('#dialog').outerHeight()) / 2) + 'px'
  65. });
  66. $(document).unbind('keydown');
  67. $(document).keydown(function(e) {
  68. if (e.keyCode == 27)
  69. browser.hideDialog();
  70. });
  71. };
  72. browser.hideDialog = function() {
  73. this.unshadow();
  74. if ($('#clipboard').hasClass('selected'))
  75. $('#clipboard').removeClass('selected');
  76. $('#dialog').css('display', 'none');
  77. $('div.folder > a > span.folder').removeClass('context');
  78. $('#dialog').html('');
  79. $('#dialog').data('title', null);
  80. $('#dialog').unbind();
  81. $('#dialog').click(function() {
  82. return false;
  83. });
  84. $(document).unbind('keydown');
  85. $(document).keydown(function(e) {
  86. return !browser.selectAll(e);
  87. });
  88. browser.hideAlert();
  89. };
  90. browser.showAlert = function(shadow) {
  91. $('#alert').css({left: 0, top: 0});
  92. if (typeof shadow == 'undefined')
  93. shadow = true;
  94. if (shadow)
  95. this.shadow();
  96. var left = parseInt(($(window).width() - $('#alert').outerWidth()) / 2),
  97. top = parseInt(($(window).height() - $('#alert').outerHeight()) / 2);
  98. var wheight = $(window).height();
  99. if (top < 0)
  100. top = 0;
  101. $('#alert').css({
  102. left: left + 'px',
  103. top: top + 'px',
  104. display: 'block'
  105. });
  106. if ($('#alert').outerHeight() > wheight) {
  107. $('#alert div.message').css({
  108. height: wheight - $('#alert div.title').outerHeight() - $('#alert div.ok').outerHeight() - 20 + 'px'
  109. });
  110. }
  111. $(document).unbind('keydown');
  112. $(document).keydown(function(e) {
  113. if (e.keyCode == 27) {
  114. browser.hideDialog();
  115. browser.hideAlert();
  116. $(document).unbind('keydown');
  117. $(document).keydown(function(e) {
  118. return !browser.selectAll(e);
  119. });
  120. }
  121. });
  122. };
  123. browser.hideAlert = function(shadow) {
  124. if (typeof shadow == 'undefined')
  125. shadow = true;
  126. if (shadow)
  127. this.unshadow();
  128. $('#alert').css('display', 'none');
  129. $('#alert').html('');
  130. $('#alert').data('title', null);
  131. };
  132. browser.alert = function(msg, shadow) {
  133. msg = msg.replace(/\r?\n/g, "<br />");
  134. var title = $('#alert').data('title') ? $('#alert').data('title') : browser.label("Attention");
  135. $('#alert').html('<div class="title"><span class="close"></span>' + title + '</div><div class="message">' + msg + '</div><div class="ok"><button>' + browser.label("OK") + '</button></div>');
  136. $('#alert div.ok button').click(function() {
  137. browser.hideAlert(shadow);
  138. });
  139. $('#alert div.title span.close').mousedown(function() {
  140. $(this).addClass('clicked');
  141. });
  142. $('#alert div.title span.close').mouseup(function() {
  143. $(this).removeClass('clicked');
  144. });
  145. $('#alert div.title span.close').click(function() {
  146. browser.hideAlert(shadow);
  147. });
  148. $('#alert').drag(browser.drag, {handle: "#alert div.title"});
  149. browser.showAlert(shadow);
  150. };
  151. browser.confirm = function(question, callBack) {
  152. $('#dialog').data('title', browser.label("Question"));
  153. $('#dialog').html('<div class="box"><div class="question">' + browser.label(question) + '<div class="buttons"><button>' + browser.label("No") + '</button> <button>' + browser.label("Yes") + '</button></div></div></div>');
  154. browser.showDialog();
  155. $('#dialog div.buttons button').first().click(function() {
  156. browser.hideDialog();
  157. });
  158. $('#dialog div.buttons button').last().click(function() {
  159. if (callBack)
  160. callBack(function() {
  161. browser.hideDialog();
  162. });
  163. else
  164. browser.hideDialog();
  165. });
  166. $('#dialog div.buttons button').get(1).focus();
  167. };
  168. browser.shadow = function() {
  169. $('#shadow').css('display', 'block');
  170. };
  171. browser.unshadow = function() {
  172. $('#shadow').css('display', 'none');
  173. };
  174. browser.showMenu = function(e) {
  175. var left = e.pageX;
  176. var top = e.pageY;
  177. if (($('#dialog').outerWidth() + left) > $(window).width())
  178. left = $(window).width() - $('#dialog').outerWidth();
  179. if (($('#dialog').outerHeight() + top) > $(window).height())
  180. top = $(window).height() - $('#dialog').outerHeight();
  181. $('#dialog').css({
  182. left: left + 'px',
  183. top: top + 'px',
  184. display: 'none'
  185. });
  186. $('#dialog').fadeIn();
  187. };
  188. browser.fileNameDialog = function(e, post, inputName, inputValue, url, labels, callBack, selectAll) {
  189. var html = '<form method="post" action="javascript:;">' +
  190. '<div class="box">' +
  191. '<input name="' + inputName + '" type="text" /><br />' +
  192. '<div style="text-align:right">' +
  193. '<input type="submit" value="' + _.htmlValue(this.label("OK")) + '" /> ' +
  194. '<input type="button" value="' + _.htmlValue(this.label("Cancel")) + '" onclick="browser.hideDialog(); browser.hideAlert(); return false" />' +
  195. '</div></div></form>';
  196. $('#dialog').html(html);
  197. $('#dialog').data('title', this.label(labels.title));
  198. $('#dialog input[name="' + inputName + '"]').attr('value', inputValue);
  199. $('#dialog').unbind();
  200. $('#dialog').click(function() {
  201. return false;
  202. });
  203. $('#dialog form').submit(function() {
  204. var name = this.elements[0];
  205. name.value = $.trim(name.value);
  206. if (name.value == '') {
  207. browser.alert(browser.label(labels.errEmpty), false);
  208. name.focus();
  209. return;
  210. } else if (/[\/\\]/g.test(name.value)) {
  211. browser.alert(browser.label(labels.errSlash), false);
  212. name.focus();
  213. return;
  214. } else if (name.value.substr(0, 1) == ".") {
  215. browser.alert(browser.label(labels.errDot), false);
  216. name.focus();
  217. return;
  218. }
  219. eval('post.' + inputName + ' = name.value;');
  220. $.ajax({
  221. type: 'POST',
  222. dataType: 'json',
  223. url: url,
  224. data: post,
  225. async: false,
  226. success: function(data) {
  227. if (browser.check4errors(data, false))
  228. return;
  229. if (callBack) callBack(data);
  230. browser.hideDialog();
  231. },
  232. error: function() {
  233. browser.alert(browser.label("Unknown error."), false);
  234. }
  235. });
  236. return false;
  237. });
  238. browser.showDialog(e);
  239. $('#dialog').css('display', 'block');
  240. $('#dialog input[type="submit"]').click(function() {
  241. return $('#dialog form').submit();
  242. });
  243. var field = $('#dialog input[type="text"]');
  244. var value = field.attr('value');
  245. if (!selectAll && /^(.+)\.[^\.]+$/ .test(value)) {
  246. value = value.replace(/^(.+)\.[^\.]+$/, "$1");
  247. _.selection(field.get(0), 0, value.length);
  248. } else {
  249. field.get(0).focus();
  250. field.get(0).select();
  251. }
  252. };
  253. browser.orderFiles = function(callBack, selected) {
  254. var order = _.kuki.get('order');
  255. var desc = (_.kuki.get('orderDesc') == 'on');
  256. if (!browser.files || !browser.files.sort)
  257. browser.files = [];
  258. browser.files = browser.files.sort(function(a, b) {
  259. var a1, b1, arr;
  260. if (!order) order = 'name';
  261. if (order == 'date') {
  262. a1 = a.mtime;
  263. b1 = b.mtime;
  264. } else if (order == 'type') {
  265. a1 = _.getFileExtension(a.name);
  266. b1 = _.getFileExtension(b.name);
  267. } else if (order == 'size') {
  268. a1 = a.size;
  269. b1 = b.size;
  270. } else
  271. eval('a1 = a.' + order + '.toLowerCase(); b1 = b.' + order + '.toLowerCase();');
  272. if ((order == 'size') || (order == 'date')) {
  273. if (a1 < b1) return desc ? 1 : -1;
  274. if (a1 > b1) return desc ? -1 : 1;
  275. }
  276. if (a1 == b1) {
  277. a1 = a.name.toLowerCase();
  278. b1 = b.name.toLowerCase();
  279. arr = [a1, b1];
  280. arr = arr.sort();
  281. return (arr[0] == a1) ? -1 : 1;
  282. }
  283. arr = [a1, b1];
  284. arr = arr.sort();
  285. if (arr[0] == a1) return desc ? 1 : -1;
  286. return desc ? -1 : 1;
  287. });
  288. browser.showFiles(callBack, selected);
  289. browser.initFiles();
  290. };
  291. browser.humanSize = function(size) {
  292. if (size < 1024) {
  293. size = size.toString() + ' B';
  294. } else if (size < 1048576) {
  295. size /= 1024;
  296. size = parseInt(size).toString() + ' KB';
  297. } else if (size < 1073741824) {
  298. size /= 1048576;
  299. size = parseInt(size).toString() + ' MB';
  300. } else if (size < 1099511627776) {
  301. size /= 1073741824;
  302. size = parseInt(size).toString() + ' GB';
  303. } else {
  304. size /= 1099511627776;
  305. size = parseInt(size).toString() + ' TB';
  306. }
  307. return size;
  308. };
  309. browser.baseGetData = function(act) {
  310. var data = 'browse.php?type=' + encodeURIComponent(this.type) + '&lng=' + this.lang;
  311. if (act)
  312. data += "&act=" + act;
  313. return data;
  314. };
  315. browser.label = function(index, data) {
  316. var label = this.labels[index] ? this.labels[index] : index;
  317. if (data)
  318. $.each(data, function(key, val) {
  319. label = label.replace('{' + key + '}', val);
  320. });
  321. return label;
  322. };
  323. browser.check4errors = function(data, shadow) {
  324. if (!data.error)
  325. return false;
  326. var msg;
  327. if (data.error.join)
  328. msg = data.error.join("\n");
  329. else
  330. msg = data.error;
  331. browser.alert(msg, shadow);
  332. return true;
  333. };
  334. browser.post = function(url, data) {
  335. var html = '<form id="postForm" method="POST" action="' + url + '">';
  336. $.each(data, function(key, val) {
  337. if ($.isArray(val))
  338. $.each(val, function(i, aval) {
  339. html += '<input type="hidden" name="' + _.htmlValue(key) + '[]" value="' + _.htmlValue(aval) + '" />';
  340. });
  341. else
  342. html += '<input type="hidden" name="' + _.htmlValue(key) + '" value="' + _.htmlValue(val) + '" />';
  343. });
  344. html += '</form>';
  345. $('#dialog').html(html);
  346. $('#dialog').css('display', 'block');
  347. $('#postForm').get(0).submit();
  348. };
  349. browser.fadeFiles = function() {
  350. $('#files > div').css({
  351. opacity: '0.4',
  352. filter: 'alpha(opacity:40)'
  353. });
  354. };