functions.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**
  2. * @package Dolphin Core
  3. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  5. */
  6. function getHtmlData( elem, url, callback, method , confirmation)
  7. {
  8. if ('undefined' != typeof(confirmation) && confirmation && !confirm(_t('_are you sure?')))
  9. return false;
  10. // in most cases it is element ID, in other cases - object of jQuery
  11. if (typeof elem == 'string')
  12. elem = '#' + elem; // create selector from ID
  13. var $block = $(elem);
  14. var blockPos = $block.css('position');
  15. $block.css('position', 'relative'); // set temporarily for displaying "loading icon"
  16. bx_loading_content($block, true);
  17. var $loadingDiv = $block.find('.bx-loading-ajax');
  18. var iLeftOff = parseInt(($block.innerWidth() / 2.0) - ($loadingDiv.outerWidth() / 2.0));
  19. var iTopOff = parseInt(($block.innerHeight() / 2.0) - ($loadingDiv.outerHeight()));
  20. if (iTopOff<0) iTopOff = 0;
  21. $loadingDiv.css({
  22. position: 'absolute',
  23. left: iLeftOff,
  24. top: iTopOff,
  25. zIndex:100
  26. });
  27. if (undefined != method && (method == 'post' || method == 'POST')) {
  28. $.post(url, function(data) {
  29. $block.html(data);
  30. $block.css('position', blockPos).bxTime();
  31. if ($.isFunction($.fn.addWebForms))
  32. $block.addWebForms();
  33. if (typeof callback == 'function')
  34. callback.apply($block);
  35. });
  36. } else {
  37. $block.load(url + '&_r=' + Math.random(), function() {
  38. $(this).css('position', blockPos).bxTime();
  39. if ($.isFunction($.fn.addWebForms))
  40. $(this).addWebForms();
  41. if (typeof callback == 'function')
  42. callback.apply(this);
  43. });
  44. }
  45. }
  46. /**
  47. * This function reloads page block automatically,
  48. * just provide any element inside the block and this function will reload the block.
  49. * @param e - element inside the block
  50. * @return true on success, or false on error - particularly, if block isn't found
  51. */
  52. function loadDynamicBlockAuto(e) {
  53. var sUrl = location.href;
  54. var sId = $(e).parents('.bx-page-block-container:first').attr('id');
  55. if (!sId || !sId.length)
  56. return false;
  57. var aMatches = sId.match(/\-(\d+)$/);
  58. if (!aMatches || !aMatches[1])
  59. return false;
  60. loadDynamicBlock(parseInt(aMatches[1]), sUrl);
  61. return true;
  62. }
  63. function loadDynamicBlock(iBlockID, sUrl) {
  64. getHtmlData($('#bx-page-block-' + iBlockID), bx_append_url_params(sUrl, 'dynamic=tab&pageBlock=' + iBlockID));
  65. return true;
  66. }
  67. function loadDynamicPopupBlock(iBlockID, sUrl) {
  68. if (!$('#dynamicPopup').length) {
  69. $('<div id="dynamicPopup" style="display:none;"></div>').prependTo('body');
  70. }
  71. $('#dynamicPopup').load(
  72. (sUrl + '&dynamic=popup&pageBlock=' + iBlockID),
  73. function() {
  74. $(this).dolPopup({
  75. left: 0,
  76. top: 0
  77. });
  78. }
  79. );
  80. }
  81. function closeDynamicPopupBlock() {
  82. $('#dynamicPopup').dolPopupHide();
  83. }
  84. /**
  85. * Translate string
  86. */
  87. function _t(s, arg0, arg1, arg2) {
  88. if (!window.aDolLang || !aDolLang[s])
  89. return s;
  90. cs = aDolLang[s];
  91. cs = cs.replace(/\{0\}/g, arg0);
  92. cs = cs.replace(/\{1\}/g, arg1);
  93. cs = cs.replace(/\{2\}/g, arg2);
  94. return cs;
  95. }
  96. function showPopupLoginForm() {
  97. var oPopupOptions = {};
  98. if ($('#login_div').length)
  99. $('#login_div').dolPopup(oPopupOptions);
  100. else {
  101. $('<div id="login_div" style="visibility: none;"></div>').prependTo('body').load(
  102. sUrlRoot + 'member.php',
  103. {
  104. action: 'show_login_form',
  105. relocate: String(window.location)
  106. },
  107. function() {
  108. $(this).dolPopup(oPopupOptions);
  109. }
  110. );
  111. }
  112. }
  113. function showPopupAnyHtml(sUrl, sId) {
  114. var oPopupOptions = {};
  115. if (!sId || !sId.length)
  116. sId = 'login_div';
  117. $('#' + sId).remove();
  118. $('<div id="' + sId + '" style="display: none;"></div>').prependTo('body').load(
  119. sUrl.match('^http[s]{0,1}:\/\/') ? sUrl : sUrlRoot + sUrl,
  120. function() {
  121. $(this).dolPopup(oPopupOptions);
  122. }
  123. );
  124. }
  125. function bx_loading_btn (e, b) {
  126. e = $(e);
  127. if (e.children('div').size())
  128. e = e.children('div').first();
  129. if (!b) {
  130. e.find('.bx-loading-ajax-btn').remove();
  131. } else if (!e.find('.bx-loading-ajax-btn').length) {
  132. e.append('<b class="bx-loading-ajax-btn"></b>');
  133. new Spinner(aSpinnerSmallOpts).spin(e.find('.bx-loading-ajax-btn').get(0));
  134. }
  135. }
  136. function bx_loading_animate (e) {
  137. e = $(e);
  138. if (!e.length)
  139. return false;
  140. if (e.find('.bx-sys-spinner').length)
  141. return false;
  142. return new Spinner(aSpinnerOpts).spin(e.get(0));
  143. }
  144. function bx_loading_content (elem, b, isReplace) {
  145. var block = $(elem);
  146. if (!b) {
  147. block.find(".bx-loading-ajax").remove();
  148. } else if (!block.find('.bx-loading-ajax').length) {
  149. if ('undefined' != typeof(isReplace) && isReplace)
  150. block.html('<div class="bx-loading-ajax" style="position:static;"></div>');
  151. else
  152. block.append('<div class="bx-loading-ajax"></div>');
  153. bx_loading_animate(block.find('.bx-loading-ajax'));
  154. }
  155. }
  156. function bx_loading (elem, b) {
  157. if (typeof elem == 'string')
  158. elem = '#' + elem;
  159. var block = $(elem);
  160. if (1 == b || true == b) {
  161. bx_loading_content(block, b);
  162. e = block.find(".bx-loading-ajax");
  163. e.css('left', parseInt(block.width()/2.0 - e.width()/2.0));
  164. var he = e.outerHeight();
  165. var hc = block.outerHeight();
  166. if (block.css('position') != 'relative' && block.css('position') != 'absolute') {
  167. if (!block.data('css-save-position'))
  168. block.data('css-save-position', block.css('position'));
  169. block.css('position', 'relative');
  170. }
  171. if (hc > he) {
  172. e.css('top', parseInt(hc/2.0 - he/2.0));
  173. }
  174. if (hc < he) {
  175. if (!block.data('css-save-min-height'))
  176. block.data('css-save-min-height', block.css('min-height'));
  177. block.css('min-height', he);
  178. }
  179. } else {
  180. if (block.data('css-save-position'))
  181. block.css('position', block.data('css-save-position'));
  182. if (block.data('css-save-min-height'))
  183. block.css('min-height', block.data('css-save-min-height'));
  184. bx_loading_content(block, b);
  185. }
  186. }
  187. /**
  188. * js version on BxBaseFunction::centerContent function
  189. * sSel - jQuery selector of content to be centered
  190. * sBlockSel - jquery selector of blocks
  191. */
  192. function bx_center_content (sSel, sBlockStyle) {
  193. var sId = 'id' + (new Date()).getTime();
  194. $(sSel).wrap('<div id="'+sId+'"></div>');
  195. //$(document).ready(function() {
  196. var eCenter = $('#' + sId);
  197. var iAll = $('#' + sId + ' ' + sBlockStyle).size();
  198. var iWidthUnit = $('#' + sId + ' ' + sBlockStyle + ':first').outerWidth({"margin":true});
  199. var iWidthContainer = eCenter.innerWidth();
  200. var iPerRow = parseInt(iWidthContainer/iWidthUnit);
  201. var iLeft = (iWidthContainer - (iAll > iPerRow ? iPerRow * iWidthUnit : iAll * iWidthUnit)) / 2;
  202. eCenter.css("padding-left", iLeft);
  203. //});
  204. }
  205. /**
  206. * Show pointer popup with menu from URL.
  207. * @param o - menu object name
  208. * @param e - element to show popup at
  209. * @param options - popup options
  210. * @param vars - additional GET variables
  211. */
  212. function bx_menu_popup (o, e, options, vars) {
  213. var options = options || {};
  214. var vars = vars || {};
  215. var o = $.extend({}, $.fn.dolPopupDefaultOptions, options, {id: o, url: bx_append_url_params('menu.php', $.extend({o:o}, vars))});
  216. $(e).dolPopupAjax(o);
  217. }
  218. /**
  219. * Show pointer popup with menu from existing HTML.
  220. * @param jSel - jQuery selector for html to show in popup
  221. * @param e - element to show popup at
  222. * @param options - popup options
  223. */
  224. function bx_menu_popup_inline (jSel, e, options) {
  225. var options = options || {};
  226. var o = $.extend({}, $.fn.dolPopupDefaultOptions, options, {pointer:{el:$(e)}});
  227. if ($(jSel + ':visible').length)
  228. $(jSel).dolPopupHide();
  229. else
  230. $(jSel).dolPopup(o);
  231. }
  232. function validateLoginForm(eForm) {
  233. return true;
  234. }
  235. /**
  236. * convert <time> tags to human readable format
  237. * @param sLang - use sLang localization
  238. * @param isAutoupdate - for internal usage only
  239. */
  240. function bx_time(sLang, isAutoupdate, sRootSel) {
  241. if ('undefined' == typeof(sRootSel))
  242. sRootSel = 'body';
  243. var iAutoupdate = 22*60*60; // autoupdate time in realtime if less than 22 hours
  244. var sSel = 'time';
  245. if ('undefined' == typeof(sLang)) {
  246. sLang = glBxTimeLang;
  247. } else {
  248. glBxTimeLang = sLang;
  249. }
  250. if ('undefined' != typeof(isAutoupdate) && isAutoupdate)
  251. sSel += '.bx-time-autoupdate';
  252. $(sRootSel).find(sSel).each(function () {
  253. var s;
  254. var sTime = $(this).attr('datetime');
  255. var iSecondsDiff = moment(sTime).unix() - moment().unix();
  256. if (iSecondsDiff < 0)
  257. iSecondsDiff = -iSecondsDiff;
  258. if (iSecondsDiff < $(this).attr('data-bx-autoformat'))
  259. s = moment(sTime).lang(sLang).fromNow(); // 'time ago' format
  260. else
  261. s = moment(sTime).lang(sLang).format($(this).attr('data-bx-format')); // custom format
  262. if (iSecondsDiff < iAutoupdate)
  263. $(this).addClass('bx-time-autoupdate');
  264. else
  265. $(this).removeClass('bx-time-autoupdate');
  266. $(this).html(s);
  267. });
  268. if ($('time.bx-time-autoupdate').size()) {
  269. setTimeout(function () {
  270. bx_time(sLang, true);
  271. }, 20000);
  272. }
  273. }
  274. (function($) {
  275. $.fn.bxTime = function() {
  276. bx_time(undefined, undefined, this);
  277. return this;
  278. };
  279. } (jQuery));
  280. /**
  281. * Perform connections AJAX request.
  282. * In case of error - it shows js alert with error message.
  283. * In case of success - the page is reloaded.
  284. *
  285. * @param sObj - connections object
  286. * @param sAction - 'add', 'remove' or 'reject'
  287. * @param iContentId - content id, initiator is always current logged in user
  288. * @param bConfirm - show confirmation dialog
  289. */
  290. function bx_conn_action(e, sObj, sAction, iContentId, bConfirm) {
  291. if ('undefined' != typeof(bConfirm) && bConfirm && !confirm(_t('_are you sure?')))
  292. return;
  293. var aParams = {
  294. obj: sObj,
  295. act: sAction,
  296. id: iContentId
  297. };
  298. var fCallback = function (data) {
  299. bx_loading_btn(e, 0);
  300. if ('object' != typeof(data))
  301. return;
  302. if (data.err) {
  303. alert(data.msg);
  304. } else {
  305. if (!loadDynamicBlockAuto(e))
  306. location.reload();
  307. else
  308. $('#bx-popup-ajax-wrapper-bx_persons_view_actions_more').remove();
  309. }
  310. };
  311. bx_loading_btn(e, 1);
  312. $.ajax({
  313. dataType: 'json',
  314. url: sUrlRoot + 'conn.php',
  315. data: aParams,
  316. type: 'POST',
  317. success: fCallback
  318. });
  319. }
  320. function bx_append_url_params (sUrl, mixedParams) {
  321. var sParams = sUrl.indexOf('?') == -1 ? '?' : '&';
  322. if(mixedParams instanceof Array) {
  323. for(var i in mixedParams)
  324. sParams += i + '=' + mixedParams[i] + '&';
  325. sParams = sParams.substr(0, sParams.length-1);
  326. }
  327. else if(mixedParams instanceof Object) {
  328. $.each(mixedParams, function(sKey, sValue) {
  329. sParams += sKey + '=' + sValue + '&';
  330. });
  331. sParams = sParams.substr(0, sParams.length-1);
  332. }
  333. else
  334. sParams += mixedParams;
  335. return sUrl + sParams;
  336. }