060.toolbar.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /** This file is part of KCFinder project
  2. *
  3. * @desc Toolbar 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. _.initToolbar = function() {
  13. $('#toolbar').disableTextSelect();
  14. $('#toolbar a').click(function() {
  15. _.menu.hide();
  16. });
  17. if (!$.$.kuki.isSet('displaySettings'))
  18. $.$.kuki.set('displaySettings', "off");
  19. if ($.$.kuki.get('displaySettings') == "on") {
  20. $('#toolbar a[href="kcact:settings"]').addClass('selected');
  21. $('#settings').show();
  22. _.resize();
  23. }
  24. $('#toolbar a[href="kcact:settings"]').click(function () {
  25. var jSettings = $('#settings');
  26. if (jSettings.css('display') == "none") {
  27. $(this).addClass('selected');
  28. $.$.kuki.set('displaySettings', "on");
  29. jSettings.show();
  30. _.fixFilesHeight();
  31. } else {
  32. $(this).removeClass('selected');
  33. $.$.kuki.set('displaySettings', "off");
  34. jSettings.hide();
  35. _.fixFilesHeight();
  36. }
  37. return false;
  38. });
  39. $('#toolbar a[href="kcact:refresh"]').click(function() {
  40. _.refresh();
  41. return false;
  42. });
  43. $('#toolbar a[href="kcact:maximize"]').click(function() {
  44. _.maximize(this);
  45. return false;
  46. });
  47. $('#toolbar a[href="kcact:about"]').click(function() {
  48. var html = '<div class="box about">' +
  49. '<div class="head"><a href="http://kcfinder.sunhater.com" target="_blank">KCFinder</a> ' + _.version + '</div>';
  50. if (_.support.check4Update)
  51. html += '<div id="checkver"><span class="loading"><span>' + _.label("Checking for new version...") + '</span></span></div>';
  52. html +=
  53. '<div>' + _.label("Licenses:") + ' <a href="http://opensource.org/licenses/GPL-3.0" target="_blank">GPLv3</a> & <a href="http://opensource.org/licenses/LGPL-3.0" target="_blank">LGPLv3</a></div>' +
  54. '<div>Copyright &copy;2010-2014 Pavel Tzonkov</div>' +
  55. '</div>';
  56. var dlg = _.dialog(_.label("About"), html, {width: 301});
  57. setTimeout(function() {
  58. $.ajax({
  59. dataType: "json",
  60. url: _.getURL('check4Update'),
  61. async: true,
  62. success: function(data) {
  63. if (!dlg.html().length)
  64. return;
  65. var span = $('#checkver');
  66. span.removeClass('loading');
  67. if (!data.version) {
  68. span.html(_.label("Unable to connect!"));
  69. return;
  70. }
  71. if (_.version < data.version)
  72. span.html('<a href="http://kcfinder.sunhater.com/download" target="_blank">' + _.label("Download version {version} now!", {version: data.version}) + '</a>');
  73. else
  74. span.html(_.label("KCFinder is up to date!"));
  75. },
  76. error: function() {
  77. if (!dlg.html().length)
  78. return;
  79. $('#checkver').removeClass('loading').html(_.label("Unable to connect!"));
  80. }
  81. });
  82. }, 1000);
  83. return false;
  84. });
  85. _.initUploadButton();
  86. };
  87. _.initUploadButton = function() {
  88. var btn = $('#toolbar a[href="kcact:upload"]');
  89. if (!_.access.files.upload) {
  90. btn.hide();
  91. return;
  92. }
  93. var top = btn.get(0).offsetTop,
  94. width = btn.outerWidth(),
  95. height = btn.outerHeight(),
  96. jInput = $('#upload input');
  97. $('#toolbar').prepend('<div id="upload" style="top:' + top + 'px;width:' + width + 'px;height:' + height + 'px"><form enctype="multipart/form-data" method="post" target="uploadResponse" action="' + _.getURL('upload') + '"><input type="file" name="upload[]" onchange="_.uploadFile(this.form)" style="height:' + height + 'px" multiple="multiple" /><input type="hidden" name="dir" value="" /></form></div>');
  98. jInput.css('margin-left', "-" + (jInput.outerWidth() - width));
  99. $('#upload').mouseover(function() {
  100. $('#toolbar a[href="kcact:upload"]').addClass('hover');
  101. }).mouseout(function() {
  102. $('#toolbar a[href="kcact:upload"]').removeClass('hover');
  103. });
  104. };
  105. _.uploadFile = function(form) {
  106. if (!_.dirWritable) {
  107. _.alert(_.label("Cannot write to upload folder."));
  108. $('#upload').detach();
  109. _.initUploadButton();
  110. return;
  111. }
  112. form.elements[1].value = _.dir;
  113. $('<iframe id="uploadResponse" name="uploadResponse" src="javascript:;"></iframe>').prependTo(document.body);
  114. $('#loading').html(_.label("Uploading file...")).show();
  115. form.submit();
  116. $('#uploadResponse').load(function() {
  117. var response = $(this).contents().find('body').text();
  118. $('#loading').hide();
  119. response = response.split("\n");
  120. _.refreshDir($('div.folder > a > span.folder').parent());
  121. var selected = [], errors = [];
  122. $.each(response, function(i, row) {
  123. if (row.substr(0, 1) == "/")
  124. selected[selected.length] = row.substr(1, row.length - 1);
  125. else
  126. errors[errors.length] = row;
  127. });
  128. if (errors.length) {
  129. errors = errors.join("\n");
  130. if (errors.replace(/^\s+/g, "").replace(/\s+$/g, "").length)
  131. _.alert(errors);
  132. }
  133. if (!selected.length)
  134. selected = null;
  135. _.refresh(selected);
  136. $('#upload').detach();
  137. setTimeout(function() {
  138. $('#uploadResponse').detach();
  139. }, 1);
  140. _.initUploadButton();
  141. });
  142. };
  143. _.maximize = function(button) {
  144. // TINYMCE 3
  145. if (_.opener.name == "tinymce") {
  146. var par = window.parent.document,
  147. ifr = $('iframe[src*="browse.php?opener=tinymce&"]', par),
  148. id = parseInt(ifr.attr('id').replace(/^mce_(\d+)_ifr$/, "$1")),
  149. win = $('#mce_' + id, par);
  150. if ($(button).hasClass('selected')) {
  151. $(button).removeClass('selected');
  152. win.css({
  153. left: _.maximizeMCE.left,
  154. top: _.maximizeMCE.top,
  155. width: _.maximizeMCE.width,
  156. height: _.maximizeMCE.height
  157. });
  158. ifr.css({
  159. width: _.maximizeMCE.width - _.maximizeMCE.Hspace,
  160. height: _.maximizeMCE.height - _.maximizeMCE.Vspace
  161. });
  162. } else {
  163. $(button).addClass('selected')
  164. _.maximizeMCE = {
  165. width: parseInt(win.css('width')),
  166. height: parseInt(win.css('height')),
  167. left: win.position().left,
  168. top: win.position().top,
  169. Hspace: parseInt(win.css('width')) - parseInt(ifr.css('width')),
  170. Vspace: parseInt(win.css('height')) - parseInt(ifr.css('height'))
  171. };
  172. var width = $(window.top).width(),
  173. height = $(window.top).height();
  174. win.css({
  175. left: $(window.parent).scrollLeft(),
  176. top: $(window.parent).scrollTop(),
  177. width: width,
  178. height: height
  179. });
  180. ifr.css({
  181. width: width - _.maximizeMCE.Hspace,
  182. height: height - _.maximizeMCE.Vspace
  183. });
  184. }
  185. // TINYMCE 4
  186. } else if (_.opener.name == "tinymce4") {
  187. var par = window.parent.document,
  188. ifr = $('iframe[src*="browse.php?opener=tinymce4&"]', par).parent(),
  189. win = ifr.parent();
  190. if ($(button).hasClass('selected')) {
  191. $(button).removeClass('selected');
  192. win.css({
  193. left: _.maximizeMCE4.left,
  194. top: _.maximizeMCE4.top,
  195. width: _.maximizeMCE4.width,
  196. height: _.maximizeMCE4.height
  197. });
  198. ifr.css({
  199. width: _.maximizeMCE4.width,
  200. height: _.maximizeMCE4.height - _.maximizeMCE4.Vspace
  201. });
  202. } else {
  203. $(button).addClass('selected');
  204. _.maximizeMCE4 = {
  205. width: parseInt(win.css('width')),
  206. height: parseInt(win.css('height')),
  207. left: win.position().left,
  208. top: win.position().top,
  209. Vspace: win.outerHeight(true) - ifr.outerHeight(true) - 1
  210. };
  211. var width = $(window.top).width(),
  212. height = $(window.top).height();
  213. win.css({
  214. left: 0,
  215. top: 0,
  216. width: width,
  217. height: height
  218. });
  219. ifr.css({
  220. width: width,
  221. height: height - _.maximizeMCE4.Vspace
  222. });
  223. }
  224. // PUPUP WINDOW
  225. } else if (window.opener) {
  226. window.moveTo(0, 0);
  227. width = screen.availWidth;
  228. height = screen.availHeight;
  229. if ($.agent.opera)
  230. height -= 50;
  231. window.resizeTo(width, height);
  232. } else {
  233. if (window.parent) {
  234. var el = null;
  235. $(window.parent.document).find('iframe').each(function() {
  236. if (this.src.replace('/?', '?') == window.location.href.replace('/?', '?')) {
  237. el = this;
  238. return false;
  239. }
  240. });
  241. // IFRAME
  242. if (el !== null)
  243. $(el).toggleFullscreen(window.parent.document);
  244. // SELF WINDOW
  245. else
  246. $('body').toggleFullscreen();
  247. } else
  248. $('body').toggleFullscreen();
  249. }
  250. };
  251. _.refresh = function(selected) {
  252. _.fadeFiles();
  253. $.ajax({
  254. type: "post",
  255. dataType: "json",
  256. url: _.getURL("chDir"),
  257. data: {dir: _.dir},
  258. async: false,
  259. success: function(data) {
  260. if (_.check4errors(data)) {
  261. $('#files > div').css({opacity: "", filter: ""});
  262. return;
  263. }
  264. _.dirWritable = data.dirWritable;
  265. _.files = data.files ? data.files : [];
  266. _.orderFiles(null, selected);
  267. _.statusDir();
  268. },
  269. error: function() {
  270. $('#files > div').css({opacity: "", filter: ""});
  271. $('#files').html(_.label("Unknown error."));
  272. }
  273. });
  274. };