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. var selected = [], errors = [];
  121. $.each(response, function(i, row) {
  122. if (row.substr(0, 1) == "/")
  123. selected[selected.length] = row.substr(1, row.length - 1);
  124. else
  125. errors[errors.length] = row;
  126. });
  127. if (errors.length) {
  128. errors = errors.join("\n");
  129. if (errors.replace(/^\s+/g, "").replace(/\s+$/g, "").length)
  130. _.alert(errors);
  131. }
  132. if (!selected.length)
  133. selected = null;
  134. _.refresh(selected);
  135. $('#upload').detach();
  136. setTimeout(function() {
  137. $('#uploadResponse').detach();
  138. }, 1);
  139. _.initUploadButton();
  140. });
  141. };
  142. _.maximize = function(button) {
  143. // TINYMCE 3
  144. if (_.opener.name == "tinymce") {
  145. var par = window.parent.document,
  146. ifr = $('iframe[src*="browse.php?opener=tinymce&"]', par),
  147. id = parseInt(ifr.attr('id').replace(/^mce_(\d+)_ifr$/, "$1")),
  148. win = $('#mce_' + id, par);
  149. if ($(button).hasClass('selected')) {
  150. $(button).removeClass('selected');
  151. win.css({
  152. left: _.maximizeMCE.left,
  153. top: _.maximizeMCE.top,
  154. width: _.maximizeMCE.width,
  155. height: _.maximizeMCE.height
  156. });
  157. ifr.css({
  158. width: _.maximizeMCE.width - _.maximizeMCE.Hspace,
  159. height: _.maximizeMCE.height - _.maximizeMCE.Vspace
  160. });
  161. } else {
  162. $(button).addClass('selected')
  163. _.maximizeMCE = {
  164. width: parseInt(win.css('width')),
  165. height: parseInt(win.css('height')),
  166. left: win.position().left,
  167. top: win.position().top,
  168. Hspace: parseInt(win.css('width')) - parseInt(ifr.css('width')),
  169. Vspace: parseInt(win.css('height')) - parseInt(ifr.css('height'))
  170. };
  171. var width = $(window.top).width(),
  172. height = $(window.top).height();
  173. win.css({
  174. left: $(window.parent).scrollLeft(),
  175. top: $(window.parent).scrollTop(),
  176. width: width,
  177. height: height
  178. });
  179. ifr.css({
  180. width: width - _.maximizeMCE.Hspace,
  181. height: height - _.maximizeMCE.Vspace
  182. });
  183. }
  184. // TINYMCE 4
  185. } else if (_.opener.name == "tinymce4") {
  186. var par = window.parent.document,
  187. ifr = $('iframe[src*="browse.php?opener=tinymce4&"]', par).parent(),
  188. win = ifr.parent();
  189. if ($(button).hasClass('selected')) {
  190. $(button).removeClass('selected');
  191. win.css({
  192. left: _.maximizeMCE4.left,
  193. top: _.maximizeMCE4.top,
  194. width: _.maximizeMCE4.width,
  195. height: _.maximizeMCE4.height
  196. });
  197. ifr.css({
  198. width: _.maximizeMCE4.width,
  199. height: _.maximizeMCE4.height - _.maximizeMCE4.Vspace
  200. });
  201. } else {
  202. $(button).addClass('selected');
  203. _.maximizeMCE4 = {
  204. width: parseInt(win.css('width')),
  205. height: parseInt(win.css('height')),
  206. left: win.position().left,
  207. top: win.position().top,
  208. Vspace: win.outerHeight(true) - ifr.outerHeight(true) - 1
  209. };
  210. var width = $(window.top).width(),
  211. height = $(window.top).height();
  212. win.css({
  213. left: 0,
  214. top: 0,
  215. width: width,
  216. height: height
  217. });
  218. ifr.css({
  219. width: width,
  220. height: height - _.maximizeMCE4.Vspace
  221. });
  222. }
  223. // PUPUP WINDOW
  224. } else if (window.opener) {
  225. window.moveTo(0, 0);
  226. width = screen.availWidth;
  227. height = screen.availHeight;
  228. if ($.agent.opera)
  229. height -= 50;
  230. window.resizeTo(width, height);
  231. } else {
  232. if (window.parent) {
  233. var el = null;
  234. $(window.parent.document).find('iframe').each(function() {
  235. if (this.src.replace('/?', '?') == window.location.href.replace('/?', '?')) {
  236. el = this;
  237. return false;
  238. }
  239. });
  240. // IFRAME
  241. if (el !== null)
  242. $(el).toggleFullscreen(window.parent.document);
  243. // SELF WINDOW
  244. else
  245. $('body').toggleFullscreen();
  246. } else
  247. $('body').toggleFullscreen();
  248. }
  249. };
  250. _.refresh = function(selected) {
  251. _.fadeFiles();
  252. $.ajax({
  253. type: "post",
  254. dataType: "json",
  255. url: _.getURL("chDir"),
  256. data: {dir: _.dir},
  257. async: false,
  258. success: function(data) {
  259. if (_.check4errors(data)) {
  260. $('#files > div').css({opacity: "", filter: ""});
  261. return;
  262. }
  263. _.dirWritable = data.dirWritable;
  264. _.files = data.files ? data.files : [];
  265. _.orderFiles(null, selected);
  266. _.statusDir();
  267. },
  268. error: function() {
  269. $('#files > div').css({opacity: "", filter: ""});
  270. $('#files').html(_.label("Unknown error."));
  271. }
  272. });
  273. };