options.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. For licensing, see LICENSE.html or http://ckeditor.com/license
  4. */
  5. CKEDITOR.dialog.add( 'scaytDialog', function( editor ) {
  6. var scayt_instance = editor.scayt;
  7. var aboutTabDefinition = '<p><img src="' + scayt_instance.getLogo() + '" /></p>' +
  8. '<p>' + scayt_instance.getLocal('version') + scayt_instance.getVersion() + '</p>' +
  9. '<p>' + scayt_instance.getLocal('text_copyrights') + '</p>';
  10. var doc = CKEDITOR.document;
  11. var optionGenerator = function() {
  12. var scayt_instance_ = editor.scayt,
  13. applicationConfig = scayt_instance.getApplicationConfig(),
  14. optionArrayUiCheckboxes = [],
  15. optionLocalizationList = {
  16. "ignore-all-caps-words" : "label_allCaps",
  17. "ignore-domain-names" : "label_ignoreDomainNames",
  18. "ignore-words-with-mixed-cases" : "label_mixedCase",
  19. "ignore-words-with-numbers" : "label_mixedWithDigits"
  20. };
  21. for(var option in applicationConfig) {
  22. var checkboxConfig = {
  23. type: "checkbox"
  24. };
  25. checkboxConfig.id = option;
  26. checkboxConfig.label = scayt_instance.getLocal(optionLocalizationList[option]);
  27. optionArrayUiCheckboxes.push(checkboxConfig);
  28. }
  29. return optionArrayUiCheckboxes;
  30. };
  31. var languageModelState = {
  32. isChanged : function() {
  33. return (this.newLang === null || this.currentLang === this.newLang) ? false : true;
  34. },
  35. currentLang: scayt_instance.getLang(),
  36. newLang: null,
  37. reset: function() {
  38. this.currentLang = scayt_instance.getLang();
  39. this.newLang = null;
  40. },
  41. id: 'lang'
  42. };
  43. var generateDialogTabs = function(tabsList, editor) {
  44. var tabs = [],
  45. uiTabs = editor.config.scayt_uiTabs;
  46. if(!uiTabs) {
  47. return tabsList;
  48. } else {
  49. for(var i in uiTabs) {
  50. (uiTabs[i] == 1) && tabs.push(tabsList[i]);
  51. }
  52. tabs.push(tabsList[tabsList.length - 1]);
  53. }
  54. return tabs;
  55. };
  56. var dialogTabs = [{
  57. id : 'options',
  58. label : scayt_instance.getLocal('tab_options'),
  59. onShow: function() {
  60. // console.log("tab show");
  61. },
  62. elements : [
  63. {
  64. type: 'vbox',
  65. id: 'scaytOptions',
  66. children: optionGenerator(),
  67. onShow: function() {
  68. var optionsTab = this.getChild(),
  69. scayt_instance = editor.scayt;
  70. for(var i = 0; i < this.getChild().length; i++) {
  71. this.getChild()[i].setValue(scayt_instance.getApplicationConfig()[this.getChild()[i].id]);
  72. }
  73. }
  74. }
  75. ]
  76. },
  77. {
  78. id : 'langs',
  79. label : scayt_instance.getLocal('tab_languages'),
  80. elements : [
  81. {
  82. id: "leftLangColumn",
  83. type: 'vbox',
  84. align: 'left',
  85. widths: ['100'],
  86. children: [
  87. {
  88. type: 'html',
  89. id: 'langBox',
  90. style: 'overflow: hidden; white-space: normal;',
  91. html: '<div><div style="float:left;width:45%;margin-left:5px;" id="left-col-' + editor.name + '"></div><div style="float:left;width:45%;margin-left:15px;" id="right-col-' + editor.name + '"></div></div>',
  92. onShow: function() {
  93. var scayt_instance = editor.scayt;
  94. var lang = scayt_instance.getLang(),
  95. prefix_id = "scaytLang_",
  96. radio = doc.getById(prefix_id + lang);
  97. radio.$.checked = true;
  98. }
  99. }
  100. ]
  101. }
  102. ]
  103. },
  104. {
  105. id : 'dictionaries',
  106. label : scayt_instance.getLocal('tab_dictionaries'),
  107. elements : [
  108. {
  109. type: 'vbox',
  110. id: 'rightCol_col__left',
  111. children: [
  112. {
  113. type: 'html',
  114. id: 'dictionaryNote',
  115. html: ''
  116. },
  117. {
  118. type: 'text',
  119. id: 'dictionaryName',
  120. label: scayt_instance.getLocal('label_fieldNameDic') || 'Dictionary name',
  121. onShow: function(data) {
  122. var dialog = data.sender,
  123. scayt_instance = editor.scayt;
  124. // IE7 specific fix
  125. setTimeout(function() {
  126. // clear dictionaryNote field
  127. dialog.getContentElement("dictionaries", "dictionaryNote").getElement().setText('');
  128. // restore/clear dictionaryName field
  129. if(scayt_instance.getUserDictionaryName() != null && scayt_instance.getUserDictionaryName() != '') {
  130. dialog.getContentElement("dictionaries", "dictionaryName").setValue(scayt_instance.getUserDictionaryName());
  131. }
  132. }, 0);
  133. }
  134. },
  135. {
  136. type: 'hbox',
  137. id: 'notExistDic',
  138. align: 'left',
  139. style: 'width:auto;',
  140. widths: [ '50%', '50%' ],
  141. children: [
  142. {
  143. type: 'button',
  144. id: 'createDic',
  145. label: scayt_instance.getLocal('btn_createDic'),
  146. title: scayt_instance.getLocal('btn_createDic'),
  147. onClick: function() {
  148. var dialog = this.getDialog(),
  149. self = dialogDefinition,
  150. scayt_instance = editor.scayt,
  151. name = dialog.getContentElement("dictionaries", "dictionaryName").getValue();
  152. scayt_instance.createUserDictionary(name, function(response) {
  153. if(!response.error) {
  154. self.toggleDictionaryButtons.call(dialog, true);
  155. }
  156. response.dialog = dialog;
  157. response.command = "create";
  158. response.name = name;
  159. editor.fire("scaytUserDictionaryAction", response);
  160. }, function(error) {
  161. error.dialog = dialog;
  162. error.command = "create";
  163. error.name = name;
  164. editor.fire("scaytUserDictionaryActionError", error);
  165. });
  166. }
  167. },
  168. {
  169. type: 'button',
  170. id: 'restoreDic',
  171. label: scayt_instance.getLocal('btn_restoreDic'),
  172. title: scayt_instance.getLocal('btn_restoreDic'),
  173. onClick: function() {
  174. var dialog = this.getDialog(),
  175. scayt_instance = editor.scayt,
  176. self = dialogDefinition,
  177. name = dialog.getContentElement("dictionaries", "dictionaryName").getValue();
  178. scayt_instance.restoreUserDictionary(name, function(response) {
  179. response.dialog = dialog;
  180. if(!response.error) {
  181. self.toggleDictionaryButtons.call(dialog, true);
  182. }
  183. response.command = "restore";
  184. response.name = name;
  185. editor.fire("scaytUserDictionaryAction", response);
  186. }, function(error) {
  187. error.dialog = dialog;
  188. error.command = "restore";
  189. error.name = name;
  190. editor.fire("scaytUserDictionaryActionError", error);
  191. });
  192. }
  193. }
  194. ]
  195. },
  196. {
  197. type: 'hbox',
  198. id: 'existDic',
  199. align: 'left',
  200. style: 'width:auto;',
  201. widths: [ '50%', '50%' ],
  202. children: [
  203. {
  204. type: 'button',
  205. id: 'removeDic',
  206. label: scayt_instance.getLocal('btn_deleteDic'),
  207. title: scayt_instance.getLocal('btn_deleteDic'),
  208. onClick: function() {
  209. var dialog = this.getDialog(),
  210. scayt_instance = editor.scayt,
  211. self = dialogDefinition,
  212. dictionaryNameField = dialog.getContentElement("dictionaries", "dictionaryName"),
  213. name = dictionaryNameField.getValue();
  214. scayt_instance.removeUserDictionary(name, function(response) {
  215. dictionaryNameField.setValue("");
  216. if(!response.error) {
  217. self.toggleDictionaryButtons.call(dialog, false);
  218. }
  219. response.dialog = dialog;
  220. response.command = "remove";
  221. response.name = name;
  222. editor.fire("scaytUserDictionaryAction", response);
  223. }, function(error) {
  224. error.dialog = dialog;
  225. error.command = "remove";
  226. error.name = name;
  227. editor.fire("scaytUserDictionaryActionError", error);
  228. });
  229. }
  230. },
  231. {
  232. type: 'button',
  233. id: 'renameDic',
  234. label: scayt_instance.getLocal('btn_renameDic'),
  235. title: scayt_instance.getLocal('btn_renameDic'),
  236. onClick: function() {
  237. var dialog = this.getDialog(),
  238. scayt_instance = editor.scayt,
  239. name = dialog.getContentElement("dictionaries", "dictionaryName").getValue();
  240. scayt_instance.renameUserDictionary(name, function(response) {
  241. response.dialog = dialog;
  242. response.command = "rename";
  243. response.name = name;
  244. editor.fire("scaytUserDictionaryAction", response);
  245. }, function(error) {
  246. error.dialog = dialog;
  247. error.command = "rename";
  248. error.name = name;
  249. editor.fire("scaytUserDictionaryActionError", error);
  250. });
  251. }
  252. }
  253. ]
  254. },
  255. {
  256. type: 'html',
  257. id: 'dicInfo',
  258. html: '<div id="dic_info_editor1" style="margin:5px auto; width:95%;white-space:normal;">' + scayt_instance.getLocal('text_descriptionDic') + '</div>'
  259. }
  260. ]
  261. }
  262. ]
  263. },
  264. {
  265. id : 'about',
  266. label : scayt_instance.getLocal('tab_about'),
  267. elements : [
  268. {
  269. type : 'html',
  270. id : 'about',
  271. style : 'margin: 5px 5px;',
  272. html : '<div><div id="scayt_about_">' +
  273. aboutTabDefinition +
  274. '</div></div>'
  275. }
  276. ]
  277. }];
  278. editor.on("scaytUserDictionaryAction", function(event) {
  279. var UILib = SCAYT.prototype.UILib,
  280. dialog = event.data.dialog,
  281. dictionaryNote = dialog.getContentElement("dictionaries", "dictionaryNote").getElement(),
  282. scayt_instance = event.editor.scayt,
  283. messageTemplate;
  284. if(event.data.error === undefined) {
  285. // success message
  286. messageTemplate = scayt_instance.getLocal("message_success_" + event.data.command + "Dic");
  287. messageTemplate = messageTemplate.replace('%s', event.data.name);
  288. dictionaryNote.setText(messageTemplate);
  289. UILib.css(dictionaryNote.$, {color: 'blue'});
  290. } else {
  291. // error message
  292. if(event.data.name === '') {
  293. // empty dictionary name
  294. dictionaryNote.setText(scayt_instance.getLocal('message_info_emptyDic'));
  295. } else {
  296. messageTemplate = scayt_instance.getLocal("message_error_" + event.data.command + "Dic");
  297. messageTemplate = messageTemplate.replace('%s', event.data.name);
  298. dictionaryNote.setText(messageTemplate);
  299. }
  300. UILib.css(dictionaryNote.$, {color: 'red'});
  301. if(scayt_instance.getUserDictionaryName() != null && scayt_instance.getUserDictionaryName() != '') {
  302. dialog.getContentElement("dictionaries", "dictionaryName").setValue(scayt_instance.getUserDictionaryName());
  303. } else {
  304. dialog.getContentElement("dictionaries", "dictionaryName").setValue("");
  305. }
  306. }
  307. });
  308. editor.on("scaytUserDictionaryActionError", function(event) {
  309. var UILib = SCAYT.prototype.UILib,
  310. dialog = event.data.dialog,
  311. dictionaryNote = dialog.getContentElement("dictionaries", "dictionaryNote").getElement(),
  312. scayt_instance = event.editor.scayt,
  313. messageTemplate;
  314. if(event.data.name === '') {
  315. // empty dictionary name
  316. dictionaryNote.setText(scayt_instance.getLocal('message_info_emptyDic'));
  317. } else {
  318. messageTemplate = scayt_instance.getLocal("message_error_" + event.data.command + "Dic");
  319. messageTemplate = messageTemplate.replace('%s', event.data.name);
  320. dictionaryNote.setText(messageTemplate);
  321. }
  322. UILib.css(dictionaryNote.$, {color: 'red'});
  323. if(scayt_instance.getUserDictionaryName() != null && scayt_instance.getUserDictionaryName() != '') {
  324. dialog.getContentElement("dictionaries", "dictionaryName").setValue(scayt_instance.getUserDictionaryName());
  325. } else {
  326. dialog.getContentElement("dictionaries", "dictionaryName").setValue("");
  327. }
  328. });
  329. var plugin = CKEDITOR.plugins.scayt;
  330. var dialogDefinition = {
  331. title: scayt_instance.getLocal('text_title'),
  332. resizable: CKEDITOR.DIALOG_RESIZE_BOTH,
  333. minWidth: 340,
  334. minHeight: 260,
  335. onLoad: function() {
  336. if(editor.config.scayt_uiTabs[1] == 0) {
  337. return;
  338. }
  339. var dialog = this,
  340. self = dialogDefinition,
  341. langBoxes = self.getLangBoxes.call(dialog);
  342. langBoxes.getParent().setStyle("white-space", "normal");
  343. //dialog.data = editor.fire( 'scaytDialog', {} );
  344. self.renderLangList(langBoxes);
  345. var scayt_instance = editor.scayt;
  346. this.definition.minWidth = this.getSize().width;
  347. this.resize(this.definition.minWidth, this.definition.minHeight);
  348. },
  349. onCancel: function() {
  350. languageModelState.reset();
  351. },
  352. onHide: function() {
  353. editor.unlockSelection();
  354. },
  355. onShow: function() {
  356. editor.fire("scaytDialogShown", this);
  357. if(editor.config.scayt_uiTabs[2] == 0) {
  358. return;
  359. }
  360. var scayt_instance = editor.scayt,
  361. self = dialogDefinition,
  362. dialog = this,
  363. dictionaryNameField = dialog.getContentElement("dictionaries", "dictionaryName"),
  364. existance = dialog.getContentElement("dictionaries", "existDic").getElement().getParent(),
  365. notExistance = dialog.getContentElement("dictionaries", "notExistDic").getElement().getParent();
  366. existance.hide();
  367. notExistance.hide();
  368. if(scayt_instance.getUserDictionaryName() != null && scayt_instance.getUserDictionaryName() != '') {
  369. dialog.getContentElement("dictionaries", "dictionaryName").setValue(scayt_instance.getUserDictionaryName());
  370. existance.show();
  371. } else {
  372. dictionaryNameField.setValue("");
  373. notExistance.show();
  374. }
  375. },
  376. onOk: function() {
  377. var dialog = this,
  378. self = dialogDefinition,
  379. scayt_instance = editor.scayt,
  380. scaytOptions = dialog.getContentElement("options", "scaytOptions"),
  381. changedOptions = self.getChangedOption.call(dialog);
  382. scayt_instance.commitOption({ changedOptions: changedOptions });
  383. },
  384. toggleDictionaryButtons: function(exist) {
  385. var existance = this.getContentElement("dictionaries", "existDic").getElement().getParent(),
  386. notExistance = this.getContentElement("dictionaries", "notExistDic").getElement().getParent();
  387. if(exist) {
  388. existance.show();
  389. notExistance.hide();
  390. } else {
  391. existance.hide();
  392. notExistance.show();
  393. }
  394. },
  395. getChangedOption: function() {
  396. var changedOption = {};
  397. if(editor.config.scayt_uiTabs[0] == 1) {
  398. var dialog = this,
  399. scaytOptions = dialog.getContentElement("options", "scaytOptions").getChild();
  400. for(var i = 0; i < scaytOptions.length; i++) {
  401. if(scaytOptions[i].isChanged()) {
  402. changedOption[scaytOptions[i].id] = scaytOptions[i].getValue();
  403. }
  404. }
  405. }
  406. if(languageModelState.isChanged()) {
  407. changedOption[languageModelState.id] = editor.config.scayt_sLang = languageModelState.currentLang = languageModelState.newLang;
  408. }
  409. return changedOption;
  410. },
  411. buildRadioInputs: function(key, value) {
  412. var divContainer = new CKEDITOR.dom.element( 'div' ),
  413. doc = CKEDITOR.document,
  414. div = doc.createElement( 'div' ),
  415. id = "scaytLang_" + value,
  416. radio = CKEDITOR.dom.element.createFromHtml( '<input id="' +
  417. id + '" type="radio" ' +
  418. ' value="' + value + '" name="scayt_lang" />' ),
  419. radioLabel = new CKEDITOR.dom.element( 'label' ),
  420. scayt_instance = editor.scayt;
  421. divContainer.setStyles({
  422. "white-space": "normal",
  423. 'position': 'relative',
  424. 'padding-bottom': '2px'
  425. });
  426. radio.on( 'click', function(data) {
  427. languageModelState.newLang = data.sender.getValue();
  428. });
  429. radioLabel.appendText(key);
  430. radioLabel.setAttribute("for", id);
  431. divContainer.append(radio);
  432. divContainer.append(radioLabel);
  433. if(value === scayt_instance.getLang()) {
  434. radio.setAttribute("checked", true);
  435. radio.setAttribute('defaultChecked', 'defaultChecked');
  436. }
  437. return divContainer;
  438. },
  439. renderLangList: function(langBoxes) {
  440. var dialog = this,
  441. leftCol = langBoxes.find('#left-col-' + editor.name).getItem(0),
  442. rightCol = langBoxes.find('#right-col-' + editor.name).getItem(0),
  443. langList = scayt_instance.getLangList(),
  444. mergedLangList = {},
  445. sortable = [],
  446. counter = 0,
  447. half, lang;
  448. for(lang in langList.ltr) {
  449. mergedLangList[lang] = langList.ltr[lang];
  450. }
  451. for(lang in langList.rtl) {
  452. mergedLangList[lang] = langList.rtl[lang];
  453. }
  454. // sort alphabetically lang list
  455. for(lang in mergedLangList) {
  456. sortable.push([lang, mergedLangList[lang]]);
  457. }
  458. sortable.sort(function(a, b) {
  459. var result = 0;
  460. if(a[1] > b[1]) {
  461. result = 1;
  462. } else if(a[1] < b[1]) {
  463. result = -1;
  464. }
  465. return result;
  466. });
  467. mergedLangList = {};
  468. for(var i = 0; i < sortable.length; i++) {
  469. mergedLangList[sortable[i][0]] = sortable[i][1];
  470. }
  471. half = Math.round(sortable.length / 2);
  472. for(lang in mergedLangList) {
  473. counter++;
  474. dialog.buildRadioInputs(mergedLangList[lang], lang).appendTo(counter <= half ? leftCol : rightCol);
  475. }
  476. },
  477. getLangBoxes: function() {
  478. var dialog = this,
  479. langboxes = dialog.getContentElement("langs", "langBox").getElement();
  480. return langboxes;
  481. },
  482. contents: generateDialogTabs(dialogTabs, editor)
  483. };
  484. return dialogDefinition;
  485. });