BxDolWiki.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup UnaCore UNA Core
  6. * @{
  7. */
  8. function BxDolWiki (sObject, eContainer, oOptions) {
  9. var oDefault = {};
  10. this._oOptions = $.extend({}, oDefault, oOptions);
  11. this._sObject = sObject;
  12. this._eCont = eContainer;
  13. this._sActionUrl = sUrlRoot + 'r.php?_q=' + this._oOptions.wiki_action_uri + '-action/';
  14. this._sCurrentLang = this._oOptions['language'];
  15. this._oTranslations = {};
  16. }
  17. BxDolWiki.prototype.bindEvents = function () {
  18. var self = this;
  19. var e = $('#bx-popup-ajax-wrapper-bx-wiki-menu-' + this._oOptions.block_id);
  20. var oEvents = {
  21. "edit": "onEdit",
  22. "delete-version": "onDeleteVersion",
  23. "delete-block": "onDeleteBlock",
  24. "translate": "onTranslate",
  25. "history": "onHistory",
  26. }
  27. $.each(oEvents, function (sSel, sFunc) {
  28. e.find(".bx-menu-item a." + sSel).on('click', {wiki_obj:self}, function() {
  29. $(".bx-popup-active").dolPopupHide(); // close menu popup
  30. BxDolWiki.prototype[sFunc].apply(self);
  31. return false;
  32. });
  33. });
  34. };
  35. BxDolWiki.prototype.removePopup = function (iBlockId) {
  36. if ('undefined' === typeof(iBlockId))
  37. iBlockId = this._oOptions.block_id;
  38. $("#bx-wiki-form-" + iBlockId).parents(".bx-popup-wrapper").remove();
  39. $(".bx-wiki-history").parents(".bx-popup-wrapper").remove();
  40. $(".bx-wiki-del-rev").parents(".bx-popup-wrapper").remove();
  41. }
  42. BxDolWiki.prototype.getTranslations = function () {
  43. return this._oTranslations;
  44. }
  45. BxDolWiki.prototype.loadingForm = function (b) {
  46. bx_loading($('#bx-form-element-content'), b);
  47. var eForm = $("#bx-wiki-form-" + this._oOptions.block_id);
  48. if (b)
  49. $(eForm).find("textarea[name=content]").val('');
  50. $(eForm).find("textarea[name=content],input[name=language],button[type=submit]").prop('disabled', b);
  51. }
  52. BxDolWiki.prototype.onChangeLangSelector = function (e) {
  53. var self = this;
  54. var eContent = $("#bx-wiki-form-" + this._oOptions.block_id + " textarea[name=content]");
  55. var sLangNew = $(e).val();
  56. // save current translation
  57. if (null !== this._sCurrentLang)
  58. this._oTranslations[this._sCurrentLang] = eContent.val();
  59. if ('undefined' === typeof(this._oTranslations[sLangNew])) {
  60. // no saved translation was found - get from server
  61. self.loadingForm(true);
  62. var sUrl = bx_append_url_params(sUrlRoot + 'r.php?_q=' + this._oOptions.wiki_action_uri + '-action/get-translation', {
  63. block_id: this._oOptions.block_id,
  64. language: sLangNew,
  65. });
  66. $.getJSON(sUrl, function (oData) {
  67. if ('undefined' !== typeof(oData.content))
  68. eContent.val(oData.content);
  69. // save current language
  70. self._sCurrentLang = sLangNew;
  71. self.loadingForm(false);
  72. });
  73. }
  74. else {
  75. // saved translation was found - switch to it immediately
  76. eContent.val(this._oTranslations[sLangNew]);
  77. // save current language
  78. this._sCurrentLang = sLangNew;
  79. }
  80. };
  81. BxDolWiki.prototype.onTranslate = function () {
  82. this._sCurrentLang = this._oOptions['language'];
  83. this._oTranslations = {};
  84. this.popup('translate');
  85. };
  86. BxDolWiki.prototype.onEdit = function () {
  87. this._sCurrentLang = this._oOptions['language'];
  88. this._oTranslations = {};
  89. this.popup('edit');
  90. };
  91. BxDolWiki.prototype.onDeleteVersion = function () {
  92. this.popup('delete-version');
  93. };
  94. BxDolWiki.prototype.onDeleteBlock = function () {
  95. var self = this;
  96. bx_confirm(this._oOptions.t_confirm_block_deletion, function () {
  97. $.post(self._sActionUrl + 'delete-block', {block_id: self._oOptions.block_id}, function (oData) {
  98. self.processResponce(oData);
  99. }, 'json');
  100. });
  101. };
  102. BxDolWiki.prototype.onHistory = function () {
  103. this.popup('history');
  104. };
  105. BxDolWiki.prototype.processResponce = function (oResponce) {
  106. var self = this;
  107. if ('undefined' === typeof(oResponce.actions))
  108. return;
  109. if ('string' === typeof(oResponce.actions))
  110. oResponce.actions = [oResponce.actions];
  111. if ('object' === typeof(oResponce.actions)) {
  112. $.each(oResponce.actions, function () {
  113. if ('function' == typeof(self['action' + this]))
  114. self['action' + this](oResponce);
  115. });
  116. }
  117. }
  118. BxDolWiki.prototype.actionShowMsg = function (oResponce) {
  119. bx_alert(oResponce.msg);
  120. }
  121. BxDolWiki.prototype.actionClosePopup = function (oResponce) {
  122. var self = this;
  123. if ($(".bx-popup-active").size())
  124. $(".bx-popup-active").dolPopupHide({onHide:function (){
  125. self.removePopup();
  126. }})
  127. else
  128. this.removePopup();
  129. }
  130. BxDolWiki.prototype.actionReload = function (oResponce) {
  131. var l = document.location;
  132. var ePage = $('#bx-page-block-' + oResponce.block_id).parents('.bx-layout-wrapper:first');
  133. var sUrl = l.hash.length ? l.href.replace(l.hash, '') : l.href;
  134. if (ePage.size())
  135. sUrl = sUrlRoot + 'page.php?i=' + ePage.attr('id').replace('bx-page-', '');
  136. loadDynamicBlock(oResponce.block_id, sUrl);
  137. }
  138. BxDolWiki.prototype.actionDeleteBlock = function (oResponce) {
  139. $('#bx-page-block-' + oResponce.block_id).remove();
  140. }
  141. BxDolWiki.prototype.popup = function (sAction) {
  142. var self = this;
  143. var sActionUrl = bx_append_url_params(this._sActionUrl + sAction, {
  144. block_id: this._oOptions.block_id,
  145. language: this._sCurrentLang,
  146. });
  147. // remove previous popup
  148. this.removePopup();
  149. // show new popup
  150. $(window).dolPopupAjax({url: sActionUrl, closeOnOuterClick: false});
  151. };
  152. function bx_wiki_add_block(e, sPage, iCellId, sActionUri) {
  153. var sActionUrl = sUrlRoot + 'r.php?_q=' + sActionUri + '-action/';
  154. bx_loading($(e), true);
  155. $.post(sActionUrl + 'add', {page: sPage, cell_id: iCellId}, function (oData) {
  156. bx_loading($(e), false);
  157. if ('undefined' !== typeof(oData.code)) {
  158. if (0 == oData.code) {
  159. $(e).parents('.bx-wiki-add-block').before('<div class="bx-page-block-container bx-def-padding-sec-topbottom" id="bx-page-block-' + oData.block_id + '">');
  160. loadDynamicBlock(oData.block_id, document.location.href);
  161. }
  162. else {
  163. bx_alert(oData.msg);
  164. }
  165. }
  166. }, 'json');
  167. }
  168. function bx_wiki_insert_img (sEditorId, sImgUrl, sImgName) {
  169. var s = "![" + sImgName + "](" + sImgUrl + ")";
  170. var e = $(sEditorId).size() ? $(sEditorId).get(0) : false;
  171. if (!e)
  172. return;
  173. if (e.setRangeText) {
  174. e.setRangeText(s);
  175. }
  176. else {
  177. e.focus();
  178. document.execCommand('insertText', false, s);
  179. }
  180. }
  181. function bx_wiki_remove_img (sEditorId, sImgUrl, sImgName) {
  182. var sVal = $(sEditorId).val();
  183. if (!sVal)
  184. return;
  185. var sImg = "![IMGNAMEHERE](" + sImgUrl + ")";
  186. sImg = bx_regexp_escape(sImg);
  187. sImg = sImg.replace(/IMGNAMEHERE/, '(.*?)');
  188. var re = new RegExp(sImg, 'gm');
  189. sVal = sVal.replace(re, '');
  190. $(sEditorId).val(sVal);
  191. }
  192. function bx_wiki_open_editor(sSel, bBackground) {
  193. const e = $(sSel).size() ? $(sSel)[0] : false;
  194. if (!e)
  195. return;
  196. bx_wiki_open_editor_convert_links(e.value, function (s) {
  197. // Open the iframe
  198. const oStackedit = new Stackedit();
  199. oStackedit.openFile({
  200. content: {
  201. text: s // and the Markdown content.
  202. }
  203. }, 'undefined' === typeof(bBackground) ? false : bBackground);
  204. // Listen to StackEdit events and apply the changes to the textarea.
  205. oStackedit.on('fileChange', (file) => {
  206. e.value = bx_wiki_open_editor_convert_links_back(file.content.text);
  207. });
  208. });
  209. }
  210. function bx_wiki_open_editor_convert_links(s, onSuccess) {
  211. var sActionUrl = sUrlRoot + 'r.php?_q=wiki-action/';
  212. $.post(sActionUrl + 'convert-links', {s: s}, function (oData) {
  213. if ('undefined' !== typeof(oData.s))
  214. onSuccess(oData.s);
  215. }, 'json');
  216. }
  217. function bx_wiki_open_editor_convert_links_back(s) {
  218. var re = new RegExp('\\\(' + bx_regexp_escape(sUrlRoot) + 's/([a-zA-Z0-9_]+)/([a-zA-Z0-9]+)[^\\\)]*\\\)', 'gm');
  219. s = s.replace(re, "($1/$2)");
  220. var sRefs = '<!-- ' + $('<textarea />').html(_t('_sys_wiki_external_editor_references_comment')).text() + ' -->';
  221. var i = s.indexOf(sRefs);
  222. if (-1 !== i)
  223. s = s.substring(0, i);
  224. return s;
  225. }
  226. /** @} */