script.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. class ClassPage {
  2. static ModeCookieName = 'doczilla-js-docs-class-page-mode';
  3. static Mode = {
  4. Tabs: 'mode-tabs',
  5. List: 'mode-list'
  6. };
  7. static StyleClasses = {
  8. Selected: 'selected',
  9. Active: 'active',
  10. CtrlPressed: 'ctrl-pressed',
  11. ShiftPressed: 'shift-pressed',
  12. ParentsBranch: 'parents-branch',
  13. ClassItem: 'class-item',
  14. ClassName: 'class-name',
  15. ClassIcon: 'class-icon',
  16. Filler: 'filler',
  17. FullSourcePrompt: 'full-source-prompt',
  18. FullSourcePromptText: 'full-source-prompt-text',
  19. FullSourcePromptButton: 'full-source-prompt-button',
  20. Spacing: 'spacing',
  21. CmLink: 'cm-link',
  22. Z8Locale: 'z8-locale',
  23. Hidden: 'hidden',
  24. Collapsed: 'collapsed',
  25. Highlighted: 'highlighted',
  26. Readonly: 'readonly',
  27. Clickable: 'clickable',
  28. Empty: 'empty',
  29. White: 'white'
  30. };
  31. static Attributes = {
  32. DataTab: 'data-tab',
  33. DataDisplayMode: 'data-display-mode',
  34. DataClassName: 'data-class-name',
  35. OnClick: 'onclick',
  36. DataPropertyName: 'data-property-name',
  37. DataPropertyType: 'data-property-type',
  38. DataPropertyParent: 'data-property-parent',
  39. DataPropertyInherited: 'data-property-inherited',
  40. DataPropertyDynamic: 'data-property-dynamic'
  41. };
  42. static Messages = {
  43. NoMixins: 'This class has no mixins.',
  44. NoChildren: 'This class has no child classes.',
  45. NoMixedIn: 'This class is not mixed in any classes.',
  46. NoParents: 'This is a base class, which has no parent classes.',
  47. ShowFullSourceText: 'There were found another entities in the source file of this class. Would you like to see full source file?',
  48. HideFullSourceText: 'Full source file shown. Would you like to hide all entities except the target class?',
  49. PromptButtonText: 'OK',
  50. CmLinkTipPrefix: 'Ctrl+Click to go to class'
  51. };
  52. static TabNames = {
  53. Editor: 'Editor',
  54. Methods: 'Methods',
  55. Parents: 'Parents',
  56. Properties: 'Properties',
  57. Mixins: 'Mixins',
  58. Children: 'Children',
  59. MixedIn: 'MixedIn'
  60. };
  61. static PropertyType = {
  62. Statics: 'statics',
  63. Base: 'base',
  64. Overridden: 'overridden',
  65. Dynamic: 'dynamic',
  66. Inherited: 'inherited'
  67. };
  68. static PropertyLabel = {
  69. Statics: 'Static properties',
  70. Base: 'Base properties',
  71. Overridden: 'Overridden properties',
  72. Dynamic: 'Dynamic properties',
  73. Inherited: 'Inherited properties'
  74. };
  75. static MethodLabel = {
  76. Statics: 'Static methods',
  77. Base: 'Base methods',
  78. Overridden: 'Overridden methods',
  79. Dynamic: 'Dynamic methods',
  80. Inherited: 'Inherited methods'
  81. };
  82. static ClassProperties = {
  83. Name: 'name',
  84. Methods: 'methods',
  85. Properties: 'properties',
  86. Children: 'children',
  87. Mixins: 'mixins',
  88. MixedIn: 'mixedIn',
  89. ParentsBranch: 'parentsBranch',
  90. Statics: 'statics',
  91. DynamicProperties: 'dynamicProperties',
  92. Root: 'root'
  93. };
  94. static ContextMenuType = {
  95. PropertyItem: 'property-item'
  96. };
  97. start() {
  98. if(typeof Class === 'string') {
  99. return this;
  100. }
  101. this.tabElements = {
  102. [ClassPage.TabNames.Editor]: DOM.get('.tab.editor'),
  103. [ClassPage.TabNames.Methods]: DOM.get('.tab.methods'),
  104. [ClassPage.TabNames.Parents]: DOM.get('.tab.parents'),
  105. [ClassPage.TabNames.Properties]: DOM.get('.tab.properties'),
  106. [ClassPage.TabNames.Mixins]: DOM.get('.tab.mixins'),
  107. [ClassPage.TabNames.Children]: DOM.get('.tab.children'),
  108. [ClassPage.TabNames.MixedIn]: DOM.get('.tab.mixedin')
  109. };
  110. this.contentElements = {
  111. [ClassPage.TabNames.Editor]: DOM.get('.content-tab#editor'),
  112. [ClassPage.TabNames.Methods]: DOM.get('.content-tab#methods'),
  113. [ClassPage.TabNames.Parents]: DOM.get('.content-tab#parents'),
  114. [ClassPage.TabNames.Properties]: DOM.get('.content-tab#properties'),
  115. [ClassPage.TabNames.Mixins]: DOM.get('.content-tab#mixins'),
  116. [ClassPage.TabNames.Children]: DOM.get('.content-tab#children'),
  117. [ClassPage.TabNames.MixedIn]: DOM.get('.content-tab#mixedin')
  118. };
  119. this.documented = Object.keys(Comments).filter((key) => { return Comments[key].text.length > 0; }).length;
  120. this.documentable = 0;
  121. this.inheritedCommentsQuery = {};
  122. this.inheritedCommentsFields = {};
  123. this.propertyItemElements = {};
  124. this.documentedPercentage = DOM.get('.class-documented-percentage');
  125. const rightContainer = this.rightContainer = DOM.get('.right');
  126. const modeCookieValue = DOM.getCookieProperty(App.CookieName, ClassPage.ModeCookieName);
  127. if(!modeCookieValue)
  128. DOM.setCookieProperty(App.CookieName, ClassPage.ModeCookieName, ClassPage.Mode.Tabs);
  129. const mode = this.mode = modeCookieValue || ClassPage.Mode.Tabs;
  130. const tabsModeButton = this.tabsModeButton = DOM.get('.display-mode-button.mode-tabs');
  131. const listModeButton = this.listModeButton = DOM.get('.display-mode-button.mode-list');
  132. /* >>> Context menu */
  133. this.contextMenu = DOM.get('.context-menu');
  134. this.contextMenuItems = {};
  135. /* <<< Context menu */
  136. (mode === ClassPage.Mode.Tabs ? tabsModeButton : listModeButton).addClass(ClassPage.StyleClasses.Selected);
  137. rightContainer.addClass(mode);
  138. this.renderContent();
  139. this.markContentInEditor();
  140. this.registerEventListeners();
  141. this.applyHash();
  142. this.openSocket();
  143. return this;
  144. }
  145. switchMode(mode) {
  146. this.rightContainer.removeClass(this.mode);
  147. this.mode = mode;
  148. DOM.setCookieProperty(App.CookieName, ClassPage.ModeCookieName, mode);
  149. this.rightContainer.addClass(mode);
  150. this.codeMirrorEditor.cmRefresh();
  151. }
  152. selectTab(tab) {
  153. tab = typeof tab === 'string' ? this.tabElements[tab] : tab;
  154. const selectedTab = this.selectedTab;
  155. let filler = selectedTab ? selectedTab.getFirstChild('.filler') : null;
  156. if(!filler)
  157. filler = DOM.create({ tag: DOM.Tags.Div, cls: ClassPage.StyleClasses.Filler });
  158. selectedTab && selectedTab.removeClass(ClassPage.StyleClasses.Selected);
  159. this.selectedTab = tab.addClass(ClassPage.StyleClasses.Selected);
  160. this.selectedTab.append(filler);
  161. }
  162. activateContent(content) {
  163. content = typeof content === 'string' ? this.contentElements[content] : content;
  164. if(this.activeContent)
  165. this.activeContent.removeClass(ClassPage.StyleClasses.Active);
  166. this.activeContent = content.addClass(ClassPage.StyleClasses.Active);
  167. if(content === this.contentElements[ClassPage.TabNames.Editor])
  168. this.codeMirrorEditor.cmRefresh();
  169. if(content === this.contentElements[ClassPage.TabNames.Properties] || content === this.contentElements[ClassPage.TabNames.Methods])
  170. DOM.getAll('.property-item-comment-input').forEach((item) => {
  171. item.style('height', `${Math.min(422, item.get().scrollHeight + 2)}px`);
  172. });
  173. }
  174. registerEventListeners() {
  175. this.registerTabsEventListeners();
  176. this.registerModeButtonsEventListeners();
  177. }
  178. registerTabsEventListeners() {
  179. const tabElements = this.tabElements;
  180. for(const tabName of Object.keys(tabElements)) {
  181. tabElements[tabName].on(DOM.Events.Click, this.onTabClick.bind(this));
  182. }
  183. }
  184. registerModeButtonsEventListeners() {
  185. this.tabsModeButton.on(DOM.Events.Click, this.onModeButtonClick.bind(this));
  186. this.listModeButton.on(DOM.Events.Click, this.onModeButtonClick.bind(this));
  187. }
  188. prepareSource() {
  189. const className = Class[ClassPage.ClassProperties.Name].replaceAll('.', '\\.');
  190. const classRx = new RegExp(`Z8\\.define\\(\'${className}\',\\s*\\{(?:.|[\r\n])+?^\\}\\);?`, 'gm');
  191. const classSourceMatch = ClassSource.match(classRx);
  192. if(!classSourceMatch) { // remove after `}\n);` issue fixed...
  193. this.sourceHasAnotherEntities = false;
  194. return this.classSource = ClassSource;
  195. }
  196. const classSource = this.classSource = classSourceMatch[0];
  197. this.sourceHasAnotherEntities = ClassSource.trim() !== classSource;
  198. return classSource;
  199. }
  200. renderContent() {
  201. this.renderEditor();
  202. this.renderParents();
  203. this.renderMixins();
  204. this.renderChildren();
  205. this.renderMixedIn();
  206. this.renderProperties();
  207. this.renderMethods();
  208. this.renderDocumentedPercentage();
  209. this.loadInheritedComments();
  210. }
  211. renderDocumentedPercentage() {
  212. this.documentedPercentage.setInnerHTML(`${Math.round(this.documented/this.documentable * 10000) / 100}%`);
  213. }
  214. renderEditor() {
  215. this.codeMirrorEditor = CodeMirror(DOM.get('#editor').get(), {
  216. [App.CodeMirrorProperties.Value]: this.prepareSource(ClassSource),
  217. [App.CodeMirrorProperties.Mode]: 'javascript',
  218. [App.CodeMirrorProperties.Theme]: 'darcula',
  219. [App.CodeMirrorProperties.Readonly]: true,
  220. [App.CodeMirrorProperties.LineNumbers]: true,
  221. [App.CodeMirrorProperties.MatchBrackets]: true,
  222. [App.CodeMirrorProperties.ScrollbarStyle]: 'overlay',
  223. [App.CodeMirrorProperties.ConfigureMouse]: (cm, repeat, ev) => {
  224. return { 'addNew': false };
  225. },
  226. });
  227. if(this.sourceHasAnotherEntities)
  228. this.renderFullSourcePrompt();
  229. this.codeMirrorEditorElement = DOM.get('.CodeMirror');
  230. }
  231. renderProperties() {
  232. const propertiesElement = this.contentElements[ClassPage.TabNames.Properties];
  233. const properties = this.getProperties(false);
  234. this.renderPropertiesType(properties, ClassPage.PropertyType.Statics, ClassPage.PropertyLabel.Statics, propertiesElement, false, false);
  235. this.renderPropertiesType(properties, ClassPage.PropertyType.Base, ClassPage.PropertyLabel.Base, propertiesElement, false, false);
  236. this.renderPropertiesType(properties, ClassPage.PropertyType.Overridden, ClassPage.PropertyLabel.Overridden, propertiesElement, false, false);
  237. this.renderPropertiesType(properties, ClassPage.PropertyType.Dynamic, ClassPage.PropertyLabel.Dynamic, propertiesElement, false, false);
  238. this.renderPropertiesType(properties, ClassPage.PropertyType.Inherited, ClassPage.PropertyLabel.Inherited, propertiesElement, true, false);
  239. }
  240. renderMethods() {
  241. const methodsElement = this.contentElements[ClassPage.TabNames.Methods];
  242. const properties = this.getProperties(true);
  243. this.renderPropertiesType(properties, ClassPage.PropertyType.Statics, ClassPage.MethodLabel.Statics, methodsElement, false, true);
  244. this.renderPropertiesType(properties, ClassPage.PropertyType.Base, ClassPage.MethodLabel.Base, methodsElement, false, true);
  245. this.renderPropertiesType(properties, ClassPage.PropertyType.Overridden, ClassPage.MethodLabel.Overridden, methodsElement, false, true);
  246. this.renderPropertiesType(properties, ClassPage.PropertyType.Dynamic, ClassPage.MethodLabel.Dynamic, methodsElement, false, true);
  247. this.renderPropertiesType(properties, ClassPage.PropertyType.Inherited, ClassPage.MethodLabel.Inherited, methodsElement, true, true);
  248. }
  249. // TODO: refactor! Make PropertyItem class
  250. renderPropertiesType(properties, type, headerText, container, initiallyHidden, isMethods) {
  251. properties = properties[type];
  252. if(!properties || properties.length == 0)
  253. return;
  254. const propertyItemClickable = (element) => {
  255. let el = element;
  256. while(!el.hasClass('property-item-comment-static') && !el.hasClass('property-item'))
  257. el = el.getParent();
  258. return element.hasClass('property-item-nearest-parent-span')
  259. || element.hasClass('property-item-comment-input')
  260. || element.hasClass('property-item-comment-button')
  261. || element.hasClass('property-item-saving-filler')
  262. || (el.hasClass('property-item-comment-static') && el.hasClass(ClassPage.StyleClasses.Clickable));
  263. };
  264. if(type !== ClassPage.PropertyType.Inherited) {
  265. this.documentable += properties.length;
  266. } else {
  267. const inheritedCommentsQuery = this.inheritedCommentsQuery;
  268. properties.forEach((prop) => {
  269. if(inheritedCommentsQuery[prop.nearestParent]) {
  270. inheritedCommentsQuery[prop.nearestParent].properties.push(prop.key);
  271. } else {
  272. inheritedCommentsQuery[prop.nearestParent] = { root: prop.nearestParentRoot, className: prop.nearestParent, properties: [prop.key] };
  273. }
  274. });
  275. }
  276. const propertiesHeaderText = DOM.create({ tag: DOM.Tags.Span, cls: 'properties-header-text', innerHTML: headerText });
  277. const propertiesHeaderCollapsedIcon = DOM.create({ tag: DOM.Tags.Div, cls: 'properties-header-collapsed-icon' });
  278. const propertiesHeader = DOM.create({ tag: DOM.Tags.Div, cls: 'properties-header', cn: [propertiesHeaderText, propertiesHeaderCollapsedIcon] }, container);
  279. const propertiesList = DOM.create({ tag: DOM.Tags.Div, cls: 'properties-list' }, container);
  280. if(initiallyHidden) {
  281. propertiesList.addClass(ClassPage.StyleClasses.Hidden);
  282. propertiesHeader.addClass(ClassPage.StyleClasses.Collapsed);
  283. }
  284. for(const property of properties) {
  285. const propertyItem = DOM.create({ tag: DOM.Tags.Div, cls: 'property-item', attr: { [ClassPage.Attributes.DataPropertyName]: property.key, [ClassPage.Attributes.DataPropertyType]: property.type }}, propertiesList).on(DOM.Events.MouseDown, (e) => {
  286. const element = CDElement.get(e.target);
  287. if(e.buttons === DOM.MouseButtons.Right) {
  288. if(element.hasClass('property-item-saving-filler'))
  289. return;
  290. setTimeout(() => {
  291. this.showContextMenu(ClassPage.ContextMenuType.PropertyItem, element, { x: e.pageX, y: e.pageY });
  292. }, 10);
  293. } else if (e.buttons === DOM.MouseButtons.Left) {
  294. if(propertyItemClickable(element))
  295. return;
  296. if(type === ClassPage.PropertyType.Inherited) {
  297. Url.goTo(`/class/${property.nearestParent}#${isMethods ? 'Methods' : 'Properties'}:${property.key}`);
  298. } else {
  299. this.searchPropertyInEditor(isMethods, property.dynamic, property.key);
  300. }
  301. }
  302. });
  303. const itemNameText = DOM.create({ tag: DOM.Tags.Span, innerHTML: isMethods ? 'Signature: ' : 'Name: ' });
  304. const itemNameValue = DOM.create({ tag: DOM.Tags.Span, cls: 'property-item-name-span', innerHTML: isMethods ? property.value : property.key });
  305. DOM.create({ tag: DOM.Tags.Div, cls: 'property-item-name', cn: [itemNameText, itemNameValue] }, propertyItem);
  306. if(type !== ClassPage.PropertyType.Dynamic && !isMethods) {
  307. const itemTypeText = DOM.create({ tag: DOM.Tags.Span, innerHTML: 'Type: ' });
  308. const itemTypeValue = DOM.create({ tag: DOM.Tags.Span, cls: 'property-item-type-span', innerHTML: property.type });
  309. DOM.create({ tag: DOM.Tags.Div, cls: 'property-item-type', cn: [itemTypeText, itemTypeValue] }, propertyItem);
  310. }
  311. if(type !== ClassPage.PropertyType.Dynamic && !isMethods && property.type !== 'undefined') {
  312. const itemValueText = DOM.create({ tag: DOM.Tags.Span, innerHTML: 'Default value: ' });
  313. const itemValueValue = DOM.create({ tag: DOM.Tags.Span, cls: 'property-item-default-value-span', innerHTML: property.type === 'string' ? `'${property.value}'` : property.value });
  314. DOM.create({ tag: DOM.Tags.Div, cls: 'property-item-default-value', cn: [itemValueText, itemValueValue] }, propertyItem);
  315. }
  316. if(type !== ClassPage.PropertyType.Dynamic && type !== ClassPage.PropertyType.Statics && type !== ClassPage.PropertyType.Base) {
  317. const itemParentText = DOM.create({ tag: DOM.Tags.Span, innerHTML: 'Nearest parent: ' });
  318. const itemParentValue = DOM.create({ tag: DOM.Tags.Span, cls: 'property-item-nearest-parent-span', innerHTML: property.nearestParent }).on('click', (e) => {
  319. Url.goTo(`/class/${property.nearestParent}`);
  320. });
  321. DOM.create({ tag: DOM.Tags.Div, cls: 'property-item-nearest-parent', cn: [itemParentText, itemParentValue] }, propertyItem);
  322. propertyItem.setAttribute(ClassPage.Attributes.DataPropertyParent, property.nearestParent);
  323. }
  324. if(type === ClassPage.PropertyType.Dynamic) {
  325. propertyItem.setAttribute(ClassPage.Attributes.DataPropertyDynamic, 'true');
  326. }
  327. const itemCommentText = DOM.create({ tag: DOM.Tags.Div, innerHTML: 'Comment:' });
  328. const itemCommentCn = [itemCommentText];
  329. const loadedComment = Comments[type === ClassPage.PropertyType.Statics ? `__static__${property.key}` : property.key];
  330. const loadedCommentText = loadedComment && typeof loadedComment === 'object' ? loadedComment.text : '';
  331. const hasComment = loadedComment && typeof loadedComment === 'object' && loadedCommentText.length > 0;
  332. const itemCommentStatic = DOM.create({ tag: DOM.Tags.Div, cls: `property-item-comment-static${!hasComment ? ' empty' : ''}`, innerHTML: hasComment ? CDUtils.nl2br(loadedCommentText) : 'Not commented yet...' });
  333. itemCommentCn.push(itemCommentStatic);
  334. if(type === ClassPage.PropertyType.Inherited) {
  335. this.inheritedCommentsFields[`${property.nearestParent}:${property.key}`] = itemCommentStatic;
  336. propertyItem.setAttribute(ClassPage.Attributes.DataPropertyInherited, 'true');
  337. }
  338. this.propertyItemElements[type === ClassPage.PropertyType.Statics ? `__static__${property.key}` : property.key] = propertyItem;
  339. if(isEditor) {
  340. const itemCommentInput = DOM.create({ tag: DOM.Tags.Textarea, cls: 'property-item-comment-input hidden', attr: { 'placeholder': 'Not commented yet...'} }).setValue(CDUtils.br2nl(loadedCommentText));
  341. itemCommentCn.push(itemCommentInput);
  342. if(type === ClassPage.PropertyType.Inherited) {
  343. itemCommentInput.addClass(ClassPage.StyleClasses.Readonly).setAttribute('readonly', 'true');
  344. } else {
  345. itemCommentStatic.addClass(ClassPage.StyleClasses.Clickable);
  346. itemCommentInput
  347. .on(DOM.Events.KeyDown, this.delayedAdjustCommentInputHeight.bind(this))
  348. .on(DOM.Events.Change, this.adjustCommentInputHeight.bind(this))
  349. .on(DOM.Events.Cut, this.delayedAdjustCommentInputHeight.bind(this))
  350. .on(DOM.Events.Paste, this.delayedAdjustCommentInputHeight.bind(this))
  351. .on(DOM.Events.Drop, this.delayedAdjustCommentInputHeight.bind(this));
  352. const onCommentSave = (e) => {
  353. const commentContent = itemCommentInput.getValue();
  354. if(commentContent === CDUtils.br2nl(itemCommentStatic.getValue()) || commentContent === '' && itemCommentStatic.hasClass('empty'))
  355. return;
  356. const propertyName = `${type === ClassPage.PropertyType.Statics ? '__static__' : ''}${property.key}`;
  357. const className = Class[ClassPage.ClassProperties.Name];
  358. const classRoot = Class[ClassPage.ClassProperties.Root];
  359. propertyItem.addClass('saving');
  360. itemCommentInput.blur();
  361. fetch('/updateComment', {
  362. method: 'POST',
  363. headers: {
  364. 'Content-Type': 'application/x-www-form-urlencoded'
  365. },
  366. body: new URLSearchParams({
  367. 'root': classRoot,
  368. 'class': className,
  369. 'property': propertyName,
  370. 'comment': commentContent
  371. })
  372. }).then((res) => {
  373. if(res.status !== 202) {
  374. propertyItem.removeClass('saving');
  375. console.error(`Comment update failed (${res.status})`);
  376. }
  377. }).catch((e) => {
  378. propertyItem.removeClass('saving');
  379. console.error(`Comment update failed`);
  380. });
  381. };
  382. const itemCommentOkButton = DOM.create({ tag: DOM.Tags.Div, cls: 'property-item-comment-button hidden', innerHTML: 'OK' }).on(DOM.Events.Click, onCommentSave);
  383. itemCommentCn.push(itemCommentOkButton);
  384. itemCommentInput.on(DOM.Events.KeyDown, (e) => {
  385. if(e.key === DOM.Keys.Escape) {
  386. const inputScrollTop = itemCommentInput.get().scrollTop;
  387. itemCommentInput.switchClass(ClassPage.StyleClasses.Hidden);
  388. itemCommentOkButton.switchClass(ClassPage.StyleClasses.Hidden);
  389. itemCommentStatic.switchClass(ClassPage.StyleClasses.Hidden);
  390. itemCommentStatic.get().scrollTop = inputScrollTop;
  391. if(!itemCommentStatic.hasClass('empty'))
  392. itemCommentInput.setValue(CDUtils.br2nl(itemCommentStatic.getValue()));
  393. }
  394. if(e.key === DOM.Keys.Enter && !e.shiftKey) {
  395. onCommentSave();
  396. e.preventDefault();
  397. }
  398. });
  399. itemCommentStatic.on(DOM.Events.Click, (e) => {
  400. itemCommentInput.switchClass(ClassPage.StyleClasses.Hidden);
  401. itemCommentInput.focus();
  402. itemCommentInput.style('height', `${Math.min(422, itemCommentStatic.get().scrollHeight + 2)}px`);
  403. itemCommentInput.get().scrollTop = itemCommentStatic.get().scrollTop;
  404. itemCommentOkButton.switchClass(ClassPage.StyleClasses.Hidden);
  405. itemCommentStatic.switchClass(ClassPage.StyleClasses.Hidden);
  406. });
  407. }
  408. DOM.create({ tag: DOM.Tags.Div, cls: 'property-item-saving-filler' }, propertyItem);
  409. }
  410. if(hasComment)
  411. itemCommentCn.push(this.createCommentDateElement(loadedComment.timestamp));
  412. DOM.create({ tag: DOM.Tags.Div, cls: 'property-item-comment', cn: itemCommentCn }, propertyItem);
  413. }
  414. propertiesHeader.on(DOM.Events.Click, (e) => {
  415. propertiesList.switchClass(ClassPage.StyleClasses.Hidden);
  416. propertiesHeader.switchClass(ClassPage.StyleClasses.Collapsed);
  417. });
  418. }
  419. createCommentDateElement(date) {
  420. const commentDateText = DOM.create({ tag: DOM.Tags.Span, cls: 'property-item-comment-date-text', innerHTML: 'Commented on: ' });
  421. const commentDateDate = DOM.create({ tag: DOM.Tags.Span, cls: 'property-item-comment-date-date', innerHTML: CDUtils.dateFormatUTC(date, 3, 'D.M.Y, H:I:S') });
  422. return DOM.create({ tag: DOM.Tags.Div, cls: 'property-item-comment-date', cn: [commentDateText, commentDateDate] });
  423. }
  424. loadInheritedComments() {
  425. if(Object.keys(this.inheritedCommentsQuery).length === 0 || Object.keys(this.inheritedCommentsFields).length === 0)
  426. return;
  427. fetch('/getInheritedComments', {
  428. method: 'POST',
  429. headers: {
  430. 'Content-Type': 'application/x-www-form-urlencoded'
  431. },
  432. body: new URLSearchParams({
  433. query: JSON.stringify(this.inheritedCommentsQuery)
  434. })
  435. }).then(res => res.json()).then((inheritedComments) => {
  436. for(const cls of Object.keys(inheritedComments)) {
  437. const props = inheritedComments[cls];
  438. for(const prop of Object.keys(props)) {
  439. const element = this.inheritedCommentsFields[`${cls}:${prop}`];
  440. if(element) {
  441. element.setInnerHTML(props[prop].text);
  442. element.removeClass(ClassPage.StyleClasses.Empty);
  443. element.getParent().append(this.createCommentDateElement(props[prop].timestamp));
  444. }
  445. }
  446. }
  447. });
  448. }
  449. getProperties(methods) {
  450. const filter = methods ? (item) => item.type === 'method' : (item) => item.type !== 'method';
  451. const statics = Class[ClassPage.ClassProperties.Statics].filter(filter);
  452. const properties = Class[ClassPage.ClassProperties.Properties].sort((a, b) => a.key.toLowerCase().localeCompare(b.key.toLowerCase())).sort((a, b) => {
  453. return Class[ClassPage.ClassProperties.ParentsBranch].indexOf(a.nearestParent) > Class[ClassPage.ClassProperties.ParentsBranch].indexOf(b.nearestParent) ? -1 : 1;
  454. }).filter(filter);
  455. const dynamicProperties = methods ? [] : Class[ClassPage.ClassProperties.DynamicProperties];
  456. const result = {
  457. [ClassPage.PropertyType.Statics]: statics,
  458. [ClassPage.PropertyType.Base]: properties.filter((item) => !item.inherited),
  459. [ClassPage.PropertyType.Overridden]: properties.filter((item) => item.overridden),
  460. [ClassPage.PropertyType.Inherited]: properties.filter((item) => item.inherited && !item.overridden),
  461. [ClassPage.PropertyType.Dynamic]: dynamicProperties
  462. };
  463. return result;
  464. }
  465. renderMixins() {
  466. const mixinsElement = this.contentElements[ClassPage.TabNames.Mixins];
  467. if(Class[ClassPage.ClassProperties.Mixins].length == 0) {
  468. DOM.create({ tag: DOM.Tags.Div, style: 'font-size: 24px;', innerHTML: ClassPage.Messages.NoMixins }, mixinsElement);
  469. mixinsElement.addClass(ClassPage.StyleClasses.Empty);
  470. return;
  471. }
  472. this.renderClassItems(Class[ClassPage.ClassProperties.Mixins], mixinsElement);
  473. }
  474. renderChildren() {
  475. const childrenElement = this.contentElements[ClassPage.TabNames.Children];
  476. if(Class[ClassPage.ClassProperties.Children].length == 0) {
  477. DOM.create({ tag: DOM.Tags.Div, style: 'font-size: 24px;', innerHTML: ClassPage.Messages.NoChildren }, childrenElement);
  478. childrenElement.addClass(ClassPage.StyleClasses.Empty);
  479. return;
  480. }
  481. this.renderClassItems(Class[ClassPage.ClassProperties.Children], childrenElement);
  482. }
  483. renderMixedIn() {
  484. const mixedInElement = this.contentElements[ClassPage.TabNames.MixedIn];
  485. if(Class[ClassPage.ClassProperties.MixedIn].length == 0) {
  486. DOM.create({ tag: DOM.Tags.Div, style: 'font-size: 24px;', innerHTML: ClassPage.Messages.NoMixedIn }, mixedInElement);
  487. mixedInElement.addClass(ClassPage.StyleClasses.Empty);
  488. return;
  489. }
  490. this.renderClassItems(Class[ClassPage.ClassProperties.MixedIn], mixedInElement);
  491. }
  492. renderFullSourcePrompt() {
  493. const editorContent = this.contentElements[ClassPage.TabNames.Editor];
  494. const prompt = DOM.create({ tag: DOM.Tags.Div, cls: ClassPage.StyleClasses.FullSourcePrompt }, editorContent);
  495. const text = this.fullSourcePromptText = DOM.create({ tag: DOM.Tags.Div, cls: ClassPage.StyleClasses.FullSourcePromptText, innerHTML: ClassPage.Messages.ShowFullSourceText }, prompt);
  496. const button = DOM.create({ tag: DOM.Tags.Div, cls: ClassPage.StyleClasses.FullSourcePromptButton, innerHTML: ClassPage.Messages.PromptButtonText }, prompt);
  497. const onButtonClick = (e) => {
  498. this.switchFullSource();
  499. };
  500. button.on(DOM.Events.Click, onButtonClick.bind(this));
  501. }
  502. switchFullSource(show) {
  503. const shown = this.fullSourceCodeShown = show !== undefined ? !show : !this.fullSourceCodeShown;
  504. this.codeMirrorEditor.cmSetValue(shown ? ClassSource : this.classSource);
  505. if(shown)
  506. this.findAndScrollToTargetClass();
  507. this.codeMirrorEditor.cmRefresh();
  508. this.markContentInEditor();
  509. this.fullSourcePromptText && this.fullSourcePromptText.setInnerHTML(shown ? ClassPage.Messages.HideFullSourceText : ClassPage.Messages.ShowFullSourceText);
  510. return shown;
  511. }
  512. markContentInEditor() {
  513. this.codeMirrorEditor.cmEachLine((lineHandle) => {
  514. this.markExtend(lineHandle);
  515. this.markMixins(lineHandle);
  516. this.markZ8Locales(lineHandle);
  517. this.markNew(lineHandle);
  518. this.markThis(lineHandle);
  519. this.markProperties(lineHandle);
  520. });
  521. }
  522. findAndScrollToTargetClass() {
  523. const className = Class[ClassPage.ClassProperties.Name].replaceAll('.', '\\.');
  524. const editor = this.codeMirrorEditor;
  525. const defineRx = new RegExp(`Z8\\.define\\(\'${className}\',`);
  526. editor.cmEachLine((lineHandle) => {
  527. const text = lineHandle.text;
  528. const match = text.match(defineRx);
  529. if(match) {
  530. editor.scrollIntoView({ line: lineHandle.lineNo(), ch: 0 }, 100);
  531. return;
  532. }
  533. });
  534. }
  535. renderParents() {
  536. const parentsContent = this.contentElements[ClassPage.TabNames.Parents];
  537. const parentsContainer = DOM.create({ tag: DOM.Tags.Div, cls: ClassPage.StyleClasses.ParentsBranch }, parentsContent);
  538. if(Class[ClassPage.ClassProperties.ParentsBranch].length == 0) {
  539. DOM.create({ tag: DOM.Tags.Div, style: 'font-size: 24px;', innerHTML: ClassPage.Messages.NoParents }, parentsContent);
  540. parentsContent.addClass(ClassPage.StyleClasses.Empty);
  541. return;
  542. }
  543. this.renderClassItems(Class[ClassPage.ClassProperties.ParentsBranch], parentsContainer, true);
  544. }
  545. renderClassItems(itemsList, container, withIndent) {
  546. let indent = 0;
  547. if(!withIndent)
  548. itemsList = itemsList.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
  549. for(const cls of itemsList) {
  550. const icon = DOM.create({ tag: DOM.Tags.Div, cls: ClassPage.StyleClasses.ClassIcon });
  551. const name = DOM.create({ tag: DOM.Tags.Div, cls: ClassPage.StyleClasses.ClassName, innerHTML: cls });
  552. const cn = [icon, name];
  553. if(indent > 0 && withIndent)
  554. cn.unshift(DOM.create({ tag: DOM.Tags.Div, cls: ClassPage.StyleClasses.Spacing, style: `width: ${10 * indent}px;` }));
  555. DOM.create({ tag: DOM.Tags.Div, cls: `${ClassPage.StyleClasses.ClassItem}${ withIndent ? ' indent' : '' }`, attr: { [ClassPage.Attributes.DataClassName]: cls }, cn: cn }, container).on(DOM.Events.Click, this.onClassClick.bind(this));
  556. indent++;
  557. }
  558. }
  559. markExtend(lineHandle) {
  560. const editor = this.codeMirrorEditor;
  561. const text = lineHandle.text;
  562. const match = text.match(/extend:\s*['"]?([\w\.]+)['"]?/);
  563. if (match) {
  564. const className = match[1];
  565. const from = { line: lineHandle.lineNo(), ch: match.index + 8 };
  566. const to = { line: lineHandle.lineNo(), ch: match.index + match[0].length };
  567. editor.markText(from, to, {
  568. className: ClassPage.StyleClasses.CmLink,
  569. title: `${ClassPage.Messages.CmLinkTipPrefix} ${className}`,
  570. attributes: {
  571. [ClassPage.Attributes.DataClassName]: className,
  572. [ClassPage.Attributes.OnClick]: 'window.page.onClassLinkClick(this);'
  573. }
  574. });
  575. }
  576. }
  577. markMixins(lineHandle) {
  578. const editor = this.codeMirrorEditor;
  579. const text = lineHandle.text;
  580. const match = text.match(/mixins:\s*(\[.*?\]|\w+)/);
  581. if (match) {
  582. let mixins = match[1].replace(/\[|\]/g, "").split(/\s*,\s*/);
  583. const mixinsStr = match[1].replace(/\[|\]/g, "");
  584. mixins = mixinsStr.split(/\s*,\s*/);
  585. const startIndex = match.index + match[0].indexOf(mixinsStr);
  586. for (var i = 0; i < mixins.length; i++) {
  587. const className = mixins[i].trim().replace(/^['"]|['"]$/g, "");
  588. const classIndex = mixinsStr.indexOf(className);
  589. const from = { line: lineHandle.lineNo(), ch: startIndex + classIndex };
  590. const to = { line: lineHandle.lineNo(), ch: startIndex + classIndex + className.length };
  591. editor.markText(from, to, {
  592. className: ClassPage.StyleClasses.CmLink,
  593. title: `${ClassPage.Messages.CmLinkTipPrefix} ${className}`,
  594. attributes: {
  595. [ClassPage.Attributes.DataClassName]: className,
  596. [ClassPage.Attributes.OnClick]: 'window.page.onClassLinkClick(this);'
  597. }
  598. });
  599. }
  600. }
  601. }
  602. markZ8Locales(lineHandle) {
  603. const editor = this.codeMirrorEditor;
  604. const text = lineHandle.text;
  605. const regexp = /Z8\.\$\('([\S]+)'(?:\s*,\s*.*)?\)/g;
  606. let match;
  607. while ((match = regexp.exec(text)) !== null) {
  608. const messageId = match[1];
  609. const from = { line: lineHandle.lineNo(), ch: match.index };
  610. const to = { line: lineHandle.lineNo(), ch: match.index + match[0].length };
  611. editor.markText(from, to, {
  612. className: ClassPage.StyleClasses.Z8Locale,
  613. title: `RU: ${Z8Locales['ru'][messageId]}\nEN: ${Z8Locales['en'][messageId]}`
  614. });
  615. }
  616. }
  617. markNew(lineHandle) {
  618. const editor = this.codeMirrorEditor;
  619. const text = lineHandle.text;
  620. const regexp = /new\s+([\w\.]+)/g;
  621. let match;
  622. while ((match = regexp.exec(text)) !== null) {
  623. const className = match[1];
  624. const from = { line: lineHandle.lineNo(), ch: match.index + 4 };
  625. const to = { line: lineHandle.lineNo(), ch: match.index + match[0].length };
  626. if(!ClassList[className] && !this.shortNameExists(className))
  627. continue;
  628. editor.markText(from, to, {
  629. className: ClassPage.StyleClasses.CmLink,
  630. title: `${ClassPage.Messages.CmLinkTipPrefix} ${className}`,
  631. attributes: {
  632. [ClassPage.Attributes.DataClassName]: className,
  633. [ClassPage.Attributes.OnClick]: 'window.page.onClassLinkClick(this);'
  634. }
  635. });
  636. }
  637. }
  638. markThis(lineHandle) {
  639. const editor = this.codeMirrorEditor;
  640. const text = lineHandle.text;
  641. const regexp = /this\.([\w]+)/g;
  642. let match;
  643. while ((match = regexp.exec(text)) !== null) {
  644. const propertyName = match[1];
  645. const from = { line: lineHandle.lineNo(), ch: match.index + 5 };
  646. const to = { line: lineHandle.lineNo(), ch: match.index + match[0].length };
  647. const foundProperty = this.findClassProperty(propertyName);
  648. if(!foundProperty)
  649. continue;
  650. editor.markText(from, to, {
  651. className: 'cm-this-prop',
  652. title: `Ctrl+Click to go to ${foundProperty.type === 'method' ? 'method' : 'property'} '${propertyName}'`,
  653. attributes: {
  654. [ClassPage.Attributes.DataPropertyName]: propertyName,
  655. [ClassPage.Attributes.DataPropertyType]: foundProperty.type === 'method' ? 'Methods' : 'Properties',
  656. [ClassPage.Attributes.DataPropertyParent]: foundProperty.inherited ? foundProperty.nearestParent : '',
  657. [ClassPage.Attributes.OnClick]: 'window.page.onPropertyClick(this);'
  658. }
  659. });
  660. }
  661. }
  662. markProperties(lineHandle) {
  663. const editor = this.codeMirrorEditor;
  664. const text = lineHandle.text;
  665. const regexp = /\t([\w]+):/g;
  666. let match;
  667. while ((match = regexp.exec(text)) !== null) {
  668. const propertyName = match[1];
  669. const from = { line: lineHandle.lineNo(), ch: match.index + 1 };
  670. const to = { line: lineHandle.lineNo(), ch: match.index + match[0].length - 1 };
  671. const foundProperty = this.findClassProperty(propertyName);
  672. if(!foundProperty)
  673. continue;
  674. editor.markText(from, to, {
  675. className: 'cm-this-prop',
  676. title: `Ctrl+Click to go to ${foundProperty.type === 'method' ? 'method' : 'property'} '${propertyName}'`,
  677. attributes: {
  678. [ClassPage.Attributes.DataPropertyName]: propertyName,
  679. [ClassPage.Attributes.DataPropertyType]: foundProperty.type === 'method' ? 'Methods' : 'Properties',
  680. [ClassPage.Attributes.DataPropertyParent]: foundProperty.inherited ? foundProperty.nearestParent : '',
  681. [ClassPage.Attributes.OnClick]: 'window.page.onPropertyClick(this);'
  682. }
  683. });
  684. }
  685. }
  686. shortNameExists(shortName) {
  687. return Object.keys(ClassList).map((key) => ClassList[key]).filter((item) => item.shortName === shortName).length > 0;
  688. }
  689. findClassProperty(propertyName) {
  690. const dynamicProperties = Class[ClassPage.ClassProperties.DynamicProperties];
  691. const properties = Class[ClassPage.ClassProperties.Properties];
  692. const statics = Class[ClassPage.ClassProperties.Statics];
  693. const foundStatic = statics.filter((prop) => prop.key === propertyName)[0];
  694. const foundDynamic = dynamicProperties.filter((prop) => prop.key === propertyName)[0];
  695. const foundProperty = properties.filter((prop) => prop.key === propertyName)[0];
  696. return foundStatic || foundDynamic || foundProperty;
  697. }
  698. onClassLinkClick(fragment) {
  699. const codeMirrorEditorElement = this.codeMirrorEditorElement;
  700. if(codeMirrorEditorElement.hasClass(ClassPage.StyleClasses.CtrlPressed))
  701. Url.goTo(`/class/${fragment.getAttribute(ClassPage.Attributes.DataClassName)}`, codeMirrorEditorElement.hasClass(ClassPage.StyleClasses.ShiftPressed));
  702. }
  703. onPropertyClick(fragment) {
  704. const codeMirrorEditorElement = this.codeMirrorEditorElement;
  705. if(codeMirrorEditorElement.hasClass(ClassPage.StyleClasses.CtrlPressed)) {
  706. const parentClassName = fragment.getAttribute(ClassPage.Attributes.DataPropertyParent);
  707. const propertyType = fragment.getAttribute(ClassPage.Attributes.DataPropertyType);
  708. const propertyName = fragment.getAttribute(ClassPage.Attributes.DataPropertyName);
  709. if(parentClassName.length > 0)
  710. Url.goTo(`/class/${parentClassName}#${propertyType}:${propertyName}`);
  711. else
  712. Url.setHash(`${propertyType}:${propertyName}`).updateLocation();
  713. }
  714. }
  715. onTabClick(e) {
  716. const element = CDElement.get(e.target);
  717. if(element.hasClass(ClassPage.StyleClasses.Selected))
  718. return;
  719. this.openTab(element.getAttribute(ClassPage.Attributes.DataTab));
  720. }
  721. openTab(tabName) {
  722. this.selectTab(tabName);
  723. this.activateContent(tabName);
  724. Url.setHash(tabName).updateLocation();
  725. }
  726. onClassClick(e) {
  727. let element = CDElement.get(e.target);
  728. while(!element.hasClass(ClassPage.StyleClasses.ClassItem))
  729. element = element.getParent();
  730. Url.goTo(`/class/${element.getAttribute(ClassPage.Attributes.DataClassName)}`);
  731. }
  732. onModeButtonClick(e) {
  733. const button = CDElement.get(e.target);
  734. const mode = button.getAttribute(ClassPage.Attributes.DataDisplayMode);
  735. this.switchMode(mode);
  736. button.addClass(ClassPage.StyleClasses.Selected);
  737. (mode === ClassPage.Mode.Tabs ? this.listModeButton : this.tabsModeButton).removeClass(ClassPage.StyleClasses.Selected);
  738. }
  739. searchInEditor(...queries) {
  740. const editor = this.codeMirrorEditor;
  741. for(const query of queries) {
  742. const cursor = editor.getSearchCursor(query, CodeMirror.Pos(editor.cmFirstLine(), 0), { caseFold: false, multiline: true });
  743. if(cursor.find(false)) {
  744. this.switchFullSource(true);
  745. this.openTab('Editor');
  746. editor.setSelection(cursor.from(), cursor.to());
  747. editor.scrollIntoView({from: cursor.from(), to: cursor.to()}, 100);
  748. return;
  749. }
  750. }
  751. }
  752. searchPropertyInEditor(isMethod, isDynamic, propertyName) {
  753. if(isMethod) {
  754. this.searchInEditor(`${propertyName}: function`);
  755. } else {
  756. if(isDynamic)
  757. this.searchInEditor(`this.${propertyName} =`, `this.${propertyName}`);
  758. else
  759. this.searchInEditor(`${propertyName}: `);
  760. }
  761. }
  762. scrollToProperty(hashTab, hashProp) {
  763. if(!hashProp)
  764. return;
  765. const item = this.contentElements[hashTab].getFirstChild(`.property-item[data-property-name="${hashProp}"]`);
  766. if(!item)
  767. return;
  768. const categoryList = item.getParent();
  769. const categoryHeader = categoryList.previousSibling();
  770. if(categoryList.hasClass(ClassPage.StyleClasses.Hidden))
  771. categoryList.removeClass(ClassPage.StyleClasses.Hidden);
  772. if(categoryHeader.hasClass(ClassPage.StyleClasses.Collapsed))
  773. categoryHeader.removeClass(ClassPage.StyleClasses.Collapsed);
  774. item.addClass(ClassPage.StyleClasses.Highlighted);
  775. item.addClass(ClassPage.StyleClasses.White);
  776. if(this.mode === ClassPage.Mode.Tabs)
  777. this.contentElements[hashTab].scrollTo(`.property-item[data-property-name="${hashProp}"]`);
  778. else
  779. item.scrollIntoView();
  780. setTimeout(() => {
  781. item.removeClass(ClassPage.StyleClasses.White);
  782. }, 1000);
  783. setTimeout(() => {
  784. item.removeClass(ClassPage.StyleClasses.Highlighted);
  785. }, 2000);
  786. }
  787. delayedAdjustCommentInputHeight(e) {
  788. setTimeout(() => { this.adjustCommentInputHeight(e) }, 0);
  789. }
  790. adjustCommentInputHeight(e) {
  791. const textArea = e.target;
  792. if(textArea.scrollHeight < 422 || e.key === DOM.Keys.Backspace) {
  793. textArea.style.height = 'auto';
  794. textArea.style.height = `${Math.min(422, textArea.scrollHeight + 2)}px`;
  795. }
  796. textArea.scrollTop = textArea.scrollHeight;
  797. }
  798. applyHash() {
  799. const hash = (Url.getHash() || '').split(':');
  800. const hashTab = hash[0];
  801. const hashProp = hash[1];
  802. const tabElements = this.tabElements;
  803. const contentElements = this.contentElements;
  804. const selectedTab = tabElements[hashTab] || tabElements[ClassPage.TabNames.Editor];
  805. const activeContent = contentElements[hashTab] || contentElements[ClassPage.TabNames.Editor];
  806. this.selectTab(selectedTab);
  807. this.activateContent(activeContent);
  808. this.scrollToProperty(hashTab, hashProp);
  809. }
  810. openSocket() {
  811. this.socket = new Socket('/ws').onMessage(this.onSocketMessage.bind(this));
  812. }
  813. onSocketMessage(e) {
  814. const changes = JSON.parse(e.data) || [];
  815. changes.forEach((changedComment) => {
  816. this.processChange(changedComment);
  817. });
  818. }
  819. processChange(changedComment) {
  820. if(changedComment.root !== Class[ClassPage.ClassProperties.Root] || changedComment.className !== Class[ClassPage.ClassProperties.Name])
  821. return;
  822. const propertyItem = this.propertyItemElements[changedComment.propertyName];
  823. propertyItem.removeClass('saving');
  824. switch(changedComment.action) {
  825. case 'create':
  826. this.documented++;
  827. this.renderDocumentedPercentage();
  828. Comments[changedComment.propertyName] = changedComment;
  829. propertyItem.getFirstChild('.property-item-comment').append(this.createCommentDateElement(changedComment.timestamp));
  830. break;
  831. case 'update':
  832. if(Comments[changedComment.propertyName].text.length === 0) {
  833. this.documented++;
  834. this.renderDocumentedPercentage();
  835. }
  836. Comments[changedComment.propertyName].text = changedComment.text;
  837. const dateElement = propertyItem.getFirstChild('.property-item-comment-date > .property-item-comment-date-date');
  838. if(dateElement) {
  839. dateElement.setInnerHTML(CDUtils.dateFormatUTC(changedComment.timestamp, 3, 'D.M.Y, H:I:S'));
  840. } else {
  841. propertyItem.getFirstChild('.property-item-comment').append(this.createCommentDateElement(changedComment.timestamp));
  842. }
  843. break;
  844. case 'remove':
  845. this.documented--;
  846. if(Comments[changedComment.propertyName])
  847. Comments[changedComment.propertyName].text = '';
  848. this.renderDocumentedPercentage();
  849. propertyItem.getFirstChild('.property-item-comment-date').remove();
  850. break;
  851. }
  852. const commentContent = changedComment.text;
  853. const itemCommentStatic = propertyItem.getFirstChild('.property-item-comment-static');
  854. const itemCommentInput = propertyItem.getFirstChild('.property-item-comment-input');
  855. const itemCommentOkButton = propertyItem.getFirstChild('.property-item-comment-button');
  856. if(itemCommentInput) {
  857. itemCommentInput.setValue(commentContent);
  858. itemCommentInput.addClass(ClassPage.StyleClasses.Hidden);
  859. itemCommentOkButton.addClass(ClassPage.StyleClasses.Hidden);
  860. }
  861. itemCommentStatic.setInnerHTML(commentContent.length > 0 ? CDUtils.nl2br(commentContent) : 'Not commented yet...');
  862. itemCommentStatic.switchClass(ClassPage.StyleClasses.Empty, commentContent.length === 0);
  863. itemCommentStatic.removeClass(ClassPage.StyleClasses.Hidden);
  864. }
  865. /* >>> Context menu | TODO: move to a completely independent module? */
  866. showContextMenu(contextMenuType, target, pos) {
  867. while(!target.hasClass(contextMenuType))
  868. target = target.getParent();
  869. switch(contextMenuType) {
  870. case ClassPage.ContextMenuType.PropertyItem:
  871. const propertyItemName = target.getAttribute(ClassPage.Attributes.DataPropertyName);
  872. const propertyItemType = target.getAttribute(ClassPage.Attributes.DataPropertyType);
  873. const propertyItemParent = target.getAttribute(ClassPage.Attributes.DataPropertyParent);
  874. const propertyItemDynamic = target.getAttribute(ClassPage.Attributes.DataPropertyDynamic);
  875. const propertyItemInhertied = target.getAttribute(ClassPage.Attributes.DataPropertyInherited);
  876. if(propertyItemInhertied !== 'true') {
  877. this.createContextMenuItem('ShowInEditor', 'Show in Editor', () => {
  878. this.searchPropertyInEditor(propertyItemType === 'method', propertyItemDynamic === 'true', propertyItemName);
  879. });
  880. }
  881. if(propertyItemParent != null) {
  882. this.createContextMenuItem('MoveToParent', 'Move to parent', () => {
  883. Url.goTo(`/class/${propertyItemParent}#${propertyItemType === 'method' ? 'Methods' : 'Properties'}:${propertyItemName}`);
  884. });
  885. }
  886. if(isEditor && !target.getFirstChild('.property-item-comment-static').hasClass(ClassPage.StyleClasses.Hidden)) {
  887. this.createContextMenuDelimiter();
  888. this.createContextMenuItem('EditComment', 'Edit comment', () => {
  889. target.getFirstChild('.property-item-comment-static').click();
  890. target.getFirstChild('.property-item-comment-input').focus();
  891. });
  892. }
  893. this.createContextMenuDelimiter();
  894. this.createContextMenuItem('CopyLink', 'Copy link', () => {
  895. DOM.copyToClipboard(`${Url.getFullPath()}#${propertyItemType === 'method' ? 'Methods' : 'Properties'}:${propertyItemName}`);
  896. });
  897. break;
  898. }
  899. this.contextMenu.style('left', `${pos.x}px`).style('top', `${pos.y}px`);
  900. this.contextMenu.removeClass(ClassPage.StyleClasses.Hidden);
  901. }
  902. createContextMenuItem(name, text, action) {
  903. const itemAction = (e) => {
  904. action(e);
  905. this.hideContextMenu();
  906. };
  907. const item = DOM.create({ tag: DOM.Tags.Div, cls: 'context-menu-item', innerHTML: text, attr: { 'data-context-menu-item-name': name } }, this.contextMenu)
  908. .on(DOM.Events.Click, itemAction);
  909. this.contextMenuItems[name] = { item: item, action: itemAction };
  910. }
  911. createContextMenuDelimiter() {
  912. DOM.create({ tag: DOM.Tags.Div, cls: 'context-menu-delimiter' }, this.contextMenu);
  913. }
  914. clearContextMenu() {
  915. for(const item of this.contextMenu.getChildren()) {
  916. const name = item.getAttribute('data-context-menu-item-name');
  917. if(name) {
  918. const action = this.contextMenuItems[name].action;
  919. item.un(DOM.Events.Click, action);
  920. this.contextMenuItems[name] = null;
  921. delete this.contextMenuItems[name];
  922. }
  923. item.remove();
  924. }
  925. }
  926. hideContextMenu() {
  927. this.contextMenu.addClass(ClassPage.StyleClasses.Hidden);
  928. this.clearContextMenu();
  929. }
  930. /* <<< Context menu */
  931. };
  932. window_.on(DOM.Events.Load, (e) => {
  933. window.page = new ClassPage().start();
  934. });
  935. window_.on(DOM.Events.KeyDown, (e) => {
  936. if(window.page && e.key === DOM.Keys.Control)
  937. window.page.codeMirrorEditorElement.addClass(ClassPage.StyleClasses.CtrlPressed);
  938. if(window.page && e.key === DOM.Keys.Shift)
  939. window.page.codeMirrorEditorElement.addClass(ClassPage.StyleClasses.ShiftPressed);
  940. });
  941. window_.on(DOM.Events.KeyUp, (e) => {
  942. if(window.page && e.key === DOM.Keys.Control)
  943. window.page.codeMirrorEditorElement.removeClass(ClassPage.StyleClasses.CtrlPressed);
  944. if(window.page && e.key === DOM.Keys.Shift)
  945. window.page.codeMirrorEditorElement.removeClass(ClassPage.StyleClasses.ShiftPressed);
  946. });
  947. window_.on(DOM.Events.HashChange, (e) => {
  948. if(window.page)
  949. window.page.applyHash();
  950. });
  951. window_.on(DOM.Events.MouseDown, (e) => {
  952. if(window.page) {
  953. let target = CDElement.get(e.target);
  954. while(target != null && !target.hasClass('context-menu')) {
  955. target = target.getParent();
  956. }
  957. if(target != null && target.hasClass('context-menu'))
  958. return;
  959. if(!window.page.contextMenu.hasClass(ClassPage.StyleClasses.Hidden))
  960. window.page.hideContextMenu();
  961. }
  962. });