script.js 44 KB

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