050.init.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /** This file is part of KCFinder project
  2. *
  3. * @desc Object initializations
  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. _.init = function() {
  13. if (!_.checkAgent()) return;
  14. $('body').click(function() {
  15. _.menu.hide();
  16. }).rightClick();
  17. $('#menu').unbind().click(function() {
  18. return false;
  19. });
  20. _.initOpeners();
  21. _.initSettings();
  22. _.initContent();
  23. _.initToolbar();
  24. _.initResizer();
  25. _.initDropUpload();
  26. var div = $('<div></div>')
  27. .css({width: 100, height: 100, overflow: 'auto', position: 'absolute', top: -1000, left: -1000})
  28. .prependTo('body').append('<div></div>').find('div').css({width: '100%', height: 200});
  29. _.scrollbarWidth = 100 - div.width();
  30. div.parent().remove();
  31. $.each($.agent, function(i) {
  32. if (i != "platform")
  33. $('body').addClass(i)
  34. });
  35. if ($.agent.platform)
  36. $.each($.agent.platform, function(i) {
  37. $('body').addClass(i)
  38. });
  39. if ($.mobile)
  40. $('body').addClass("mobile");
  41. };
  42. _.checkAgent = function() {
  43. if (($.agent.msie && !$.agent.opera && !$.agent.chromeframe && (parseInt($.agent.msie) < 9)) ||
  44. ($.agent.opera && (parseInt($.agent.version) < 10)) ||
  45. ($.agent.firefox && (parseFloat($.agent.firefox) < 1.8))
  46. ) {
  47. var html = '<div style="padding:10px">Your browser is not capable to display KCFinder. Please update your browser or install another one: <a href="http://www.mozilla.com/firefox/" target="_blank">Mozilla Firefox</a>, <a href="http://www.apple.com/safari" target="_blank">Apple Safari</a>, <a href="http://www.google.com/chrome" target="_blank">Google Chrome</a>, <a href="http://www.opera.com/browser" target="_blank">Opera</a>.';
  48. if ($.agent.msie && !$.agent.opera)
  49. html += ' You may also install <a href="http://www.google.com/chromeframe" target="_blank">Google Chrome Frame ActiveX plugin</a> to get Internet Explorer 6, 7, 8 working.';
  50. html += '</div>';
  51. $('body').html(html);
  52. return false;
  53. }
  54. return true;
  55. };
  56. _.initOpeners = function() {
  57. try {
  58. // TinyMCE 3
  59. if (_.opener.name == "tinymce") {
  60. if (typeof tinyMCEPopup == "undefined")
  61. _.opener.name = null;
  62. else
  63. _.opener.callBack = true;
  64. // TinyMCE 4
  65. } else if (_.opener.name == "tinymce4")
  66. _.opener.callBack = true;
  67. // CKEditor
  68. else if (_.opener.name == "ckeditor") {
  69. if (window.parent && window.parent.CKEDITOR)
  70. _.opener.CKEditor.object = window.parent.CKEDITOR;
  71. else if (window.opener && window.opener.CKEDITOR) {
  72. _.opener.CKEditor.object = window.opener.CKEDITOR;
  73. _.opener.callBack = true;
  74. } else
  75. _.opener.CKEditor = null;
  76. // FCKeditor
  77. } else if ((!_.opener.name || (_.opener.name == "fckeditor")) && window.opener && window.opener.SetUrl) {
  78. _.opener.name = "fckeditor";
  79. _.opener.callBack = true;
  80. }
  81. // Custom callback
  82. if (!_.opener.callBack) {
  83. if ((window.opener && window.opener.KCFinder && window.opener.KCFinder.callBack) ||
  84. (window.parent && window.parent.KCFinder && window.parent.KCFinder.callBack)
  85. )
  86. _.opener.callBack = window.opener
  87. ? window.opener.KCFinder.callBack
  88. : window.parent.KCFinder.callBack;
  89. if ((
  90. window.opener &&
  91. window.opener.KCFinder &&
  92. window.opener.KCFinder.callBackMultiple
  93. ) || (
  94. window.parent &&
  95. window.parent.KCFinder &&
  96. window.parent.KCFinder.callBackMultiple
  97. )
  98. )
  99. _.opener.callBackMultiple = window.opener
  100. ? window.opener.KCFinder.callBackMultiple
  101. : window.parent.KCFinder.callBackMultiple;
  102. }
  103. } catch(e) {}
  104. };
  105. _.initContent = function() {
  106. $('div#folders').html(_.label("Loading folders..."));
  107. $('div#files').html(_.label("Loading files..."));
  108. $.ajax({
  109. type: "get",
  110. dataType: "json",
  111. url: _.getURL("init"),
  112. async: false,
  113. success: function(data) {
  114. if (_.check4errors(data))
  115. return;
  116. _.dirWritable = data.dirWritable;
  117. $('#folders').html(_.buildTree(data.tree));
  118. _.setTreeData(data.tree);
  119. _.setTitle("KCFinder: /" + _.dir);
  120. _.initFolders();
  121. _.files = data.files ? data.files : [];
  122. _.orderFiles();
  123. },
  124. error: function() {
  125. $('div#folders').html(_.label("Unknown error."));
  126. $('div#files').html(_.label("Unknown error."));
  127. }
  128. });
  129. };
  130. _.initResizer = function() {
  131. var cursor = ($.agent.opera) ? 'move' : 'col-resize';
  132. $('#resizer').css('cursor', cursor).draggable({
  133. axis: 'x',
  134. start: function() {
  135. $(this).css({
  136. opacity: "0.4",
  137. filter: "alpha(opacity=40)"
  138. });
  139. $('#all').css('cursor', cursor);
  140. },
  141. stop: function() {
  142. $(this).css({
  143. opacity: "0",
  144. filter: "alpha(opacity=0)"
  145. });
  146. $('#all').css('cursor', "");
  147. var jLeft = $('#left'),
  148. jRight = $('#right'),
  149. jFiles = $('#files'),
  150. jFolders = $('#folders'),
  151. left = parseInt($(this).css('left')) + parseInt($(this).css('width')),
  152. w = 0, r;
  153. $('#toolbar a').each(function() {
  154. if ($(this).css('display') != "none")
  155. w += $(this).outerWidth(true);
  156. });
  157. r = $(window).width() - w;
  158. if (left < 100)
  159. left = 100;
  160. if (left > r)
  161. left = r;
  162. var right = $(window).width() - left;
  163. jLeft.css('width', left);
  164. jRight.css('width', right);
  165. jFiles.css('width', jRight.innerWidth() - jFiles.outerHSpace());
  166. $('#resizer').css({
  167. left: jLeft.outerWidth() - jFolders.outerRightSpace('m'),
  168. width: jFolders.outerRightSpace('m') + jFiles.outerLeftSpace('m')
  169. });
  170. _.fixFilesHeight();
  171. }
  172. });
  173. };
  174. _.resize = function() {
  175. var jLeft = $('#left'),
  176. jRight = $('#right'),
  177. jStatus = $('#status'),
  178. jFolders = $('#folders'),
  179. jFiles = $('#files'),
  180. jResizer = $('#resizer'),
  181. jWindow = $(window);
  182. jLeft.css({
  183. width: "25%",
  184. height: jWindow.height() - jStatus.outerHeight()
  185. });
  186. jRight.css({
  187. width: "75%",
  188. height: jWindow.height() - jStatus.outerHeight()
  189. });
  190. $('#toolbar').css('height', $('#toolbar a').outerHeight());
  191. jResizer.css('height', $(window).height());
  192. jFolders.css('height', jLeft.outerHeight() - jFolders.outerVSpace());
  193. _.fixFilesHeight();
  194. var width = jLeft.outerWidth() + jRight.outerWidth();
  195. jStatus.css('width', width);
  196. while (jStatus.outerWidth() > width)
  197. jStatus.css('width', parseInt(jStatus.css('width')) - 1);
  198. while (jStatus.outerWidth() < width)
  199. jStatus.css('width', parseInt(jStatus.css('width')) + 1);
  200. jFiles.css('width', jRight.innerWidth() - jFiles.outerHSpace());
  201. jResizer.css({
  202. left: jLeft.outerWidth() - jFolders.outerRightSpace('m'),
  203. width: jFolders.outerRightSpace('m') + jFiles.outerLeftSpace('m')
  204. });
  205. };
  206. _.setTitle = function(title) {
  207. document.title = title;
  208. if (_.opener.name == "tinymce")
  209. tinyMCEPopup.editor.windowManager.setTitle(window, title);
  210. else if (_.opener.name == "tinymce4") {
  211. var ifr = $('iframe[src*="browse.php?opener=tinymce4&"]', window.parent.document),
  212. path = ifr.attr('src').split('browse.php?')[0];
  213. ifr.parent().parent().find('div.mce-title').html('<span style="padding:0 0 0 28px;margin:-2px 0 -3px -6px;display:block;font-size:1em;font-weight:bold;background:url(' + path + 'themes/default/img/kcf_logo.png) left center no-repeat">' + title + '</span>');
  214. }
  215. };
  216. _.fixFilesHeight = function() {
  217. var jFiles = $('#files'),
  218. jSettings = $('#settings');
  219. jFiles.css('height',
  220. $('#left').outerHeight() - $('#toolbar').outerHeight() - jFiles.outerVSpace() -
  221. ((jSettings.css('display') != "none") ? jSettings.outerHeight() : 0)
  222. );
  223. };