annotations.src.js 217 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028
  1. /**
  2. * @license Highcharts JS v8.1.2 (2020-06-16)
  3. *
  4. * Annotations module
  5. *
  6. * (c) 2009-2019 Torstein Honsi
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/modules/annotations', ['highcharts'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'annotations/eventEmitterMixin.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
  32. /* *
  33. *
  34. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  35. *
  36. * */
  37. var addEvent = U.addEvent, fireEvent = U.fireEvent, inArray = U.inArray, objectEach = U.objectEach, pick = U.pick, removeEvent = U.removeEvent;
  38. /* eslint-disable valid-jsdoc */
  39. /**
  40. * It provides methods for:
  41. * - adding and handling DOM events and a drag event,
  42. * - mapping a mouse move event to the distance between two following events.
  43. * The units of the distance are specific to a transformation,
  44. * e.g. for rotation they are radians, for scaling they are scale factors.
  45. *
  46. * @private
  47. * @mixin
  48. * @memberOf Annotation
  49. */
  50. var eventEmitterMixin = {
  51. /**
  52. * Add emitter events.
  53. */
  54. addEvents: function () {
  55. var emitter = this, addMouseDownEvent = function (element) {
  56. addEvent(element, H.isTouchDevice ? 'touchstart' : 'mousedown', function (e) {
  57. emitter.onMouseDown(e);
  58. });
  59. };
  60. addMouseDownEvent(this.graphic.element);
  61. (emitter.labels || []).forEach(function (label) {
  62. if (label.options.useHTML && label.graphic.text) {
  63. // Mousedown event bound to HTML element (#13070).
  64. addMouseDownEvent(label.graphic.text.element);
  65. }
  66. });
  67. objectEach(emitter.options.events, function (event, type) {
  68. var eventHandler = function (e) {
  69. if (type !== 'click' || !emitter.cancelClick) {
  70. event.call(emitter, emitter.chart.pointer.normalize(e), emitter.target);
  71. }
  72. };
  73. if (inArray(type, emitter.nonDOMEvents || []) === -1) {
  74. emitter.graphic.on(type, eventHandler);
  75. }
  76. else {
  77. addEvent(emitter, type, eventHandler);
  78. }
  79. });
  80. if (emitter.options.draggable) {
  81. addEvent(emitter, H.isTouchDevice ? 'touchmove' : 'drag', emitter.onDrag);
  82. if (!emitter.graphic.renderer.styledMode) {
  83. var cssPointer_1 = {
  84. cursor: {
  85. x: 'ew-resize',
  86. y: 'ns-resize',
  87. xy: 'move'
  88. }[emitter.options.draggable]
  89. };
  90. emitter.graphic.css(cssPointer_1);
  91. (emitter.labels || []).forEach(function (label) {
  92. if (label.options.useHTML && label.graphic.text) {
  93. label.graphic.text.css(cssPointer_1);
  94. }
  95. });
  96. }
  97. }
  98. if (!emitter.isUpdating) {
  99. fireEvent(emitter, 'add');
  100. }
  101. },
  102. /**
  103. * Remove emitter document events.
  104. */
  105. removeDocEvents: function () {
  106. if (this.removeDrag) {
  107. this.removeDrag = this.removeDrag();
  108. }
  109. if (this.removeMouseUp) {
  110. this.removeMouseUp = this.removeMouseUp();
  111. }
  112. },
  113. /**
  114. * Mouse down handler.
  115. */
  116. onMouseDown: function (e) {
  117. var emitter = this, pointer = emitter.chart.pointer, prevChartX, prevChartY;
  118. if (e.preventDefault) {
  119. e.preventDefault();
  120. }
  121. // On right click, do nothing:
  122. if (e.button === 2) {
  123. return;
  124. }
  125. e = pointer.normalize(e);
  126. prevChartX = e.chartX;
  127. prevChartY = e.chartY;
  128. emitter.cancelClick = false;
  129. emitter.chart.hasDraggedAnnotation = true;
  130. emitter.removeDrag = addEvent(H.doc, H.isTouchDevice ? 'touchmove' : 'mousemove', function (e) {
  131. emitter.hasDragged = true;
  132. e = pointer.normalize(e);
  133. e.prevChartX = prevChartX;
  134. e.prevChartY = prevChartY;
  135. fireEvent(emitter, 'drag', e);
  136. prevChartX = e.chartX;
  137. prevChartY = e.chartY;
  138. });
  139. emitter.removeMouseUp = addEvent(H.doc, H.isTouchDevice ? 'touchend' : 'mouseup', function (e) {
  140. emitter.cancelClick = emitter.hasDragged;
  141. emitter.hasDragged = false;
  142. emitter.chart.hasDraggedAnnotation = false;
  143. // ControlPoints vs Annotation:
  144. fireEvent(pick(emitter.target, emitter), 'afterUpdate');
  145. emitter.onMouseUp(e);
  146. });
  147. },
  148. /**
  149. * Mouse up handler.
  150. */
  151. onMouseUp: function (_e) {
  152. var chart = this.chart, annotation = this.target || this, annotationsOptions = chart.options.annotations, index = chart.annotations.indexOf(annotation);
  153. this.removeDocEvents();
  154. annotationsOptions[index] = annotation.options;
  155. },
  156. /**
  157. * Drag and drop event. All basic annotations should share this
  158. * capability as well as the extended ones.
  159. */
  160. onDrag: function (e) {
  161. if (this.chart.isInsidePlot(e.chartX - this.chart.plotLeft, e.chartY - this.chart.plotTop)) {
  162. var translation = this.mouseMoveToTranslation(e);
  163. if (this.options.draggable === 'x') {
  164. translation.y = 0;
  165. }
  166. if (this.options.draggable === 'y') {
  167. translation.x = 0;
  168. }
  169. if (this.points.length) {
  170. this.translate(translation.x, translation.y);
  171. }
  172. else {
  173. this.shapes.forEach(function (shape) {
  174. shape.translate(translation.x, translation.y);
  175. });
  176. this.labels.forEach(function (label) {
  177. label.translate(translation.x, translation.y);
  178. });
  179. }
  180. this.redraw(false);
  181. }
  182. },
  183. /**
  184. * Map mouse move event to the radians.
  185. */
  186. mouseMoveToRadians: function (e, cx, cy) {
  187. var prevDy = e.prevChartY - cy, prevDx = e.prevChartX - cx, dy = e.chartY - cy, dx = e.chartX - cx, temp;
  188. if (this.chart.inverted) {
  189. temp = prevDx;
  190. prevDx = prevDy;
  191. prevDy = temp;
  192. temp = dx;
  193. dx = dy;
  194. dy = temp;
  195. }
  196. return Math.atan2(dy, dx) - Math.atan2(prevDy, prevDx);
  197. },
  198. /**
  199. * Map mouse move event to the distance between two following events.
  200. */
  201. mouseMoveToTranslation: function (e) {
  202. var dx = e.chartX - e.prevChartX, dy = e.chartY - e.prevChartY, temp;
  203. if (this.chart.inverted) {
  204. temp = dy;
  205. dy = dx;
  206. dx = temp;
  207. }
  208. return {
  209. x: dx,
  210. y: dy
  211. };
  212. },
  213. /**
  214. * Map mouse move to the scale factors.
  215. *
  216. * @param {Object} e event
  217. * @param {number} cx center x
  218. * @param {number} cy center y
  219. **/
  220. mouseMoveToScale: function (e, cx, cy) {
  221. var prevDx = e.prevChartX - cx, prevDy = e.prevChartY - cy, dx = e.chartX - cx, dy = e.chartY - cy, sx = (dx || 1) / (prevDx || 1), sy = (dy || 1) / (prevDy || 1), temp;
  222. if (this.chart.inverted) {
  223. temp = sy;
  224. sy = sx;
  225. sx = temp;
  226. }
  227. return {
  228. x: sx,
  229. y: sy
  230. };
  231. },
  232. /**
  233. * Destroy the event emitter.
  234. */
  235. destroy: function () {
  236. this.removeDocEvents();
  237. removeEvent(this);
  238. this.hcEvents = null;
  239. }
  240. };
  241. return eventEmitterMixin;
  242. });
  243. _registerModule(_modules, 'annotations/ControlPoint.js', [_modules['parts/Utilities.js'], _modules['annotations/eventEmitterMixin.js']], function (U, eventEmitterMixin) {
  244. /* *
  245. *
  246. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  247. *
  248. * */
  249. /**
  250. * Callback to modify annotation's possitioner controls.
  251. *
  252. * @callback Highcharts.AnnotationControlPointPositionerFunction
  253. * @param {Highcharts.AnnotationControlPoint} this
  254. * @param {Highcharts.AnnotationControllable} target
  255. * @return {Highcharts.PositionObject}
  256. */
  257. var extend = U.extend, merge = U.merge, pick = U.pick;
  258. /* eslint-disable no-invalid-this, valid-jsdoc */
  259. /**
  260. * A control point class which is a connection between controllable
  261. * transform methods and a user actions.
  262. *
  263. * @requires modules/annotations
  264. *
  265. * @class
  266. * @name Highcharts.AnnotationControlPoint
  267. *
  268. * @hideconstructor
  269. *
  270. * @param {Highcharts.Chart} chart
  271. * A chart instance.
  272. *
  273. * @param {Highcharts.AnnotationControllable} target
  274. * A controllable instance which is a target for a control point.
  275. *
  276. * @param {Highcharts.AnnotationControlPointOptionsObject} options
  277. * An options object.
  278. *
  279. * @param {number} [index]
  280. * Point index.
  281. */
  282. var ControlPoint = /** @class */ (function () {
  283. function ControlPoint(chart, target, options, index) {
  284. /**
  285. *
  286. * Properties
  287. *
  288. */
  289. this.addEvents = eventEmitterMixin.addEvents;
  290. this.graphic = void 0;
  291. this.mouseMoveToRadians = eventEmitterMixin.mouseMoveToRadians;
  292. this.mouseMoveToScale = eventEmitterMixin.mouseMoveToScale;
  293. this.mouseMoveToTranslation = eventEmitterMixin.mouseMoveToTranslation;
  294. this.onDrag = eventEmitterMixin.onDrag;
  295. this.onMouseDown = eventEmitterMixin.onMouseDown;
  296. this.onMouseUp = eventEmitterMixin.onMouseUp;
  297. this.removeDocEvents = eventEmitterMixin.removeDocEvents;
  298. /**
  299. *
  300. * Functions
  301. *
  302. */
  303. /**
  304. * List of events for `anntation.options.events` that should not be
  305. * added to `annotation.graphic` but to the `annotation`.
  306. * @private
  307. * @name Highcharts.AnnotationControlPoint#nonDOMEvents
  308. * @type {Array<string>}
  309. */
  310. this.nonDOMEvents = ['drag'];
  311. this.chart = chart;
  312. this.target = target;
  313. this.options = options;
  314. this.index = pick(options.index, index);
  315. }
  316. /**
  317. * Set the visibility of the control point.
  318. *
  319. * @function Highcharts.AnnotationControlPoint#setVisibility
  320. *
  321. * @param {boolean} visible
  322. * Visibility of the control point.
  323. *
  324. * @return {void}
  325. */
  326. ControlPoint.prototype.setVisibility = function (visible) {
  327. this.graphic.attr('visibility', visible ? 'visible' : 'hidden');
  328. this.options.visible = visible;
  329. };
  330. /**
  331. * Render the control point.
  332. * @private
  333. */
  334. ControlPoint.prototype.render = function () {
  335. var chart = this.chart, options = this.options;
  336. this.graphic = chart.renderer
  337. .symbol(options.symbol, 0, 0, options.width, options.height)
  338. .add(chart.controlPointsGroup)
  339. .css(options.style);
  340. this.setVisibility(options.visible);
  341. // npm test -- --tests "highcharts/annotations-advanced/*"
  342. this.addEvents();
  343. };
  344. /**
  345. * Redraw the control point.
  346. * @private
  347. * @param {boolean} [animation]
  348. */
  349. ControlPoint.prototype.redraw = function (animation) {
  350. this.graphic[animation ? 'animate' : 'attr'](this.options.positioner.call(this, this.target));
  351. };
  352. /**
  353. * Destroy the control point.
  354. * @private
  355. */
  356. ControlPoint.prototype.destroy = function () {
  357. eventEmitterMixin.destroy.call(this);
  358. if (this.graphic) {
  359. this.graphic = this.graphic.destroy();
  360. }
  361. this.chart = null;
  362. this.target = null;
  363. this.options = null;
  364. };
  365. /**
  366. * Update the control point.
  367. *
  368. * @function Highcharts.AnnotationControlPoint#update
  369. *
  370. * @param {Partial<Highcharts.AnnotationControlPointOptionsObject>} userOptions
  371. * New options for the control point.
  372. *
  373. * @return {void}
  374. */
  375. ControlPoint.prototype.update = function (userOptions) {
  376. var chart = this.chart, target = this.target, index = this.index, options = merge(true, this.options, userOptions);
  377. this.destroy();
  378. this.constructor(chart, target, options, index);
  379. this.render(chart.controlPointsGroup);
  380. this.redraw();
  381. };
  382. return ControlPoint;
  383. }());
  384. return ControlPoint;
  385. });
  386. _registerModule(_modules, 'annotations/MockPoint.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
  387. /* *
  388. *
  389. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  390. *
  391. * */
  392. /**
  393. * @private
  394. * @interface Highcharts.AnnotationMockLabelOptionsObject
  395. */ /**
  396. * Point instance of the point.
  397. * @name Highcharts.AnnotationMockLabelOptionsObject#point
  398. * @type {Highcharts.AnnotationMockPoint}
  399. */ /**
  400. * X value translated to x axis scale.
  401. * @name Highcharts.AnnotationMockLabelOptionsObject#x
  402. * @type {number|null}
  403. */ /**
  404. * Y value translated to y axis scale.
  405. * @name Highcharts.AnnotationMockLabelOptionsObject#y
  406. * @type {number|null}
  407. */
  408. /**
  409. * A mock series instance imitating a real series from a real point.
  410. * @private
  411. * @interface Highcharts.AnnotationMockSeries
  412. */ /**
  413. * Whether a series is visible.
  414. * @name Highcharts.AnnotationMockSeries#visible
  415. * @type {boolean}
  416. */ /**
  417. * A chart instance.
  418. * @name Highcharts.AnnotationMockSeries#chart
  419. * @type {Highcharts.Chart}
  420. */ /**
  421. * @name Highcharts.AnnotationMockSeries#getPlotBox
  422. * @type {Function}
  423. */
  424. /**
  425. * Indicates if this is a mock point for an annotation.
  426. * @name Highcharts.Point#mock
  427. * @type {boolean|undefined}
  428. */
  429. var defined = U.defined, extend = U.extend, fireEvent = U.fireEvent;
  430. /* eslint-disable no-invalid-this, valid-jsdoc */
  431. /**
  432. * A trimmed point object which imitates {@link Highchart.Point} class. It is
  433. * created when there is a need of pointing to some chart's position using axis
  434. * values or pixel values
  435. *
  436. * @requires modules/annotations
  437. *
  438. * @private
  439. * @class
  440. * @name Highcharts.AnnotationMockPoint
  441. *
  442. * @hideconstructor
  443. *
  444. * @param {Highcharts.Chart} chart
  445. * The chart instance.
  446. *
  447. * @param {Highcharts.AnnotationControllable|null} target
  448. * The related controllable.
  449. *
  450. * @param {Highcharts.AnnotationMockPointOptionsObject|Function} options
  451. * The options object.
  452. */
  453. var MockPoint = /** @class */ (function () {
  454. function MockPoint(chart, target, options) {
  455. this.isInside = void 0;
  456. this.plotX = void 0;
  457. this.plotY = void 0;
  458. this.x = void 0;
  459. this.y = void 0;
  460. /* *
  461. *
  462. * Functions
  463. *
  464. * */
  465. /**
  466. * A flag indicating that a point is not the real one.
  467. *
  468. * @type {boolean}
  469. * @default true
  470. */
  471. this.mock = true;
  472. /**
  473. * A mock series instance imitating a real series from a real point.
  474. *
  475. * @name Annotation.AnnotationMockPoint#series
  476. * @type {Highcharts.AnnotationMockSeries}
  477. */
  478. this.series = {
  479. visible: true,
  480. chart: chart,
  481. getPlotBox: H.Series.prototype.getPlotBox
  482. };
  483. /**
  484. * @name Annotation.AnnotationMockPoint#target
  485. * @type {Highcharts.AnnotationControllable|null}
  486. */
  487. this.target = target || null;
  488. /**
  489. * Options for the mock point.
  490. *
  491. * @name Annotation.AnnotationMockPoint#options
  492. * @type {Highcharts.AnnotationsMockPointOptionsObject}
  493. */
  494. this.options = options;
  495. /**
  496. * If an xAxis is set it represents the point's value in terms of the
  497. * xAxis.
  498. *
  499. * @name Annotation.AnnotationMockPoint#x
  500. * @type {number|undefined}
  501. */
  502. /**
  503. * If an yAxis is set it represents the point's value in terms of the
  504. * yAxis.
  505. *
  506. * @name Annotation.AnnotationMockPoint#y
  507. * @type {number|undefined}
  508. */
  509. /**
  510. * It represents the point's pixel x coordinate relative to its plot
  511. * box.
  512. *
  513. * @name Annotation.AnnotationMockPoint#plotX
  514. * @type {number|undefined}
  515. */
  516. /**
  517. * It represents the point's pixel y position relative to its plot box.
  518. *
  519. * @name Annotation.AnnotationMockPoint#plotY
  520. * @type {number|undefined}
  521. */
  522. /**
  523. * Whether the point is inside the plot box.
  524. *
  525. * @name Annotation.AnnotationMockPoint#isInside
  526. * @type {boolean|undefined}
  527. */
  528. this.applyOptions(this.getOptions());
  529. }
  530. /**
  531. * Create a mock point from a real Highcharts point.
  532. *
  533. * @private
  534. * @static
  535. *
  536. * @param {Highcharts.Point} point
  537. *
  538. * @return {Highcharts.AnnotationMockPoint}
  539. * A mock point instance.
  540. */
  541. MockPoint.fromPoint = function (point) {
  542. return new MockPoint(point.series.chart, null, {
  543. x: point.x,
  544. y: point.y,
  545. xAxis: point.series.xAxis,
  546. yAxis: point.series.yAxis
  547. });
  548. };
  549. /**
  550. * Get the pixel position from the point like object.
  551. *
  552. * @private
  553. * @static
  554. *
  555. * @param {Highcharts.AnnotationPointType} point
  556. *
  557. * @param {boolean} [paneCoordinates]
  558. * whether the pixel position should be relative
  559. *
  560. * @return {Highcharts.PositionObject} pixel position
  561. */
  562. MockPoint.pointToPixels = function (point, paneCoordinates) {
  563. var series = point.series, chart = series.chart, x = point.plotX, y = point.plotY, plotBox;
  564. if (chart.inverted) {
  565. if (point.mock) {
  566. x = point.plotY;
  567. y = point.plotX;
  568. }
  569. else {
  570. x = chart.plotWidth - point.plotY;
  571. y = chart.plotHeight - point.plotX;
  572. }
  573. }
  574. if (series && !paneCoordinates) {
  575. plotBox = series.getPlotBox();
  576. x += plotBox.translateX;
  577. y += plotBox.translateY;
  578. }
  579. return {
  580. x: x,
  581. y: y
  582. };
  583. };
  584. /**
  585. * Get fresh mock point options from the point like object.
  586. *
  587. * @private
  588. * @static
  589. *
  590. * @param {Highcharts.AnnotationPointType} point
  591. *
  592. * @return {Highcharts.AnnotationMockPointOptionsObject}
  593. * A mock point's options.
  594. */
  595. MockPoint.pointToOptions = function (point) {
  596. return {
  597. x: point.x,
  598. y: point.y,
  599. xAxis: point.series.xAxis,
  600. yAxis: point.series.yAxis
  601. };
  602. };
  603. /**
  604. * Check if the point has dynamic options.
  605. * @private
  606. * @return {boolean}
  607. * A positive flag if the point has dynamic options.
  608. */
  609. MockPoint.prototype.hasDynamicOptions = function () {
  610. return typeof this.options === 'function';
  611. };
  612. /**
  613. * Get the point's options.
  614. * @private
  615. * @return {Highcharts.AnnotationMockPointOptionsObject}
  616. * The mock point's options.
  617. */
  618. MockPoint.prototype.getOptions = function () {
  619. return this.hasDynamicOptions() ?
  620. this.options(this.target) :
  621. this.options;
  622. };
  623. /**
  624. * Apply options for the point.
  625. * @private
  626. * @param {Highcharts.AnnotationMockPointOptionsObject} options
  627. */
  628. MockPoint.prototype.applyOptions = function (options) {
  629. this.command = options.command;
  630. this.setAxis(options, 'x');
  631. this.setAxis(options, 'y');
  632. this.refresh();
  633. };
  634. /**
  635. * Set x or y axis.
  636. * @private
  637. * @param {Highcharts.AnnotationMockPointOptionsObject} options
  638. * @param {string} xOrY
  639. * 'x' or 'y' string literal
  640. */
  641. MockPoint.prototype.setAxis = function (options, xOrY) {
  642. var axisName = (xOrY + 'Axis'), axisOptions = options[axisName], chart = this.series.chart;
  643. this.series[axisName] =
  644. axisOptions instanceof H.Axis ?
  645. axisOptions :
  646. defined(axisOptions) ?
  647. (chart[axisName][axisOptions] ||
  648. chart.get(axisOptions)) :
  649. null;
  650. };
  651. /**
  652. * Transform the mock point to an anchor (relative position on the chart).
  653. * @private
  654. * @return {Array<number>}
  655. * A quadruple of numbers which denotes x, y, width and height of the box
  656. **/
  657. MockPoint.prototype.toAnchor = function () {
  658. var anchor = [this.plotX, this.plotY, 0, 0];
  659. if (this.series.chart.inverted) {
  660. anchor[0] = this.plotY;
  661. anchor[1] = this.plotX;
  662. }
  663. return anchor;
  664. };
  665. /**
  666. * Returns a label config object - the same as
  667. * Highcharts.Point.prototype.getLabelConfig
  668. * @private
  669. * @return {Highcharts.AnnotationMockLabelOptionsObject} the point's label config
  670. */
  671. MockPoint.prototype.getLabelConfig = function () {
  672. return {
  673. x: this.x,
  674. y: this.y,
  675. point: this
  676. };
  677. };
  678. /**
  679. * Check if the point is inside its pane.
  680. * @private
  681. * @return {boolean} A flag indicating whether the point is inside the pane.
  682. */
  683. MockPoint.prototype.isInsidePlot = function () {
  684. var plotX = this.plotX, plotY = this.plotY, xAxis = this.series.xAxis, yAxis = this.series.yAxis, e = {
  685. x: plotX,
  686. y: plotY,
  687. isInsidePlot: true
  688. };
  689. if (xAxis) {
  690. e.isInsidePlot = defined(plotX) && plotX >= 0 && plotX <= xAxis.len;
  691. }
  692. if (yAxis) {
  693. e.isInsidePlot =
  694. e.isInsidePlot &&
  695. defined(plotY) &&
  696. plotY >= 0 && plotY <= yAxis.len;
  697. }
  698. fireEvent(this.series.chart, 'afterIsInsidePlot', e);
  699. return e.isInsidePlot;
  700. };
  701. /**
  702. * Refresh point values and coordinates based on its options.
  703. * @private
  704. */
  705. MockPoint.prototype.refresh = function () {
  706. var series = this.series, xAxis = series.xAxis, yAxis = series.yAxis, options = this.getOptions();
  707. if (xAxis) {
  708. this.x = options.x;
  709. this.plotX = xAxis.toPixels(options.x, true);
  710. }
  711. else {
  712. this.x = null;
  713. this.plotX = options.x;
  714. }
  715. if (yAxis) {
  716. this.y = options.y;
  717. this.plotY = yAxis.toPixels(options.y, true);
  718. }
  719. else {
  720. this.y = null;
  721. this.plotY = options.y;
  722. }
  723. this.isInside = this.isInsidePlot();
  724. };
  725. /**
  726. * Translate the point.
  727. *
  728. * @private
  729. *
  730. * @param {number|undefined} cx
  731. * Origin x transformation.
  732. *
  733. * @param {number|undefined} cy
  734. * Origin y transformation.
  735. *
  736. * @param {number} dx
  737. * Translation for x coordinate.
  738. *
  739. * @param {number} dy
  740. * Translation for y coordinate.
  741. **/
  742. MockPoint.prototype.translate = function (_cx, _cy, dx, dy) {
  743. if (!this.hasDynamicOptions()) {
  744. this.plotX += dx;
  745. this.plotY += dy;
  746. this.refreshOptions();
  747. }
  748. };
  749. /**
  750. * Scale the point.
  751. *
  752. * @private
  753. *
  754. * @param {number} cx
  755. * Origin x transformation.
  756. *
  757. * @param {number} cy
  758. * Origin y transformation.
  759. *
  760. * @param {number} sx
  761. * Scale factor x.
  762. *
  763. * @param {number} sy
  764. * Scale factor y.
  765. */
  766. MockPoint.prototype.scale = function (cx, cy, sx, sy) {
  767. if (!this.hasDynamicOptions()) {
  768. var x = this.plotX * sx, y = this.plotY * sy, tx = (1 - sx) * cx, ty = (1 - sy) * cy;
  769. this.plotX = tx + x;
  770. this.plotY = ty + y;
  771. this.refreshOptions();
  772. }
  773. };
  774. /**
  775. * Rotate the point.
  776. * @private
  777. * @param {number} cx origin x rotation
  778. * @param {number} cy origin y rotation
  779. * @param {number} radians
  780. */
  781. MockPoint.prototype.rotate = function (cx, cy, radians) {
  782. if (!this.hasDynamicOptions()) {
  783. var cos = Math.cos(radians), sin = Math.sin(radians), x = this.plotX, y = this.plotY, tx, ty;
  784. x -= cx;
  785. y -= cy;
  786. tx = x * cos - y * sin;
  787. ty = x * sin + y * cos;
  788. this.plotX = tx + cx;
  789. this.plotY = ty + cy;
  790. this.refreshOptions();
  791. }
  792. };
  793. /**
  794. * Refresh point options based on its plot coordinates.
  795. * @private
  796. */
  797. MockPoint.prototype.refreshOptions = function () {
  798. var series = this.series, xAxis = series.xAxis, yAxis = series.yAxis;
  799. this.x = this.options.x = xAxis ?
  800. this.options.x = xAxis.toValue(this.plotX, true) :
  801. this.plotX;
  802. this.y = this.options.y = yAxis ?
  803. yAxis.toValue(this.plotY, true) :
  804. this.plotY;
  805. };
  806. return MockPoint;
  807. }());
  808. return MockPoint;
  809. });
  810. _registerModule(_modules, 'annotations/controllable/controllableMixin.js', [_modules['annotations/ControlPoint.js'], _modules['annotations/MockPoint.js'], _modules['parts/Tooltip.js'], _modules['parts/Utilities.js']], function (ControlPoint, MockPoint, Tooltip, U) {
  811. /* *
  812. *
  813. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  814. *
  815. * */
  816. var isObject = U.isObject, isString = U.isString, merge = U.merge, splat = U.splat;
  817. /**
  818. * An object which denots a controllable's anchor positions - relative and
  819. * absolute.
  820. *
  821. * @private
  822. * @interface Highcharts.AnnotationAnchorObject
  823. */ /**
  824. * Relative to the plot area position
  825. * @name Highcharts.AnnotationAnchorObject#relativePosition
  826. * @type {Highcharts.BBoxObject}
  827. */ /**
  828. * Absolute position
  829. * @name Highcharts.AnnotationAnchorObject#absolutePosition
  830. * @type {Highcharts.BBoxObject}
  831. */
  832. /**
  833. * @interface Highcharts.AnnotationControllable
  834. */ /**
  835. * @name Highcharts.AnnotationControllable#annotation
  836. * @type {Highcharts.Annotation}
  837. */ /**
  838. * @name Highcharts.AnnotationControllable#chart
  839. * @type {Highcharts.Chart}
  840. */ /**
  841. * @name Highcharts.AnnotationControllable#collection
  842. * @type {string}
  843. */ /**
  844. * @private
  845. * @name Highcharts.AnnotationControllable#controlPoints
  846. * @type {Array<Highcharts.AnnotationControlPoint>}
  847. */ /**
  848. * @name Highcharts.AnnotationControllable#points
  849. * @type {Array<Highcharts.Point>}
  850. */
  851. /* eslint-disable no-invalid-this, valid-jsdoc */
  852. /**
  853. * It provides methods for handling points, control points
  854. * and points transformations.
  855. *
  856. * @private
  857. * @mixin
  858. * @name Highcharts.AnnotationControllableMixin
  859. */
  860. var controllableMixin = {
  861. /**
  862. * Init the controllable
  863. */
  864. init: function (annotation, options, index) {
  865. this.annotation = annotation;
  866. this.chart = annotation.chart;
  867. this.options = options;
  868. this.points = [];
  869. this.controlPoints = [];
  870. this.index = index;
  871. this.linkPoints();
  872. this.addControlPoints();
  873. },
  874. /**
  875. * Redirect attr usage on the controllable graphic element.
  876. */
  877. attr: function () {
  878. this.graphic.attr.apply(this.graphic, arguments);
  879. },
  880. /**
  881. * Get the controllable's points options.
  882. *
  883. * @return {Array<Highcharts.PointOptionsObject>}
  884. * An array of points' options.
  885. */
  886. getPointsOptions: function () {
  887. var options = this.options;
  888. return (options.points || (options.point && splat(options.point)));
  889. },
  890. /**
  891. * Utility function for mapping item's options
  892. * to element's attribute
  893. *
  894. * @param {Highcharts.AnnotationsLabelsOptions|Highcharts.AnnotationsShapesOptions} options
  895. *
  896. * @return {Highcharts.SVGAttributes}
  897. * Mapped options.
  898. */
  899. attrsFromOptions: function (options) {
  900. var map = this.constructor.attrsMap, attrs = {}, key, mappedKey, styledMode = this.chart.styledMode;
  901. for (key in options) { // eslint-disable-line guard-for-in
  902. mappedKey = map[key];
  903. if (mappedKey &&
  904. (!styledMode ||
  905. ['fill', 'stroke', 'stroke-width']
  906. .indexOf(mappedKey) === -1)) {
  907. attrs[mappedKey] = options[key];
  908. }
  909. }
  910. return attrs;
  911. },
  912. /**
  913. * Returns object which denotes anchor position - relative and absolute.
  914. *
  915. * @param {Highcharts.AnnotationPointType} point
  916. * A point like object.
  917. *
  918. * @return {Highcharts.AnnotationAnchorObject} a controllable anchor
  919. */
  920. anchor: function (point) {
  921. var plotBox = point.series.getPlotBox(), box = point.mock ?
  922. point.toAnchor() :
  923. Tooltip.prototype.getAnchor.call({
  924. chart: point.series.chart
  925. }, point), anchor = {
  926. x: box[0] + (this.options.x || 0),
  927. y: box[1] + (this.options.y || 0),
  928. height: box[2] || 0,
  929. width: box[3] || 0
  930. };
  931. return {
  932. relativePosition: anchor,
  933. absolutePosition: merge(anchor, {
  934. x: anchor.x + plotBox.translateX,
  935. y: anchor.y + plotBox.translateY
  936. })
  937. };
  938. },
  939. /**
  940. * Map point's options to a point-like object.
  941. *
  942. * @param {string|Function|Highcharts.AnnotationMockPointOptionsObject|Highcharts.AnnotationPointType} pointOptions
  943. * Point's options.
  944. *
  945. * @param {Highcharts.AnnotationPointType} point
  946. * A point-like instance.
  947. *
  948. * @return {Highcharts.AnnotationPointType|null}
  949. * if the point is found/set returns this point, otherwise null
  950. */
  951. point: function (pointOptions, point) {
  952. if (pointOptions && pointOptions.series) {
  953. return pointOptions;
  954. }
  955. if (!point || point.series === null) {
  956. if (isObject(pointOptions)) {
  957. point = new MockPoint(this.chart, this, pointOptions);
  958. }
  959. else if (isString(pointOptions)) {
  960. point = this.chart.get(pointOptions) || null;
  961. }
  962. else if (typeof pointOptions === 'function') {
  963. var pointConfig = pointOptions.call(point, this);
  964. point = pointConfig.series ?
  965. pointConfig :
  966. new MockPoint(this.chart, this, pointOptions);
  967. }
  968. }
  969. return point;
  970. },
  971. /**
  972. * Find point-like objects based on points options.
  973. *
  974. * @return {Array<Annotation.PointLike>} an array of point-like objects
  975. */
  976. linkPoints: function () {
  977. var pointsOptions = this.getPointsOptions(), points = this.points, len = (pointsOptions && pointsOptions.length) || 0, i, point;
  978. for (i = 0; i < len; i++) {
  979. point = this.point(pointsOptions[i], points[i]);
  980. if (!point) {
  981. points.length = 0;
  982. return;
  983. }
  984. if (point.mock) {
  985. point.refresh();
  986. }
  987. points[i] = point;
  988. }
  989. return points;
  990. },
  991. /**
  992. * Add control points to a controllable.
  993. */
  994. addControlPoints: function () {
  995. var controlPointsOptions = this.options.controlPoints;
  996. (controlPointsOptions || []).forEach(function (controlPointOptions, i) {
  997. var options = merge(this.options.controlPointOptions, controlPointOptions);
  998. if (!options.index) {
  999. options.index = i;
  1000. }
  1001. controlPointsOptions[i] = options;
  1002. this.controlPoints.push(new ControlPoint(this.chart, this, options));
  1003. }, this);
  1004. },
  1005. /**
  1006. * Check if a controllable should be rendered/redrawn.
  1007. *
  1008. * @return {boolean}
  1009. * Whether a controllable should be drawn.
  1010. */
  1011. shouldBeDrawn: function () {
  1012. return Boolean(this.points.length);
  1013. },
  1014. /**
  1015. * Render a controllable.
  1016. */
  1017. render: function (_parentGroup) {
  1018. this.controlPoints.forEach(function (controlPoint) {
  1019. controlPoint.render();
  1020. });
  1021. },
  1022. /**
  1023. * Redraw a controllable.
  1024. *
  1025. * @param {boolean} [animation]
  1026. */
  1027. redraw: function (animation) {
  1028. this.controlPoints.forEach(function (controlPoint) {
  1029. controlPoint.redraw(animation);
  1030. });
  1031. },
  1032. /**
  1033. * Transform a controllable with a specific transformation.
  1034. *
  1035. * @param {string} transformation a transformation name
  1036. * @param {number|null} cx origin x transformation
  1037. * @param {number|null} cy origin y transformation
  1038. * @param {number} p1 param for the transformation
  1039. * @param {number} [p2] param for the transformation
  1040. */
  1041. transform: function (transformation, cx, cy, p1, p2) {
  1042. if (this.chart.inverted) {
  1043. var temp = cx;
  1044. cx = cy;
  1045. cy = temp;
  1046. }
  1047. this.points.forEach(function (point, i) {
  1048. this.transformPoint(transformation, cx, cy, p1, p2, i);
  1049. }, this);
  1050. },
  1051. /**
  1052. * Transform a point with a specific transformation
  1053. * If a transformed point is a real point it is replaced with
  1054. * the mock point.
  1055. *
  1056. * @param {string} transformation a transformation name
  1057. * @param {number|null} cx origin x transformation
  1058. * @param {number|null} cy origin y transformation
  1059. * @param {number} p1 param for the transformation
  1060. * @param {number|undefined} p2 param for the transformation
  1061. * @param {number} i index of the point
  1062. */
  1063. transformPoint: function (transformation, cx, cy, p1, p2, i) {
  1064. var point = this.points[i];
  1065. if (!point.mock) {
  1066. point = this.points[i] = MockPoint.fromPoint(point);
  1067. }
  1068. point[transformation](cx, cy, p1, p2);
  1069. },
  1070. /**
  1071. * Translate a controllable.
  1072. *
  1073. * @param {number} dx translation for x coordinate
  1074. * @param {number} dy translation for y coordinate
  1075. **/
  1076. translate: function (dx, dy) {
  1077. this.transform('translate', null, null, dx, dy);
  1078. },
  1079. /**
  1080. * Translate a specific point within a controllable.
  1081. *
  1082. * @param {number} dx translation for x coordinate
  1083. * @param {number} dy translation for y coordinate
  1084. * @param {number} i index of the point
  1085. **/
  1086. translatePoint: function (dx, dy, i) {
  1087. this.transformPoint('translate', null, null, dx, dy, i);
  1088. },
  1089. /**
  1090. * Translate shape within controllable item.
  1091. * Replaces `controllable.translate` method.
  1092. *
  1093. * @param {number} dx translation for x coordinate
  1094. * @param {number} dy translation for y coordinate
  1095. */
  1096. translateShape: function (dx, dy) {
  1097. var chart = this.annotation.chart,
  1098. // Annotation.options
  1099. shapeOptions = this.annotation.userOptions,
  1100. // Chart.options.annotations
  1101. annotationIndex = chart.annotations.indexOf(this.annotation), chartOptions = chart.options.annotations[annotationIndex];
  1102. this.translatePoint(dx, dy, 0);
  1103. // Options stored in:
  1104. // - chart (for exporting)
  1105. // - current config (for redraws)
  1106. chartOptions[this.collection][this.index].point = this.options.point;
  1107. shapeOptions[this.collection][this.index].point = this.options.point;
  1108. },
  1109. /**
  1110. * Rotate a controllable.
  1111. *
  1112. * @param {number} cx origin x rotation
  1113. * @param {number} cy origin y rotation
  1114. * @param {number} radians
  1115. **/
  1116. rotate: function (cx, cy, radians) {
  1117. this.transform('rotate', cx, cy, radians);
  1118. },
  1119. /**
  1120. * Scale a controllable.
  1121. *
  1122. * @param {number} cx origin x rotation
  1123. * @param {number} cy origin y rotation
  1124. * @param {number} sx scale factor x
  1125. * @param {number} sy scale factor y
  1126. */
  1127. scale: function (cx, cy, sx, sy) {
  1128. this.transform('scale', cx, cy, sx, sy);
  1129. },
  1130. /**
  1131. * Set control points' visibility.
  1132. *
  1133. * @param {boolean} visible
  1134. */
  1135. setControlPointsVisibility: function (visible) {
  1136. this.controlPoints.forEach(function (controlPoint) {
  1137. controlPoint.setVisibility(visible);
  1138. });
  1139. },
  1140. /**
  1141. * Destroy a controllable.
  1142. */
  1143. destroy: function () {
  1144. if (this.graphic) {
  1145. this.graphic = this.graphic.destroy();
  1146. }
  1147. if (this.tracker) {
  1148. this.tracker = this.tracker.destroy();
  1149. }
  1150. this.controlPoints.forEach(function (controlPoint) {
  1151. controlPoint.destroy();
  1152. });
  1153. this.chart = null;
  1154. this.points = null;
  1155. this.controlPoints = null;
  1156. this.options = null;
  1157. if (this.annotation) {
  1158. this.annotation = null;
  1159. }
  1160. },
  1161. /**
  1162. * Update a controllable.
  1163. *
  1164. * @param {Object} newOptions
  1165. */
  1166. update: function (newOptions) {
  1167. var annotation = this.annotation, options = merge(true, this.options, newOptions), parentGroup = this.graphic.parentGroup;
  1168. this.destroy();
  1169. this.constructor(annotation, options);
  1170. this.render(parentGroup);
  1171. this.redraw();
  1172. }
  1173. };
  1174. return controllableMixin;
  1175. });
  1176. _registerModule(_modules, 'annotations/controllable/markerMixin.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
  1177. /* *
  1178. *
  1179. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  1180. *
  1181. * */
  1182. var addEvent = U.addEvent, defined = U.defined, merge = U.merge, objectEach = U.objectEach, uniqueKey = U.uniqueKey;
  1183. /**
  1184. * Options for configuring markers for annotations.
  1185. *
  1186. * An example of the arrow marker:
  1187. * <pre>
  1188. * {
  1189. * arrow: {
  1190. * id: 'arrow',
  1191. * tagName: 'marker',
  1192. * refY: 5,
  1193. * refX: 5,
  1194. * markerWidth: 10,
  1195. * markerHeight: 10,
  1196. * children: [{
  1197. * tagName: 'path',
  1198. * attrs: {
  1199. * d: 'M 0 0 L 10 5 L 0 10 Z',
  1200. * strokeWidth: 0
  1201. * }
  1202. * }]
  1203. * }
  1204. * }
  1205. * </pre>
  1206. *
  1207. * @sample highcharts/annotations/custom-markers/
  1208. * Define a custom marker for annotations
  1209. *
  1210. * @sample highcharts/css/annotations-markers/
  1211. * Define markers in a styled mode
  1212. *
  1213. * @type {Highcharts.Dictionary<Highcharts.SVGDefinitionObject>}
  1214. * @since 6.0.0
  1215. * @optionparent defs
  1216. */
  1217. var defaultMarkers = {
  1218. /**
  1219. * @type {Highcharts.SVGDefinitionObject}
  1220. */
  1221. arrow: {
  1222. tagName: 'marker',
  1223. render: false,
  1224. id: 'arrow',
  1225. refY: 5,
  1226. refX: 9,
  1227. markerWidth: 10,
  1228. markerHeight: 10,
  1229. /**
  1230. * @type {Array<Highcharts.DefsOptions>}
  1231. */
  1232. children: [{
  1233. tagName: 'path',
  1234. d: 'M 0 0 L 10 5 L 0 10 Z',
  1235. strokeWidth: 0
  1236. }]
  1237. },
  1238. /**
  1239. * @type {Highcharts.SVGDefinitionObject}
  1240. */
  1241. 'reverse-arrow': {
  1242. tagName: 'marker',
  1243. render: false,
  1244. id: 'reverse-arrow',
  1245. refY: 5,
  1246. refX: 1,
  1247. markerWidth: 10,
  1248. markerHeight: 10,
  1249. children: [{
  1250. tagName: 'path',
  1251. // reverse triangle (used as an arrow)
  1252. d: 'M 0 5 L 10 0 L 10 10 Z',
  1253. strokeWidth: 0
  1254. }]
  1255. }
  1256. };
  1257. H.SVGRenderer.prototype.addMarker = function (id, markerOptions) {
  1258. var options = { id: id };
  1259. var attrs = {
  1260. stroke: markerOptions.color || 'none',
  1261. fill: markerOptions.color || 'rgba(0, 0, 0, 0.75)'
  1262. };
  1263. options.children = markerOptions.children.map(function (child) {
  1264. return merge(attrs, child);
  1265. });
  1266. var marker = this.definition(merge(true, {
  1267. markerWidth: 20,
  1268. markerHeight: 20,
  1269. refX: 0,
  1270. refY: 0,
  1271. orient: 'auto'
  1272. }, markerOptions, options));
  1273. marker.id = id;
  1274. return marker;
  1275. };
  1276. /* eslint-disable no-invalid-this, valid-jsdoc */
  1277. var createMarkerSetter = function (markerType) {
  1278. return function (value) {
  1279. this.attr(markerType, 'url(#' + value + ')');
  1280. };
  1281. };
  1282. /**
  1283. * @private
  1284. * @mixin
  1285. * @name Highcharts.AnnotaitonMarkerMixin
  1286. */
  1287. var markerMixin = {
  1288. markerEndSetter: createMarkerSetter('marker-end'),
  1289. markerStartSetter: createMarkerSetter('marker-start'),
  1290. /**
  1291. * Set markers.
  1292. * @private
  1293. * @param {Highcharts.AnnotationControllablePath} item
  1294. */
  1295. setItemMarkers: function (item) {
  1296. var itemOptions = item.options, chart = item.chart, defs = chart.options.defs, fill = itemOptions.fill, color = defined(fill) && fill !== 'none' ?
  1297. fill :
  1298. itemOptions.stroke, setMarker = function (markerType) {
  1299. var markerId = itemOptions[markerType], def, predefinedMarker, key, marker;
  1300. if (markerId) {
  1301. for (key in defs) { // eslint-disable-line guard-for-in
  1302. def = defs[key];
  1303. if (markerId === def.id &&
  1304. def.tagName === 'marker') {
  1305. predefinedMarker = def;
  1306. break;
  1307. }
  1308. }
  1309. if (predefinedMarker) {
  1310. marker = item[markerType] = chart.renderer
  1311. .addMarker((itemOptions.id || uniqueKey()) + '-' +
  1312. predefinedMarker.id, merge(predefinedMarker, { color: color }));
  1313. item.attr(markerType, marker.attr('id'));
  1314. }
  1315. }
  1316. };
  1317. ['markerStart', 'markerEnd'].forEach(setMarker);
  1318. }
  1319. };
  1320. addEvent(H.Chart, 'afterGetContainer', function () {
  1321. this.options.defs = merge(defaultMarkers, this.options.defs || {});
  1322. objectEach(this.options.defs, function (def) {
  1323. if (def.tagName === 'marker' && def.render !== false) {
  1324. this.renderer.addMarker(def.id, def);
  1325. }
  1326. }, this);
  1327. });
  1328. return markerMixin;
  1329. });
  1330. _registerModule(_modules, 'annotations/controllable/ControllablePath.js', [_modules['annotations/controllable/controllableMixin.js'], _modules['parts/Globals.js'], _modules['annotations/controllable/markerMixin.js'], _modules['parts/Utilities.js']], function (controllableMixin, H, markerMixin, U) {
  1331. /* *
  1332. *
  1333. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  1334. *
  1335. * */
  1336. var extend = U.extend, merge = U.merge;
  1337. // See TRACKER_FILL in highcharts.src.js
  1338. var TRACKER_FILL = 'rgba(192,192,192,' + (H.svg ? 0.0001 : 0.002) + ')';
  1339. /* eslint-disable no-invalid-this, valid-jsdoc */
  1340. /**
  1341. * A controllable path class.
  1342. *
  1343. * @requires modules/annotations
  1344. *
  1345. * @private
  1346. * @class
  1347. * @name Highcharts.AnnotationControllablePath
  1348. *
  1349. * @param {Highcharts.Annotation}
  1350. * Related annotation.
  1351. *
  1352. * @param {Highcharts.AnnotationsShapeOptions} options
  1353. * A path's options object.
  1354. *
  1355. * @param {number} index
  1356. * Index of the path.
  1357. **/
  1358. var ControllablePath = function (annotation, options, index) {
  1359. this.init(annotation, options, index);
  1360. this.collection = 'shapes';
  1361. };
  1362. /**
  1363. * A map object which allows to map options attributes to element attributes
  1364. *
  1365. * @name Highcharts.AnnotationControllablePath.attrsMap
  1366. * @type {Highcharts.Dictionary<string>}
  1367. */
  1368. ControllablePath.attrsMap = {
  1369. dashStyle: 'dashstyle',
  1370. strokeWidth: 'stroke-width',
  1371. stroke: 'stroke',
  1372. fill: 'fill',
  1373. zIndex: 'zIndex'
  1374. };
  1375. merge(true, ControllablePath.prototype, controllableMixin, /** @lends Highcharts.AnnotationControllablePath# */ {
  1376. /**
  1377. * @type 'path'
  1378. */
  1379. type: 'path',
  1380. setMarkers: markerMixin.setItemMarkers,
  1381. /**
  1382. * Map the controllable path to 'd' path attribute.
  1383. *
  1384. * @return {Highcharts.SVGPathArray|null}
  1385. * A path's d attribute.
  1386. */
  1387. toD: function () {
  1388. var dOption = this.options.d;
  1389. if (dOption) {
  1390. return typeof dOption === 'function' ?
  1391. dOption.call(this) :
  1392. dOption;
  1393. }
  1394. var points = this.points, len = points.length, showPath = len, point = points[0], position = showPath && this.anchor(point).absolutePosition, pointIndex = 0, command, d = [];
  1395. if (position) {
  1396. d.push(['M', position.x, position.y]);
  1397. while (++pointIndex < len && showPath) {
  1398. point = points[pointIndex];
  1399. command = point.command || 'L';
  1400. position = this.anchor(point).absolutePosition;
  1401. if (command === 'M') {
  1402. d.push([command, position.x, position.y]);
  1403. }
  1404. else if (command === 'L') {
  1405. d.push([command, position.x, position.y]);
  1406. }
  1407. else if (command === 'Z') {
  1408. d.push([command]);
  1409. }
  1410. showPath = point.series.visible;
  1411. }
  1412. }
  1413. return showPath ?
  1414. this.chart.renderer.crispLine(d, this.graphic.strokeWidth()) :
  1415. null;
  1416. },
  1417. shouldBeDrawn: function () {
  1418. return (controllableMixin.shouldBeDrawn.call(this) || Boolean(this.options.d));
  1419. },
  1420. render: function (parent) {
  1421. var options = this.options, attrs = this.attrsFromOptions(options);
  1422. this.graphic = this.annotation.chart.renderer
  1423. .path([['M', 0, 0]])
  1424. .attr(attrs)
  1425. .add(parent);
  1426. if (options.className) {
  1427. this.graphic.addClass(options.className);
  1428. }
  1429. this.tracker = this.annotation.chart.renderer
  1430. .path([['M', 0, 0]])
  1431. .addClass('highcharts-tracker-line')
  1432. .attr({
  1433. zIndex: 2
  1434. })
  1435. .add(parent);
  1436. if (!this.annotation.chart.styledMode) {
  1437. this.tracker.attr({
  1438. 'stroke-linejoin': 'round',
  1439. stroke: TRACKER_FILL,
  1440. fill: TRACKER_FILL,
  1441. 'stroke-width': this.graphic.strokeWidth() +
  1442. options.snap * 2
  1443. });
  1444. }
  1445. controllableMixin.render.call(this);
  1446. extend(this.graphic, {
  1447. markerStartSetter: markerMixin.markerStartSetter,
  1448. markerEndSetter: markerMixin.markerEndSetter
  1449. });
  1450. this.setMarkers(this);
  1451. },
  1452. redraw: function (animation) {
  1453. var d = this.toD(), action = animation ? 'animate' : 'attr';
  1454. if (d) {
  1455. this.graphic[action]({ d: d });
  1456. this.tracker[action]({ d: d });
  1457. }
  1458. else {
  1459. this.graphic.attr({ d: 'M 0 ' + -9e9 });
  1460. this.tracker.attr({ d: 'M 0 ' + -9e9 });
  1461. }
  1462. this.graphic.placed = this.tracker.placed = Boolean(d);
  1463. controllableMixin.redraw.call(this, animation);
  1464. }
  1465. });
  1466. return ControllablePath;
  1467. });
  1468. _registerModule(_modules, 'annotations/controllable/ControllableRect.js', [_modules['annotations/controllable/controllableMixin.js'], _modules['annotations/controllable/ControllablePath.js'], _modules['parts/Utilities.js']], function (controllableMixin, ControllablePath, U) {
  1469. /* *
  1470. *
  1471. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  1472. *
  1473. * */
  1474. var merge = U.merge;
  1475. /* eslint-disable no-invalid-this, valid-jsdoc */
  1476. /**
  1477. * A controllable rect class.
  1478. *
  1479. * @requires modules/annotations
  1480. *
  1481. * @private
  1482. * @class
  1483. * @name Highcharts.AnnotationControllableRect
  1484. *
  1485. * @param {Highcharts.Annotation} annotation
  1486. * An annotation instance.
  1487. *
  1488. * @param {Highcharts.AnnotationsShapeOptions} options
  1489. * A rect's options.
  1490. *
  1491. * @param {number} index
  1492. * Index of the rectangle
  1493. */
  1494. var ControllableRect = function (annotation, options, index) {
  1495. this.init(annotation, options, index);
  1496. this.collection = 'shapes';
  1497. };
  1498. /**
  1499. * @typedef {Annotation.ControllablePath.AttrsMap}
  1500. * Annotation.ControllableRect.AttrsMap
  1501. * @property {string} width=width
  1502. * @property {string} height=height
  1503. */
  1504. /**
  1505. * A map object which allows to map options attributes to element attributes
  1506. *
  1507. * @type {Annotation.ControllableRect.AttrsMap}
  1508. */
  1509. ControllableRect.attrsMap = merge(ControllablePath.attrsMap, {
  1510. width: 'width',
  1511. height: 'height'
  1512. });
  1513. merge(true, ControllableRect.prototype, controllableMixin, /** @lends Annotation.ControllableRect# */ {
  1514. /**
  1515. * @type 'rect'
  1516. */
  1517. type: 'rect',
  1518. translate: controllableMixin.translateShape,
  1519. render: function (parent) {
  1520. var attrs = this.attrsFromOptions(this.options);
  1521. this.graphic = this.annotation.chart.renderer
  1522. .rect(0, -9e9, 0, 0)
  1523. .attr(attrs)
  1524. .add(parent);
  1525. controllableMixin.render.call(this);
  1526. },
  1527. redraw: function (animation) {
  1528. var position = this.anchor(this.points[0]).absolutePosition;
  1529. if (position) {
  1530. this.graphic[animation ? 'animate' : 'attr']({
  1531. x: position.x,
  1532. y: position.y,
  1533. width: this.options.width,
  1534. height: this.options.height
  1535. });
  1536. }
  1537. else {
  1538. this.attr({
  1539. x: 0,
  1540. y: -9e9
  1541. });
  1542. }
  1543. this.graphic.placed = Boolean(position);
  1544. controllableMixin.redraw.call(this, animation);
  1545. }
  1546. });
  1547. return ControllableRect;
  1548. });
  1549. _registerModule(_modules, 'annotations/controllable/ControllableCircle.js', [_modules['annotations/controllable/controllableMixin.js'], _modules['annotations/controllable/ControllablePath.js'], _modules['parts/Utilities.js']], function (controllableMixin, ControllablePath, U) {
  1550. /* *
  1551. *
  1552. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  1553. *
  1554. * */
  1555. var merge = U.merge;
  1556. /* eslint-disable no-invalid-this, valid-jsdoc */
  1557. /**
  1558. * A controllable circle class.
  1559. *
  1560. * @requires modules/annotations
  1561. *
  1562. * @private
  1563. * @constructor
  1564. * @name Highcharts.AnnotationControllableCircle
  1565. *
  1566. * @param {Highcharts.Annotation} annotation an annotation instance
  1567. * @param {Highcharts.AnnotationsShapeOptions} options a shape's options
  1568. * @param {number} index of the circle
  1569. **/
  1570. var ControllableCircle = function (annotation, options, index) {
  1571. this.init(annotation, options, index);
  1572. this.collection = 'shapes';
  1573. };
  1574. /**
  1575. * A map object which allows to map options attributes to element attributes.
  1576. *
  1577. * @name Highcharts.AnnotationControllableCircle.attrsMap
  1578. * @type {Highcharts.Dictionary<string>}
  1579. */
  1580. ControllableCircle.attrsMap = merge(ControllablePath.attrsMap, {
  1581. r: 'r'
  1582. });
  1583. merge(true, ControllableCircle.prototype, controllableMixin, /** @lends Highcharts.AnnotationControllableCircle# */ {
  1584. /**
  1585. * @type 'circle'
  1586. */
  1587. type: 'circle',
  1588. translate: controllableMixin.translateShape,
  1589. render: function (parent) {
  1590. var attrs = this.attrsFromOptions(this.options);
  1591. this.graphic = this.annotation.chart.renderer
  1592. .circle(0, -9e9, 0)
  1593. .attr(attrs)
  1594. .add(parent);
  1595. controllableMixin.render.call(this);
  1596. },
  1597. redraw: function (animation) {
  1598. var position = this.anchor(this.points[0]).absolutePosition;
  1599. if (position) {
  1600. this.graphic[animation ? 'animate' : 'attr']({
  1601. x: position.x,
  1602. y: position.y,
  1603. r: this.options.r
  1604. });
  1605. }
  1606. else {
  1607. this.graphic.attr({
  1608. x: 0,
  1609. y: -9e9
  1610. });
  1611. }
  1612. this.graphic.placed = Boolean(position);
  1613. controllableMixin.redraw.call(this, animation);
  1614. },
  1615. /**
  1616. * Set the radius.
  1617. *
  1618. * @param {number} r a radius to be set
  1619. */
  1620. setRadius: function (r) {
  1621. this.options.r = r;
  1622. }
  1623. });
  1624. return ControllableCircle;
  1625. });
  1626. _registerModule(_modules, 'annotations/controllable/ControllableLabel.js', [_modules['annotations/controllable/controllableMixin.js'], _modules['parts/Globals.js'], _modules['annotations/MockPoint.js'], _modules['parts/Tooltip.js'], _modules['parts/Utilities.js']], function (controllableMixin, H, MockPoint, Tooltip, U) {
  1627. /* *
  1628. *
  1629. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  1630. *
  1631. * */
  1632. var extend = U.extend, format = U.format, isNumber = U.isNumber, merge = U.merge, pick = U.pick;
  1633. /* eslint-disable no-invalid-this, valid-jsdoc */
  1634. /**
  1635. * A controllable label class.
  1636. *
  1637. * @requires modules/annotations
  1638. *
  1639. * @private
  1640. * @class
  1641. * @name Highcharts.AnnotationControllableLabel
  1642. *
  1643. * @param {Highcharts.Annotation} annotation
  1644. * An annotation instance.
  1645. * @param {Highcharts.AnnotationsLabelOptions} options
  1646. * A label's options.
  1647. * @param {number} index
  1648. * Index of the label.
  1649. */
  1650. var ControllableLabel = function (annotation, options, index) {
  1651. this.init(annotation, options, index);
  1652. this.collection = 'labels';
  1653. };
  1654. /**
  1655. * Shapes which do not have background - the object is used for proper
  1656. * setting of the contrast color.
  1657. *
  1658. * @type {Array<string>}
  1659. */
  1660. ControllableLabel.shapesWithoutBackground = ['connector'];
  1661. /**
  1662. * Returns new aligned position based alignment options and box to align to.
  1663. * It is almost a one-to-one copy from SVGElement.prototype.align
  1664. * except it does not use and mutate an element
  1665. *
  1666. * @param {Highcharts.AnnotationAlignObject} alignOptions
  1667. *
  1668. * @param {Highcharts.BBoxObject} box
  1669. *
  1670. * @return {Highcharts.PositionObject}
  1671. * Aligned position.
  1672. */
  1673. ControllableLabel.alignedPosition = function (alignOptions, box) {
  1674. var align = alignOptions.align, vAlign = alignOptions.verticalAlign, x = (box.x || 0) + (alignOptions.x || 0), y = (box.y || 0) + (alignOptions.y || 0), alignFactor, vAlignFactor;
  1675. if (align === 'right') {
  1676. alignFactor = 1;
  1677. }
  1678. else if (align === 'center') {
  1679. alignFactor = 2;
  1680. }
  1681. if (alignFactor) {
  1682. x += (box.width - (alignOptions.width || 0)) / alignFactor;
  1683. }
  1684. if (vAlign === 'bottom') {
  1685. vAlignFactor = 1;
  1686. }
  1687. else if (vAlign === 'middle') {
  1688. vAlignFactor = 2;
  1689. }
  1690. if (vAlignFactor) {
  1691. y += (box.height - (alignOptions.height || 0)) / vAlignFactor;
  1692. }
  1693. return {
  1694. x: Math.round(x),
  1695. y: Math.round(y)
  1696. };
  1697. };
  1698. /**
  1699. * Returns new alignment options for a label if the label is outside the
  1700. * plot area. It is almost a one-to-one copy from
  1701. * Series.prototype.justifyDataLabel except it does not mutate the label and
  1702. * it works with absolute instead of relative position.
  1703. */
  1704. ControllableLabel.justifiedOptions = function (chart, label, alignOptions, alignAttr) {
  1705. var align = alignOptions.align, verticalAlign = alignOptions.verticalAlign, padding = label.box ? 0 : (label.padding || 0), bBox = label.getBBox(), off,
  1706. //
  1707. options = {
  1708. align: align,
  1709. verticalAlign: verticalAlign,
  1710. x: alignOptions.x,
  1711. y: alignOptions.y,
  1712. width: label.width,
  1713. height: label.height
  1714. },
  1715. //
  1716. x = alignAttr.x - chart.plotLeft, y = alignAttr.y - chart.plotTop;
  1717. // Off left
  1718. off = x + padding;
  1719. if (off < 0) {
  1720. if (align === 'right') {
  1721. options.align = 'left';
  1722. }
  1723. else {
  1724. options.x = -off;
  1725. }
  1726. }
  1727. // Off right
  1728. off = x + bBox.width - padding;
  1729. if (off > chart.plotWidth) {
  1730. if (align === 'left') {
  1731. options.align = 'right';
  1732. }
  1733. else {
  1734. options.x = chart.plotWidth - off;
  1735. }
  1736. }
  1737. // Off top
  1738. off = y + padding;
  1739. if (off < 0) {
  1740. if (verticalAlign === 'bottom') {
  1741. options.verticalAlign = 'top';
  1742. }
  1743. else {
  1744. options.y = -off;
  1745. }
  1746. }
  1747. // Off bottom
  1748. off = y + bBox.height - padding;
  1749. if (off > chart.plotHeight) {
  1750. if (verticalAlign === 'top') {
  1751. options.verticalAlign = 'bottom';
  1752. }
  1753. else {
  1754. options.y = chart.plotHeight - off;
  1755. }
  1756. }
  1757. return options;
  1758. };
  1759. /**
  1760. * A map object which allows to map options attributes to element attributes
  1761. *
  1762. * @type {Highcharts.Dictionary<string>}
  1763. */
  1764. ControllableLabel.attrsMap = {
  1765. backgroundColor: 'fill',
  1766. borderColor: 'stroke',
  1767. borderWidth: 'stroke-width',
  1768. zIndex: 'zIndex',
  1769. borderRadius: 'r',
  1770. padding: 'padding'
  1771. };
  1772. merge(true, ControllableLabel.prototype, controllableMixin,
  1773. /** @lends Annotation.ControllableLabel# */ {
  1774. /**
  1775. * Translate the point of the label by deltaX and deltaY translations.
  1776. * The point is the label's anchor.
  1777. *
  1778. * @param {number} dx translation for x coordinate
  1779. * @param {number} dy translation for y coordinate
  1780. **/
  1781. translatePoint: function (dx, dy) {
  1782. controllableMixin.translatePoint.call(this, dx, dy, 0);
  1783. },
  1784. /**
  1785. * Translate x and y position relative to the label's anchor.
  1786. *
  1787. * @param {number} dx translation for x coordinate
  1788. * @param {number} dy translation for y coordinate
  1789. **/
  1790. translate: function (dx, dy) {
  1791. var chart = this.annotation.chart,
  1792. // Annotation.options
  1793. labelOptions = this.annotation.userOptions,
  1794. // Chart.options.annotations
  1795. annotationIndex = chart.annotations.indexOf(this.annotation), chartAnnotations = chart.options.annotations, chartOptions = chartAnnotations[annotationIndex], temp;
  1796. if (chart.inverted) {
  1797. temp = dx;
  1798. dx = dy;
  1799. dy = temp;
  1800. }
  1801. // Local options:
  1802. this.options.x += dx;
  1803. this.options.y += dy;
  1804. // Options stored in chart:
  1805. chartOptions[this.collection][this.index].x = this.options.x;
  1806. chartOptions[this.collection][this.index].y = this.options.y;
  1807. labelOptions[this.collection][this.index].x = this.options.x;
  1808. labelOptions[this.collection][this.index].y = this.options.y;
  1809. },
  1810. render: function (parent) {
  1811. var options = this.options, attrs = this.attrsFromOptions(options), style = options.style;
  1812. this.graphic = this.annotation.chart.renderer
  1813. .label('', 0, -9999, // #10055
  1814. options.shape, null, null, options.useHTML, null, 'annotation-label')
  1815. .attr(attrs)
  1816. .add(parent);
  1817. if (!this.annotation.chart.styledMode) {
  1818. if (style.color === 'contrast') {
  1819. style.color = this.annotation.chart.renderer.getContrast(ControllableLabel.shapesWithoutBackground.indexOf(options.shape) > -1 ? '#FFFFFF' : options.backgroundColor);
  1820. }
  1821. this.graphic
  1822. .css(options.style)
  1823. .shadow(options.shadow);
  1824. }
  1825. if (options.className) {
  1826. this.graphic.addClass(options.className);
  1827. }
  1828. this.graphic.labelrank = options.labelrank;
  1829. controllableMixin.render.call(this);
  1830. },
  1831. redraw: function (animation) {
  1832. var options = this.options, text = this.text || options.format || options.text, label = this.graphic, point = this.points[0], show = false, anchor, attrs;
  1833. label.attr({
  1834. text: text ?
  1835. format(text, point.getLabelConfig(), this.annotation.chart) :
  1836. options.formatter.call(point, this)
  1837. });
  1838. anchor = this.anchor(point);
  1839. attrs = this.position(anchor);
  1840. show = attrs;
  1841. if (show) {
  1842. label.alignAttr = attrs;
  1843. attrs.anchorX = anchor.absolutePosition.x;
  1844. attrs.anchorY = anchor.absolutePosition.y;
  1845. label[animation ? 'animate' : 'attr'](attrs);
  1846. }
  1847. else {
  1848. label.attr({
  1849. x: 0,
  1850. y: -9999 // #10055
  1851. });
  1852. }
  1853. label.placed = Boolean(show);
  1854. controllableMixin.redraw.call(this, animation);
  1855. },
  1856. /**
  1857. * All basic shapes don't support alignTo() method except label.
  1858. * For a controllable label, we need to subtract translation from
  1859. * options.
  1860. */
  1861. anchor: function () {
  1862. var anchor = controllableMixin.anchor.apply(this, arguments), x = this.options.x || 0, y = this.options.y || 0;
  1863. anchor.absolutePosition.x -= x;
  1864. anchor.absolutePosition.y -= y;
  1865. anchor.relativePosition.x -= x;
  1866. anchor.relativePosition.y -= y;
  1867. return anchor;
  1868. },
  1869. /**
  1870. * Returns the label position relative to its anchor.
  1871. *
  1872. * @param {Highcharts.AnnotationAnchorObject} anchor
  1873. *
  1874. * @return {Highcharts.PositionObject|null}
  1875. */
  1876. position: function (anchor) {
  1877. var item = this.graphic, chart = this.annotation.chart, point = this.points[0], itemOptions = this.options, anchorAbsolutePosition = anchor.absolutePosition, anchorRelativePosition = anchor.relativePosition, itemPosition, alignTo, itemPosRelativeX, itemPosRelativeY, showItem = point.series.visible &&
  1878. MockPoint.prototype.isInsidePlot.call(point);
  1879. if (showItem) {
  1880. if (itemOptions.distance) {
  1881. itemPosition = Tooltip.prototype.getPosition.call({
  1882. chart: chart,
  1883. distance: pick(itemOptions.distance, 16)
  1884. }, item.width, item.height, {
  1885. plotX: anchorRelativePosition.x,
  1886. plotY: anchorRelativePosition.y,
  1887. negative: point.negative,
  1888. ttBelow: point.ttBelow,
  1889. h: (anchorRelativePosition.height || anchorRelativePosition.width)
  1890. });
  1891. }
  1892. else if (itemOptions.positioner) {
  1893. itemPosition = itemOptions.positioner.call(this);
  1894. }
  1895. else {
  1896. alignTo = {
  1897. x: anchorAbsolutePosition.x,
  1898. y: anchorAbsolutePosition.y,
  1899. width: 0,
  1900. height: 0
  1901. };
  1902. itemPosition = ControllableLabel.alignedPosition(extend(itemOptions, {
  1903. width: item.width,
  1904. height: item.height
  1905. }), alignTo);
  1906. if (this.options.overflow === 'justify') {
  1907. itemPosition = ControllableLabel.alignedPosition(ControllableLabel.justifiedOptions(chart, item, itemOptions, itemPosition), alignTo);
  1908. }
  1909. }
  1910. if (itemOptions.crop) {
  1911. itemPosRelativeX = itemPosition.x - chart.plotLeft;
  1912. itemPosRelativeY = itemPosition.y - chart.plotTop;
  1913. showItem =
  1914. chart.isInsidePlot(itemPosRelativeX, itemPosRelativeY) &&
  1915. chart.isInsidePlot(itemPosRelativeX + item.width, itemPosRelativeY + item.height);
  1916. }
  1917. }
  1918. return showItem ? itemPosition : null;
  1919. }
  1920. });
  1921. /* ********************************************************************** */
  1922. /**
  1923. * General symbol definition for labels with connector
  1924. * @private
  1925. */
  1926. H.SVGRenderer.prototype.symbols.connector = function (x, y, w, h, options) {
  1927. var anchorX = options && options.anchorX, anchorY = options && options.anchorY, path, yOffset, lateral = w / 2;
  1928. if (isNumber(anchorX) && isNumber(anchorY)) {
  1929. path = [['M', anchorX, anchorY]];
  1930. // Prefer 45 deg connectors
  1931. yOffset = y - anchorY;
  1932. if (yOffset < 0) {
  1933. yOffset = -h - yOffset;
  1934. }
  1935. if (yOffset < w) {
  1936. lateral = anchorX < x + (w / 2) ? yOffset : w - yOffset;
  1937. }
  1938. // Anchor below label
  1939. if (anchorY > y + h) {
  1940. path.push(['L', x + lateral, y + h]);
  1941. // Anchor above label
  1942. }
  1943. else if (anchorY < y) {
  1944. path.push(['L', x + lateral, y]);
  1945. // Anchor left of label
  1946. }
  1947. else if (anchorX < x) {
  1948. path.push(['L', x, y + h / 2]);
  1949. // Anchor right of label
  1950. }
  1951. else if (anchorX > x + w) {
  1952. path.push(['L', x + w, y + h / 2]);
  1953. }
  1954. }
  1955. return path || [];
  1956. };
  1957. return ControllableLabel;
  1958. });
  1959. _registerModule(_modules, 'annotations/controllable/ControllableImage.js', [_modules['annotations/controllable/ControllableLabel.js'], _modules['annotations/controllable/controllableMixin.js'], _modules['parts/Utilities.js']], function (ControllableLabel, controllableMixin, U) {
  1960. /* *
  1961. *
  1962. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  1963. *
  1964. * */
  1965. var merge = U.merge;
  1966. /* eslint-disable no-invalid-this, valid-jsdoc */
  1967. /**
  1968. * A controllable image class.
  1969. *
  1970. * @requires modules/annotations
  1971. *
  1972. * @private
  1973. * @class
  1974. * @name Highcharts.AnnotationControllableImage
  1975. *
  1976. * @param {Highcharts.Annotation} annotation
  1977. * An annotation instance.
  1978. *
  1979. * @param {Highcharts.AnnotationsShapeOptions} options
  1980. * A controllable's options.
  1981. *
  1982. * @param {number} index
  1983. * Index of the image.
  1984. **/
  1985. var ControllableImage = function (annotation, options, index) {
  1986. this.init(annotation, options, index);
  1987. this.collection = 'shapes';
  1988. };
  1989. /**
  1990. * A map object which allows to map options attributes to element attributes
  1991. *
  1992. * @name Highcharts.AnnotationControllableImage.attrsMap
  1993. * @type {Highcharts.Dictionary<string>}
  1994. */
  1995. ControllableImage.attrsMap = {
  1996. width: 'width',
  1997. height: 'height',
  1998. zIndex: 'zIndex'
  1999. };
  2000. merge(true, ControllableImage.prototype, controllableMixin, /** @lends Annotation.ControllableImage# */ {
  2001. /**
  2002. * @type 'image'
  2003. */
  2004. type: 'image',
  2005. translate: controllableMixin.translateShape,
  2006. render: function (parent) {
  2007. var attrs = this.attrsFromOptions(this.options), options = this.options;
  2008. this.graphic = this.annotation.chart.renderer
  2009. .image(options.src, 0, -9e9, options.width, options.height)
  2010. .attr(attrs)
  2011. .add(parent);
  2012. this.graphic.width = options.width;
  2013. this.graphic.height = options.height;
  2014. controllableMixin.render.call(this);
  2015. },
  2016. redraw: function (animation) {
  2017. var anchor = this.anchor(this.points[0]), position = ControllableLabel.prototype.position.call(this, anchor);
  2018. if (position) {
  2019. this.graphic[animation ? 'animate' : 'attr']({
  2020. x: position.x,
  2021. y: position.y
  2022. });
  2023. }
  2024. else {
  2025. this.graphic.attr({
  2026. x: 0,
  2027. y: -9e9
  2028. });
  2029. }
  2030. this.graphic.placed = Boolean(position);
  2031. controllableMixin.redraw.call(this, animation);
  2032. }
  2033. });
  2034. return ControllableImage;
  2035. });
  2036. _registerModule(_modules, 'annotations/annotations.src.js', [_modules['parts/Chart.js'], _modules['annotations/controllable/controllableMixin.js'], _modules['annotations/controllable/ControllableRect.js'], _modules['annotations/controllable/ControllableCircle.js'], _modules['annotations/controllable/ControllablePath.js'], _modules['annotations/controllable/ControllableImage.js'], _modules['annotations/controllable/ControllableLabel.js'], _modules['annotations/ControlPoint.js'], _modules['annotations/eventEmitterMixin.js'], _modules['parts/Globals.js'], _modules['annotations/MockPoint.js'], _modules['parts/Pointer.js'], _modules['parts/Utilities.js']], function (Chart, ControllableMixin, ControllableRect, ControllableCircle, ControllablePath, ControllableImage, ControllableLabel, ControlPoint, EventEmitterMixin, H, MockPoint, Pointer, U) {
  2037. /* *
  2038. *
  2039. * (c) 2009-2017 Highsoft, Black Label
  2040. *
  2041. * License: www.highcharts.com/license
  2042. *
  2043. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  2044. *
  2045. * */
  2046. var chartProto = Chart.prototype;
  2047. var addEvent = U.addEvent, defined = U.defined, destroyObjectProperties = U.destroyObjectProperties, erase = U.erase, extend = U.extend, find = U.find, fireEvent = U.fireEvent, merge = U.merge, pick = U.pick, splat = U.splat, wrap = U.wrap;
  2048. /* *********************************************************************
  2049. *
  2050. * ANNOTATION
  2051. *
  2052. ******************************************************************** */
  2053. /**
  2054. * Possible directions for draggable annotations. An empty string (`''`)
  2055. * makes the annotation undraggable.
  2056. *
  2057. * @typedef {''|'x'|'xy'|'y'} Highcharts.AnnotationDraggableValue
  2058. */
  2059. /**
  2060. * @private
  2061. * @typedef {
  2062. * Highcharts.AnnotationControllableCircle|
  2063. * Highcharts.AnnotationControllableImage|
  2064. * Highcharts.AnnotationControllablePath|
  2065. * Highcharts.AnnotationControllableRect
  2066. * }
  2067. * Highcharts.AnnotationShapeType
  2068. * @requires modules/annotations
  2069. */
  2070. /**
  2071. * @private
  2072. * @typedef {
  2073. * Highcharts.AnnotationControllableLabel
  2074. * }
  2075. * Highcharts.AnnotationLabelType
  2076. * @requires modules/annotations
  2077. */
  2078. /**
  2079. * A point-like object, a mock point or a point used in series.
  2080. * @private
  2081. * @typedef {Highcharts.AnnotationMockPoint|Highcharts.Point} Highcharts.AnnotationPointType
  2082. * @requires modules/annotations
  2083. */
  2084. /* eslint-disable no-invalid-this, valid-jsdoc */
  2085. /**
  2086. * An annotation class which serves as a container for items like labels or
  2087. * shapes. Created items are positioned on the chart either by linking them to
  2088. * existing points or created mock points
  2089. *
  2090. * @class
  2091. * @name Highcharts.Annotation
  2092. *
  2093. * @param {Highcharts.Chart} chart a chart instance
  2094. * @param {Highcharts.AnnotationsOptions} userOptions the options object
  2095. */
  2096. var Annotation = /** @class */ (function () {
  2097. /* *
  2098. *
  2099. * Constructors
  2100. *
  2101. * */
  2102. function Annotation(chart, userOptions) {
  2103. /* *
  2104. *
  2105. * Properties
  2106. *
  2107. * */
  2108. this.annotation = void 0;
  2109. this.coll = 'annotations';
  2110. this.collection = void 0;
  2111. this.graphic = void 0;
  2112. this.group = void 0;
  2113. this.labelCollector = void 0;
  2114. this.labelsGroup = void 0;
  2115. this.shapesGroup = void 0;
  2116. var labelsAndShapes;
  2117. /**
  2118. * The chart that the annotation belongs to.
  2119. *
  2120. * @type {Highcharts.Chart}
  2121. */
  2122. this.chart = chart;
  2123. /**
  2124. * The array of points which defines the annotation.
  2125. *
  2126. * @type {Array<Highcharts.Point>}
  2127. */
  2128. this.points = [];
  2129. /**
  2130. * The array of control points.
  2131. *
  2132. * @private
  2133. * @name Highcharts.Annotation#controlPoints
  2134. * @type {Array<Annotation.ControlPoint>}
  2135. */
  2136. this.controlPoints = [];
  2137. this.coll = 'annotations';
  2138. /**
  2139. * The array of labels which belong to the annotation.
  2140. *
  2141. * @private
  2142. * @name Highcharts.Annotation#labels
  2143. * @type {Array<Highcharts.AnnotationLabelType>}
  2144. */
  2145. this.labels = [];
  2146. /**
  2147. * The array of shapes which belong to the annotation.
  2148. *
  2149. * @private
  2150. * @name Highcharts.Annotation#shapes
  2151. * @type {Array<Highcharts.AnnotationShapeType>}
  2152. */
  2153. this.shapes = [];
  2154. /**
  2155. * The options for the annotations.
  2156. *
  2157. * @name Highcharts.Annotation#options
  2158. * @type {Highcharts.AnnotationsOptions}
  2159. */
  2160. this.options = merge(this.defaultOptions, userOptions);
  2161. /**
  2162. * The user options for the annotations.
  2163. *
  2164. * @name Highcharts.Annotation#userOptions
  2165. * @type {Highcharts.AnnotationsOptions}
  2166. */
  2167. this.userOptions = userOptions;
  2168. // Handle labels and shapes - those are arrays
  2169. // Merging does not work with arrays (stores reference)
  2170. labelsAndShapes = this.getLabelsAndShapesOptions(this.options, userOptions);
  2171. this.options.labels = labelsAndShapes.labels;
  2172. this.options.shapes = labelsAndShapes.shapes;
  2173. /**
  2174. * The callback that reports to the overlapping-labels module which
  2175. * labels it should account for.
  2176. * @private
  2177. * @name Highcharts.Annotation#labelCollector
  2178. * @type {Function}
  2179. */
  2180. /**
  2181. * The group svg element.
  2182. *
  2183. * @name Highcharts.Annotation#group
  2184. * @type {Highcharts.SVGElement}
  2185. */
  2186. /**
  2187. * The group svg element of the annotation's shapes.
  2188. *
  2189. * @name Highcharts.Annotation#shapesGroup
  2190. * @type {Highcharts.SVGElement}
  2191. */
  2192. /**
  2193. * The group svg element of the annotation's labels.
  2194. *
  2195. * @name Highcharts.Annotation#labelsGroup
  2196. * @type {Highcharts.SVGElement}
  2197. */
  2198. this.init(chart, this.options);
  2199. }
  2200. /**
  2201. * Initialize the annotation.
  2202. * @private
  2203. */
  2204. Annotation.prototype.init = function () {
  2205. this.linkPoints();
  2206. this.addControlPoints();
  2207. this.addShapes();
  2208. this.addLabels();
  2209. this.setLabelCollector();
  2210. };
  2211. Annotation.prototype.getLabelsAndShapesOptions = function (baseOptions, newOptions) {
  2212. var mergedOptions = {};
  2213. ['labels', 'shapes'].forEach(function (name) {
  2214. if (baseOptions[name]) {
  2215. mergedOptions[name] = splat(newOptions[name]).map(function (basicOptions, i) {
  2216. return merge(baseOptions[name][i], basicOptions);
  2217. });
  2218. }
  2219. });
  2220. return mergedOptions;
  2221. };
  2222. Annotation.prototype.addShapes = function () {
  2223. (this.options.shapes || []).forEach(function (shapeOptions, i) {
  2224. var shape = this.initShape(shapeOptions, i);
  2225. merge(true, this.options.shapes[i], shape.options);
  2226. }, this);
  2227. };
  2228. Annotation.prototype.addLabels = function () {
  2229. (this.options.labels || []).forEach(function (labelsOptions, i) {
  2230. var labels = this.initLabel(labelsOptions, i);
  2231. merge(true, this.options.labels[i], labels.options);
  2232. }, this);
  2233. };
  2234. Annotation.prototype.addClipPaths = function () {
  2235. this.setClipAxes();
  2236. if (this.clipXAxis && this.clipYAxis) {
  2237. this.clipRect = this.chart.renderer.clipRect(this.getClipBox());
  2238. }
  2239. };
  2240. Annotation.prototype.setClipAxes = function () {
  2241. var xAxes = this.chart.xAxis, yAxes = this.chart.yAxis, linkedAxes = (this.options.labels || [])
  2242. .concat(this.options.shapes || [])
  2243. .reduce(function (axes, labelOrShape) {
  2244. return [
  2245. xAxes[labelOrShape &&
  2246. labelOrShape.point &&
  2247. labelOrShape.point.xAxis] || axes[0],
  2248. yAxes[labelOrShape &&
  2249. labelOrShape.point &&
  2250. labelOrShape.point.yAxis] || axes[1]
  2251. ];
  2252. }, []);
  2253. this.clipXAxis = linkedAxes[0];
  2254. this.clipYAxis = linkedAxes[1];
  2255. };
  2256. Annotation.prototype.getClipBox = function () {
  2257. if (this.clipXAxis && this.clipYAxis) {
  2258. return {
  2259. x: this.clipXAxis.left,
  2260. y: this.clipYAxis.top,
  2261. width: this.clipXAxis.width,
  2262. height: this.clipYAxis.height
  2263. };
  2264. }
  2265. };
  2266. Annotation.prototype.setLabelCollector = function () {
  2267. var annotation = this;
  2268. annotation.labelCollector = function () {
  2269. return annotation.labels.reduce(function (labels, label) {
  2270. if (!label.options.allowOverlap) {
  2271. labels.push(label.graphic);
  2272. }
  2273. return labels;
  2274. }, []);
  2275. };
  2276. annotation.chart.labelCollectors.push(annotation.labelCollector);
  2277. };
  2278. /**
  2279. * Set an annotation options.
  2280. * @private
  2281. * @param {Highcharts.AnnotationsOptions} - user options for an annotation
  2282. */
  2283. Annotation.prototype.setOptions = function (userOptions) {
  2284. this.options = merge(this.defaultOptions, userOptions);
  2285. };
  2286. Annotation.prototype.redraw = function (animation) {
  2287. this.linkPoints();
  2288. if (!this.graphic) {
  2289. this.render();
  2290. }
  2291. if (this.clipRect) {
  2292. this.clipRect.animate(this.getClipBox());
  2293. }
  2294. this.redrawItems(this.shapes, animation);
  2295. this.redrawItems(this.labels, animation);
  2296. ControllableMixin.redraw.call(this, animation);
  2297. };
  2298. /**
  2299. * @private
  2300. * @param {Array<Highcharts.AnnotationControllable>} items
  2301. * @param {boolean} [animation]
  2302. */
  2303. Annotation.prototype.redrawItems = function (items, animation) {
  2304. var i = items.length;
  2305. // needs a backward loop
  2306. // labels/shapes array might be modified
  2307. // due to destruction of the item
  2308. while (i--) {
  2309. this.redrawItem(items[i], animation);
  2310. }
  2311. };
  2312. /**
  2313. * @private
  2314. * @param {Array<Highcharts.AnnotationControllable>} items
  2315. */
  2316. Annotation.prototype.renderItems = function (items) {
  2317. var i = items.length;
  2318. while (i--) {
  2319. this.renderItem(items[i]);
  2320. }
  2321. };
  2322. Annotation.prototype.render = function () {
  2323. var renderer = this.chart.renderer;
  2324. this.graphic = renderer
  2325. .g('annotation')
  2326. .attr({
  2327. zIndex: this.options.zIndex,
  2328. visibility: this.options.visible ?
  2329. 'visible' :
  2330. 'hidden'
  2331. })
  2332. .add();
  2333. this.shapesGroup = renderer
  2334. .g('annotation-shapes')
  2335. .add(this.graphic)
  2336. .clip(this.chart.plotBoxClip);
  2337. this.labelsGroup = renderer
  2338. .g('annotation-labels')
  2339. .attr({
  2340. // hideOverlappingLabels requires translation
  2341. translateX: 0,
  2342. translateY: 0
  2343. })
  2344. .add(this.graphic);
  2345. this.addClipPaths();
  2346. if (this.clipRect) {
  2347. this.graphic.clip(this.clipRect);
  2348. }
  2349. // Render shapes and labels before adding events (#13070).
  2350. this.renderItems(this.shapes);
  2351. this.renderItems(this.labels);
  2352. this.addEvents();
  2353. ControllableMixin.render.call(this);
  2354. };
  2355. /**
  2356. * Set the annotation's visibility.
  2357. * @private
  2358. * @param {boolean} [visible]
  2359. * Whether to show or hide an annotation. If the param is omitted, the
  2360. * annotation's visibility is toggled.
  2361. */
  2362. Annotation.prototype.setVisibility = function (visible) {
  2363. var options = this.options, visibility = pick(visible, !options.visible);
  2364. this.graphic.attr('visibility', visibility ? 'visible' : 'hidden');
  2365. if (!visibility) {
  2366. this.setControlPointsVisibility(false);
  2367. }
  2368. options.visible = visibility;
  2369. };
  2370. Annotation.prototype.setControlPointsVisibility = function (visible) {
  2371. var setItemControlPointsVisibility = function (item) {
  2372. item.setControlPointsVisibility(visible);
  2373. };
  2374. ControllableMixin.setControlPointsVisibility.call(this, visible);
  2375. this.shapes.forEach(setItemControlPointsVisibility);
  2376. this.labels.forEach(setItemControlPointsVisibility);
  2377. };
  2378. /**
  2379. * Destroy the annotation. This function does not touch the chart
  2380. * that the annotation belongs to (all annotations are kept in
  2381. * the chart.annotations array) - it is recommended to use
  2382. * {@link Highcharts.Chart#removeAnnotation} instead.
  2383. * @private
  2384. */
  2385. Annotation.prototype.destroy = function () {
  2386. var chart = this.chart, destroyItem = function (item) {
  2387. item.destroy();
  2388. };
  2389. this.labels.forEach(destroyItem);
  2390. this.shapes.forEach(destroyItem);
  2391. this.clipXAxis = null;
  2392. this.clipYAxis = null;
  2393. erase(chart.labelCollectors, this.labelCollector);
  2394. EventEmitterMixin.destroy.call(this);
  2395. ControllableMixin.destroy.call(this);
  2396. destroyObjectProperties(this, chart);
  2397. };
  2398. /**
  2399. * See {@link Highcharts.Chart#removeAnnotation}.
  2400. * @private
  2401. */
  2402. Annotation.prototype.remove = function () {
  2403. // Let chart.update() remove annoations on demand
  2404. return this.chart.removeAnnotation(this);
  2405. };
  2406. /**
  2407. * Updates an annotation.
  2408. *
  2409. * @function Highcharts.Annotation#update
  2410. *
  2411. * @param {Partial<Highcharts.AnnotationsOptions>} userOptions
  2412. * New user options for the annotation.
  2413. *
  2414. * @return {void}
  2415. */
  2416. Annotation.prototype.update = function (userOptions, redraw) {
  2417. var chart = this.chart, labelsAndShapes = this.getLabelsAndShapesOptions(this.userOptions, userOptions), userOptionsIndex = chart.annotations.indexOf(this), options = merge(true, this.userOptions, userOptions);
  2418. options.labels = labelsAndShapes.labels;
  2419. options.shapes = labelsAndShapes.shapes;
  2420. this.destroy();
  2421. this.constructor(chart, options);
  2422. // Update options in chart options, used in exporting (#9767):
  2423. chart.options.annotations[userOptionsIndex] = options;
  2424. this.isUpdating = true;
  2425. if (pick(redraw, true)) {
  2426. chart.redraw();
  2427. }
  2428. fireEvent(this, 'afterUpdate');
  2429. this.isUpdating = false;
  2430. };
  2431. /* *************************************************************
  2432. * ITEM SECTION
  2433. * Contains methods for handling a single item in an annotation
  2434. **************************************************************** */
  2435. /**
  2436. * Initialisation of a single shape
  2437. * @private
  2438. * @param {Object} shapeOptions - a confg object for a single shape
  2439. */
  2440. Annotation.prototype.initShape = function (shapeOptions, index) {
  2441. var options = merge(this.options.shapeOptions, {
  2442. controlPointOptions: this.options.controlPointOptions
  2443. }, shapeOptions), shape = new Annotation.shapesMap[options.type](this, options, index);
  2444. shape.itemType = 'shape';
  2445. this.shapes.push(shape);
  2446. return shape;
  2447. };
  2448. /**
  2449. * Initialisation of a single label
  2450. * @private
  2451. */
  2452. Annotation.prototype.initLabel = function (labelOptions, index) {
  2453. var options = merge(this.options.labelOptions, {
  2454. controlPointOptions: this.options.controlPointOptions
  2455. }, labelOptions), label = new ControllableLabel(this, options, index);
  2456. label.itemType = 'label';
  2457. this.labels.push(label);
  2458. return label;
  2459. };
  2460. /**
  2461. * Redraw a single item.
  2462. * @private
  2463. * @param {Annotation.Label|Annotation.Shape} item
  2464. * @param {boolean} [animation]
  2465. */
  2466. Annotation.prototype.redrawItem = function (item, animation) {
  2467. item.linkPoints();
  2468. if (!item.shouldBeDrawn()) {
  2469. this.destroyItem(item);
  2470. }
  2471. else {
  2472. if (!item.graphic) {
  2473. this.renderItem(item);
  2474. }
  2475. item.redraw(pick(animation, true) && item.graphic.placed);
  2476. if (item.points.length) {
  2477. this.adjustVisibility(item);
  2478. }
  2479. }
  2480. };
  2481. /**
  2482. * Hide or show annotaiton attached to points.
  2483. * @private
  2484. * @param {Annotation.Label|Annotation.Shape} item
  2485. */
  2486. Annotation.prototype.adjustVisibility = function (item) {
  2487. var hasVisiblePoints = false, label = item.graphic;
  2488. item.points.forEach(function (point) {
  2489. if (point.series.visible !== false &&
  2490. point.visible !== false) {
  2491. hasVisiblePoints = true;
  2492. }
  2493. });
  2494. if (!hasVisiblePoints) {
  2495. label.hide();
  2496. }
  2497. else if (label.visibility === 'hidden') {
  2498. label.show();
  2499. }
  2500. };
  2501. /**
  2502. * Destroy a single item.
  2503. * @private
  2504. * @param {Annotation.Label|Annotation.Shape} item
  2505. */
  2506. Annotation.prototype.destroyItem = function (item) {
  2507. // erase from shapes or labels array
  2508. erase(this[item.itemType + 's'], item);
  2509. item.destroy();
  2510. };
  2511. /**
  2512. * @private
  2513. */
  2514. Annotation.prototype.renderItem = function (item) {
  2515. item.render(item.itemType === 'label' ?
  2516. this.labelsGroup :
  2517. this.shapesGroup);
  2518. };
  2519. /**
  2520. * @private
  2521. */
  2522. Annotation.ControlPoint = ControlPoint;
  2523. /**
  2524. * @private
  2525. */
  2526. Annotation.MockPoint = MockPoint;
  2527. /**
  2528. * An object uses for mapping between a shape type and a constructor.
  2529. * To add a new shape type extend this object with type name as a key
  2530. * and a constructor as its value.
  2531. */
  2532. Annotation.shapesMap = {
  2533. 'rect': ControllableRect,
  2534. 'circle': ControllableCircle,
  2535. 'path': ControllablePath,
  2536. 'image': ControllableImage
  2537. };
  2538. /**
  2539. * @private
  2540. */
  2541. Annotation.types = {};
  2542. return Annotation;
  2543. }());
  2544. merge(true, Annotation.prototype, ControllableMixin, EventEmitterMixin,
  2545. // restore original Annotation implementation after mixin overwrite
  2546. merge(Annotation.prototype,
  2547. /** @lends Highcharts.Annotation# */
  2548. {
  2549. /**
  2550. * List of events for `annotation.options.events` that should not be
  2551. * added to `annotation.graphic` but to the `annotation`.
  2552. *
  2553. * @private
  2554. * @type {Array<string>}
  2555. */
  2556. nonDOMEvents: ['add', 'afterUpdate', 'drag', 'remove'],
  2557. /**
  2558. * A basic type of an annotation. It allows to add custom labels
  2559. * or shapes. The items can be tied to points, axis coordinates
  2560. * or chart pixel coordinates.
  2561. *
  2562. * @sample highcharts/annotations/basic/
  2563. * Basic annotations
  2564. * @sample highcharts/demo/annotations/
  2565. * Advanced annotations
  2566. * @sample highcharts/css/annotations
  2567. * Styled mode
  2568. * @sample highcharts/annotations-advanced/controllable
  2569. * Controllable items
  2570. * @sample {highstock} stock/annotations/fibonacci-retracements
  2571. * Custom annotation, Fibonacci retracement
  2572. *
  2573. * @type {Array<*>}
  2574. * @since 6.0.0
  2575. * @requires modules/annotations
  2576. * @optionparent annotations
  2577. *
  2578. * @private
  2579. */
  2580. defaultOptions: {
  2581. /**
  2582. * Sets an ID for an annotation. Can be user later when
  2583. * removing an annotation in [Chart#removeAnnotation(id)](
  2584. * /class-reference/Highcharts.Chart#removeAnnotation) method.
  2585. *
  2586. * @type {number|string}
  2587. * @apioption annotations.id
  2588. */
  2589. /**
  2590. * Whether the annotation is visible.
  2591. *
  2592. * @sample highcharts/annotations/visible/
  2593. * Set annotation visibility
  2594. */
  2595. visible: true,
  2596. /**
  2597. * Allow an annotation to be draggable by a user. Possible
  2598. * values are `'x'`, `'xy'`, `'y'` and `''` (disabled).
  2599. *
  2600. * @sample highcharts/annotations/draggable/
  2601. * Annotations draggable: 'xy'
  2602. *
  2603. * @type {Highcharts.AnnotationDraggableValue}
  2604. */
  2605. draggable: 'xy',
  2606. /**
  2607. * Options for annotation's labels. Each label inherits options
  2608. * from the labelOptions object. An option from the labelOptions
  2609. * can be overwritten by config for a specific label.
  2610. *
  2611. * @requires modules/annotations
  2612. */
  2613. labelOptions: {
  2614. /**
  2615. * The alignment of the annotation's label. If right,
  2616. * the right side of the label should be touching the point.
  2617. *
  2618. * @sample highcharts/annotations/label-position/
  2619. * Set labels position
  2620. *
  2621. * @type {Highcharts.AlignValue}
  2622. */
  2623. align: 'center',
  2624. /**
  2625. * Whether to allow the annotation's labels to overlap.
  2626. * To make the labels less sensitive for overlapping,
  2627. * the can be set to 0.
  2628. *
  2629. * @sample highcharts/annotations/tooltip-like/
  2630. * Hide overlapping labels
  2631. */
  2632. allowOverlap: false,
  2633. /**
  2634. * The background color or gradient for the annotation's
  2635. * label.
  2636. *
  2637. * @sample highcharts/annotations/label-presentation/
  2638. * Set labels graphic options
  2639. *
  2640. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  2641. */
  2642. backgroundColor: 'rgba(0, 0, 0, 0.75)',
  2643. /**
  2644. * The border color for the annotation's label.
  2645. *
  2646. * @sample highcharts/annotations/label-presentation/
  2647. * Set labels graphic options
  2648. *
  2649. * @type {Highcharts.ColorString}
  2650. */
  2651. borderColor: 'black',
  2652. /**
  2653. * The border radius in pixels for the annotaiton's label.
  2654. *
  2655. * @sample highcharts/annotations/label-presentation/
  2656. * Set labels graphic options
  2657. */
  2658. borderRadius: 3,
  2659. /**
  2660. * The border width in pixels for the annotation's label
  2661. *
  2662. * @sample highcharts/annotations/label-presentation/
  2663. * Set labels graphic options
  2664. */
  2665. borderWidth: 1,
  2666. /**
  2667. * A class name for styling by CSS.
  2668. *
  2669. * @sample highcharts/css/annotations
  2670. * Styled mode annotations
  2671. *
  2672. * @since 6.0.5
  2673. */
  2674. className: '',
  2675. /**
  2676. * Whether to hide the annotation's label
  2677. * that is outside the plot area.
  2678. *
  2679. * @sample highcharts/annotations/label-crop-overflow/
  2680. * Crop or justify labels
  2681. */
  2682. crop: false,
  2683. /**
  2684. * The label's pixel distance from the point.
  2685. *
  2686. * @sample highcharts/annotations/label-position/
  2687. * Set labels position
  2688. *
  2689. * @type {number}
  2690. * @apioption annotations.labelOptions.distance
  2691. */
  2692. /**
  2693. * A
  2694. * [format](https://www.highcharts.com/docs/chart-concepts/labels-and-string-formatting)
  2695. * string for the data label.
  2696. *
  2697. * @see [plotOptions.series.dataLabels.format](plotOptions.series.dataLabels.format.html)
  2698. *
  2699. * @sample highcharts/annotations/label-text/
  2700. * Set labels text
  2701. *
  2702. * @type {string}
  2703. * @apioption annotations.labelOptions.format
  2704. */
  2705. /**
  2706. * Alias for the format option.
  2707. *
  2708. * @see [format](annotations.labelOptions.format.html)
  2709. *
  2710. * @sample highcharts/annotations/label-text/
  2711. * Set labels text
  2712. *
  2713. * @type {string}
  2714. * @apioption annotations.labelOptions.text
  2715. */
  2716. /**
  2717. * Callback JavaScript function to format the annotation's
  2718. * label. Note that if a `format` or `text` are defined,
  2719. * the format or text take precedence and the formatter is
  2720. * ignored. `This` refers to a point object.
  2721. *
  2722. * @sample highcharts/annotations/label-text/
  2723. * Set labels text
  2724. *
  2725. * @type {Highcharts.FormatterCallbackFunction<Highcharts.Point>}
  2726. * @default function () { return defined(this.y) ? this.y : 'Annotation label'; }
  2727. */
  2728. formatter: function () {
  2729. return defined(this.y) ? this.y : 'Annotation label';
  2730. },
  2731. /**
  2732. * How to handle the annotation's label that flow outside
  2733. * the plot area. The justify option aligns the label inside
  2734. * the plot area.
  2735. *
  2736. * @sample highcharts/annotations/label-crop-overflow/
  2737. * Crop or justify labels
  2738. *
  2739. * @validvalue ["allow", "justify"]
  2740. */
  2741. overflow: 'justify',
  2742. /**
  2743. * When either the borderWidth or the backgroundColor is
  2744. * set, this is the padding within the box.
  2745. *
  2746. * @sample highcharts/annotations/label-presentation/
  2747. * Set labels graphic options
  2748. */
  2749. padding: 5,
  2750. /**
  2751. * The shadow of the box. The shadow can be an object
  2752. * configuration containing `color`, `offsetX`, `offsetY`,
  2753. * `opacity` and `width`.
  2754. *
  2755. * @sample highcharts/annotations/label-presentation/
  2756. * Set labels graphic options
  2757. *
  2758. * @type {boolean|Highcharts.ShadowOptionsObject}
  2759. */
  2760. shadow: false,
  2761. /**
  2762. * The name of a symbol to use for the border around the
  2763. * label. Symbols are predefined functions on the Renderer
  2764. * object.
  2765. *
  2766. * @sample highcharts/annotations/shapes/
  2767. * Available shapes for labels
  2768. */
  2769. shape: 'callout',
  2770. /**
  2771. * Styles for the annotation's label.
  2772. *
  2773. * @see [plotOptions.series.dataLabels.style](plotOptions.series.dataLabels.style.html)
  2774. *
  2775. * @sample highcharts/annotations/label-presentation/
  2776. * Set labels graphic options
  2777. *
  2778. * @type {Highcharts.CSSObject}
  2779. */
  2780. style: {
  2781. /** @ignore */
  2782. fontSize: '11px',
  2783. /** @ignore */
  2784. fontWeight: 'normal',
  2785. /** @ignore */
  2786. color: 'contrast'
  2787. },
  2788. /**
  2789. * Whether to [use HTML](https://www.highcharts.com/docs/chart-concepts/labels-and-string-formatting#html)
  2790. * to render the annotation's label.
  2791. */
  2792. useHTML: false,
  2793. /**
  2794. * The vertical alignment of the annotation's label.
  2795. *
  2796. * @sample highcharts/annotations/label-position/
  2797. * Set labels position
  2798. *
  2799. * @type {Highcharts.VerticalAlignValue}
  2800. */
  2801. verticalAlign: 'bottom',
  2802. /**
  2803. * The x position offset of the label relative to the point.
  2804. * Note that if a `distance` is defined, the distance takes
  2805. * precedence over `x` and `y` options.
  2806. *
  2807. * @sample highcharts/annotations/label-position/
  2808. * Set labels position
  2809. */
  2810. x: 0,
  2811. /**
  2812. * The y position offset of the label relative to the point.
  2813. * Note that if a `distance` is defined, the distance takes
  2814. * precedence over `x` and `y` options.
  2815. *
  2816. * @sample highcharts/annotations/label-position/
  2817. * Set labels position
  2818. */
  2819. y: -16
  2820. },
  2821. /**
  2822. * An array of labels for the annotation. For options that apply
  2823. * to multiple labels, they can be added to the
  2824. * [labelOptions](annotations.labelOptions.html).
  2825. *
  2826. * @type {Array<*>}
  2827. * @extends annotations.labelOptions
  2828. * @apioption annotations.labels
  2829. */
  2830. /**
  2831. * This option defines the point to which the label will be
  2832. * connected. It can be either the point which exists in the
  2833. * series - it is referenced by the point's id - or a new point
  2834. * with defined x, y properties and optionally axes.
  2835. *
  2836. * @sample highcharts/annotations/mock-point/
  2837. * Attach annotation to a mock point
  2838. *
  2839. * @declare Highcharts.AnnotationMockPointOptionsObject
  2840. * @type {string|*}
  2841. * @requires modules/annotations
  2842. * @apioption annotations.labels.point
  2843. */
  2844. /**
  2845. * The x position of the point. Units can be either in axis
  2846. * or chart pixel coordinates.
  2847. *
  2848. * @type {number}
  2849. * @apioption annotations.labels.point.x
  2850. */
  2851. /**
  2852. * The y position of the point. Units can be either in axis
  2853. * or chart pixel coordinates.
  2854. *
  2855. * @type {number}
  2856. * @apioption annotations.labels.point.y
  2857. */
  2858. /**
  2859. * This number defines which xAxis the point is connected to.
  2860. * It refers to either the axis id or the index of the axis in
  2861. * the xAxis array. If the option is not configured or the axis
  2862. * is not found the point's x coordinate refers to the chart
  2863. * pixels.
  2864. *
  2865. * @type {number|string|null}
  2866. * @apioption annotations.labels.point.xAxis
  2867. */
  2868. /**
  2869. * This number defines which yAxis the point is connected to.
  2870. * It refers to either the axis id or the index of the axis in
  2871. * the yAxis array. If the option is not configured or the axis
  2872. * is not found the point's y coordinate refers to the chart
  2873. * pixels.
  2874. *
  2875. * @type {number|string|null}
  2876. * @apioption annotations.labels.point.yAxis
  2877. */
  2878. /**
  2879. * An array of shapes for the annotation. For options that apply
  2880. * to multiple shapes, then can be added to the
  2881. * [shapeOptions](annotations.shapeOptions.html).
  2882. *
  2883. * @type {Array<*>}
  2884. * @extends annotations.shapeOptions
  2885. * @apioption annotations.shapes
  2886. */
  2887. /**
  2888. * This option defines the point to which the shape will be
  2889. * connected. It can be either the point which exists in the
  2890. * series - it is referenced by the point's id - or a new point
  2891. * with defined x, y properties and optionally axes.
  2892. *
  2893. * @declare Highcharts.AnnotationMockPointOptionsObject
  2894. * @type {string|Highcharts.AnnotationMockPointOptionsObject}
  2895. * @extends annotations.labels.point
  2896. * @apioption annotations.shapes.point
  2897. */
  2898. /**
  2899. * An array of points for the shape. This option is available
  2900. * for shapes which can use multiple points such as path. A
  2901. * point can be either a point object or a point's id.
  2902. *
  2903. * @see [annotations.shapes.point](annotations.shapes.point.html)
  2904. *
  2905. * @declare Highcharts.AnnotationMockPointOptionsObject
  2906. * @type {Array<string|*>}
  2907. * @extends annotations.labels.point
  2908. * @apioption annotations.shapes.points
  2909. */
  2910. /**
  2911. * The URL for an image to use as the annotation shape. Note,
  2912. * type has to be set to `'image'`.
  2913. *
  2914. * @see [annotations.shapes.type](annotations.shapes.type)
  2915. * @sample highcharts/annotations/shape-src/
  2916. * Define a marker image url for annotations
  2917. *
  2918. * @type {string}
  2919. * @apioption annotations.shapes.src
  2920. */
  2921. /**
  2922. * Id of the marker which will be drawn at the final vertex of
  2923. * the path. Custom markers can be defined in defs property.
  2924. *
  2925. * @see [defs.markers](defs.markers.html)
  2926. *
  2927. * @sample highcharts/annotations/custom-markers/
  2928. * Define a custom marker for annotations
  2929. *
  2930. * @type {string}
  2931. * @apioption annotations.shapes.markerEnd
  2932. */
  2933. /**
  2934. * Id of the marker which will be drawn at the first vertex of
  2935. * the path. Custom markers can be defined in defs property.
  2936. *
  2937. * @see [defs.markers](defs.markers.html)
  2938. *
  2939. * @sample {highcharts} highcharts/annotations/custom-markers/
  2940. * Define a custom marker for annotations
  2941. *
  2942. * @type {string}
  2943. * @apioption annotations.shapes.markerStart
  2944. */
  2945. /**
  2946. * Options for annotation's shapes. Each shape inherits options
  2947. * from the shapeOptions object. An option from the shapeOptions
  2948. * can be overwritten by config for a specific shape.
  2949. *
  2950. * @requires modules/annotations
  2951. */
  2952. shapeOptions: {
  2953. /**
  2954. * The width of the shape.
  2955. *
  2956. * @sample highcharts/annotations/shape/
  2957. * Basic shape annotation
  2958. *
  2959. * @type {number}
  2960. * @apioption annotations.shapeOptions.width
  2961. **/
  2962. /**
  2963. * The height of the shape.
  2964. *
  2965. * @sample highcharts/annotations/shape/
  2966. * Basic shape annotation
  2967. *
  2968. * @type {number}
  2969. * @apioption annotations.shapeOptions.height
  2970. */
  2971. /**
  2972. * The type of the shape, e.g. circle or rectangle.
  2973. *
  2974. * @sample highcharts/annotations/shape/
  2975. * Basic shape annotation
  2976. *
  2977. * @type {string}
  2978. * @default 'rect'
  2979. * @apioption annotations.shapeOptions.type
  2980. */
  2981. /**
  2982. * The URL for an image to use as the annotation shape.
  2983. * Note, type has to be set to `'image'`.
  2984. *
  2985. * @see [annotations.shapeOptions.type](annotations.shapeOptions.type)
  2986. * @sample highcharts/annotations/shape-src/
  2987. * Define a marker image url for annotations
  2988. *
  2989. * @type {string}
  2990. * @apioption annotations.shapeOptions.src
  2991. */
  2992. /**
  2993. * Name of the dash style to use for the shape's stroke.
  2994. *
  2995. * @sample {highcharts} highcharts/plotoptions/series-dashstyle-all/
  2996. * Possible values demonstrated
  2997. *
  2998. * @type {Highcharts.DashStyleValue}
  2999. * @apioption annotations.shapeOptions.dashStyle
  3000. */
  3001. /**
  3002. * The color of the shape's stroke.
  3003. *
  3004. * @sample highcharts/annotations/shape/
  3005. * Basic shape annotation
  3006. *
  3007. * @type {Highcharts.ColorString}
  3008. */
  3009. stroke: 'rgba(0, 0, 0, 0.75)',
  3010. /**
  3011. * The pixel stroke width of the shape.
  3012. *
  3013. * @sample highcharts/annotations/shape/
  3014. * Basic shape annotation
  3015. */
  3016. strokeWidth: 1,
  3017. /**
  3018. * The color of the shape's fill.
  3019. *
  3020. * @sample highcharts/annotations/shape/
  3021. * Basic shape annotation
  3022. *
  3023. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  3024. */
  3025. fill: 'rgba(0, 0, 0, 0.75)',
  3026. /**
  3027. * The radius of the shape.
  3028. *
  3029. * @sample highcharts/annotations/shape/
  3030. * Basic shape annotation
  3031. */
  3032. r: 0,
  3033. /**
  3034. * Defines additional snapping area around an annotation
  3035. * making this annotation to focus. Defined in pixels.
  3036. */
  3037. snap: 2
  3038. },
  3039. /**
  3040. * Options for annotation's control points. Each control point
  3041. * inherits options from controlPointOptions object.
  3042. * Options from the controlPointOptions can be overwritten
  3043. * by options in a specific control point.
  3044. *
  3045. * @declare Highcharts.AnnotationControlPointOptionsObject
  3046. * @requires modules/annotations
  3047. * @apioption annotations.controlPointOptions
  3048. */
  3049. controlPointOptions: {
  3050. /**
  3051. * @type {Highcharts.AnnotationControlPointPositionerFunction}
  3052. * @apioption annotations.controlPointOptions.positioner
  3053. */
  3054. symbol: 'circle',
  3055. width: 10,
  3056. height: 10,
  3057. style: {
  3058. stroke: 'black',
  3059. 'stroke-width': 2,
  3060. fill: 'white'
  3061. },
  3062. visible: false,
  3063. events: {}
  3064. },
  3065. /**
  3066. * Event callback when annotation is added to the chart.
  3067. *
  3068. * @type {Highcharts.EventCallbackFunction<Highcharts.Annotation>}
  3069. * @since 7.1.0
  3070. * @apioption annotations.events.add
  3071. */
  3072. /**
  3073. * Event callback when annotation is updated (e.g. drag and
  3074. * droppped or resized by control points).
  3075. *
  3076. * @type {Highcharts.EventCallbackFunction<Highcharts.Annotation>}
  3077. * @since 7.1.0
  3078. * @apioption annotations.events.afterUpdate
  3079. */
  3080. /**
  3081. * Event callback when annotation is removed from the chart.
  3082. *
  3083. * @type {Highcharts.EventCallbackFunction<Highcharts.Annotation>}
  3084. * @since 7.1.0
  3085. * @apioption annotations.events.remove
  3086. */
  3087. /**
  3088. * Events available in annotations.
  3089. *
  3090. * @requires modules/annotations
  3091. */
  3092. events: {},
  3093. /**
  3094. * The Z index of the annotation.
  3095. */
  3096. zIndex: 6
  3097. }
  3098. }));
  3099. H.extendAnnotation = function (Constructor, BaseConstructor, prototype, defaultOptions) {
  3100. BaseConstructor = BaseConstructor || Annotation;
  3101. merge(true, Constructor.prototype, BaseConstructor.prototype, prototype);
  3102. Constructor.prototype.defaultOptions = merge(Constructor.prototype.defaultOptions, defaultOptions || {});
  3103. };
  3104. /* *********************************************************************
  3105. *
  3106. * EXTENDING CHART PROTOTYPE
  3107. *
  3108. ******************************************************************** */
  3109. extend(chartProto, /** @lends Highcharts.Chart# */ {
  3110. initAnnotation: function (userOptions) {
  3111. var Constructor = Annotation.types[userOptions.type] || Annotation, annotation = new Constructor(this, userOptions);
  3112. this.annotations.push(annotation);
  3113. return annotation;
  3114. },
  3115. /**
  3116. * Add an annotation to the chart after render time.
  3117. *
  3118. * @param {Highcharts.AnnotationsOptions} options
  3119. * The annotation options for the new, detailed annotation.
  3120. * @param {boolean} [redraw]
  3121. *
  3122. * @return {Highcharts.Annotation} - The newly generated annotation.
  3123. */
  3124. addAnnotation: function (userOptions, redraw) {
  3125. var annotation = this.initAnnotation(userOptions);
  3126. this.options.annotations.push(annotation.options);
  3127. if (pick(redraw, true)) {
  3128. annotation.redraw();
  3129. }
  3130. return annotation;
  3131. },
  3132. /**
  3133. * Remove an annotation from the chart.
  3134. *
  3135. * @param {number|string|Highcharts.Annotation} idOrAnnotation
  3136. * The annotation's id or direct annotation object.
  3137. */
  3138. removeAnnotation: function (idOrAnnotation) {
  3139. var annotations = this.annotations, annotation = idOrAnnotation.coll === 'annotations' ?
  3140. idOrAnnotation :
  3141. find(annotations, function (annotation) {
  3142. return annotation.options.id === idOrAnnotation;
  3143. });
  3144. if (annotation) {
  3145. fireEvent(annotation, 'remove');
  3146. erase(this.options.annotations, annotation.options);
  3147. erase(annotations, annotation);
  3148. annotation.destroy();
  3149. }
  3150. },
  3151. drawAnnotations: function () {
  3152. this.plotBoxClip.attr(this.plotBox);
  3153. this.annotations.forEach(function (annotation) {
  3154. annotation.redraw();
  3155. });
  3156. }
  3157. });
  3158. // Let chart.update() update annotations
  3159. chartProto.collectionsWithUpdate.push('annotations');
  3160. // Let chart.update() create annoations on demand
  3161. chartProto.collectionsWithInit.annotations = [chartProto.addAnnotation];
  3162. chartProto.callbacks.push(function (chart) {
  3163. chart.annotations = [];
  3164. if (!chart.options.annotations) {
  3165. chart.options.annotations = [];
  3166. }
  3167. chart.plotBoxClip = this.renderer.clipRect(this.plotBox);
  3168. chart.controlPointsGroup = chart.renderer
  3169. .g('control-points')
  3170. .attr({ zIndex: 99 })
  3171. .clip(chart.plotBoxClip)
  3172. .add();
  3173. chart.options.annotations.forEach(function (annotationOptions, i) {
  3174. var annotation = chart.initAnnotation(annotationOptions);
  3175. chart.options.annotations[i] = annotation.options;
  3176. });
  3177. chart.drawAnnotations();
  3178. addEvent(chart, 'redraw', chart.drawAnnotations);
  3179. addEvent(chart, 'destroy', function () {
  3180. chart.plotBoxClip.destroy();
  3181. chart.controlPointsGroup.destroy();
  3182. });
  3183. });
  3184. wrap(Pointer.prototype, 'onContainerMouseDown', function (proceed) {
  3185. if (!this.chart.hasDraggedAnnotation) {
  3186. proceed.apply(this, Array.prototype.slice.call(arguments, 1));
  3187. }
  3188. });
  3189. H.Annotation = Annotation;
  3190. return Annotation;
  3191. });
  3192. _registerModule(_modules, 'mixins/navigation.js', [], function () {
  3193. /**
  3194. *
  3195. * (c) 2010-2018 Paweł Fus
  3196. *
  3197. * License: www.highcharts.com/license
  3198. *
  3199. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  3200. *
  3201. * */
  3202. var chartNavigation = {
  3203. /**
  3204. * Initializes `chart.navigation` object which delegates `update()` methods
  3205. * to all other common classes (used in exporting and navigationBindings).
  3206. *
  3207. * @private
  3208. * @param {Highcharts.Chart} chart
  3209. * The chart instance.
  3210. * @return {void}
  3211. */
  3212. initUpdate: function (chart) {
  3213. if (!chart.navigation) {
  3214. chart.navigation = {
  3215. updates: [],
  3216. update: function (options, redraw) {
  3217. this.updates.forEach(function (updateConfig) {
  3218. updateConfig.update.call(updateConfig.context, options, redraw);
  3219. });
  3220. }
  3221. };
  3222. }
  3223. },
  3224. /**
  3225. * Registers an `update()` method in the `chart.navigation` object.
  3226. *
  3227. * @private
  3228. * @param {Highcharts.ChartNavigationUpdateFunction} update
  3229. * The `update()` method that will be called in `chart.update()`.
  3230. * @param {Highcharts.Chart} chart
  3231. * The chart instance. `update()` will use that as a context
  3232. * (`this`).
  3233. * @return {void}
  3234. */
  3235. addUpdate: function (update, chart) {
  3236. if (!chart.navigation) {
  3237. this.initUpdate(chart);
  3238. }
  3239. chart.navigation.updates.push({
  3240. update: update,
  3241. context: chart
  3242. });
  3243. }
  3244. };
  3245. return chartNavigation;
  3246. });
  3247. _registerModule(_modules, 'annotations/navigationBindings.js', [_modules['annotations/annotations.src.js'], _modules['mixins/navigation.js'], _modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (Annotation, chartNavigationMixin, H, U) {
  3248. /* *
  3249. *
  3250. * (c) 2009-2017 Highsoft, Black Label
  3251. *
  3252. * License: www.highcharts.com/license
  3253. *
  3254. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  3255. *
  3256. * */
  3257. var addEvent = U.addEvent, attr = U.attr, extend = U.extend, format = U.format, fireEvent = U.fireEvent, isArray = U.isArray, isFunction = U.isFunction, isNumber = U.isNumber, isObject = U.isObject, merge = U.merge, objectEach = U.objectEach, pick = U.pick, setOptions = U.setOptions;
  3258. /**
  3259. * A config object for navigation bindings in annotations.
  3260. *
  3261. * @interface Highcharts.NavigationBindingsOptionsObject
  3262. */ /**
  3263. * ClassName of the element for a binding.
  3264. * @name Highcharts.NavigationBindingsOptionsObject#className
  3265. * @type {string|undefined}
  3266. */ /**
  3267. * Last event to be fired after last step event.
  3268. * @name Highcharts.NavigationBindingsOptionsObject#end
  3269. * @type {Function|undefined}
  3270. */ /**
  3271. * Initial event, fired on a button click.
  3272. * @name Highcharts.NavigationBindingsOptionsObject#init
  3273. * @type {Function|undefined}
  3274. */ /**
  3275. * Event fired on first click on a chart.
  3276. * @name Highcharts.NavigationBindingsOptionsObject#start
  3277. * @type {Function|undefined}
  3278. */ /**
  3279. * Last event to be fired after last step event. Array of step events to be
  3280. * called sequentially after each user click.
  3281. * @name Highcharts.NavigationBindingsOptionsObject#steps
  3282. * @type {Array<Function>|undefined}
  3283. */
  3284. var doc = H.doc, win = H.win, PREFIX = 'highcharts-';
  3285. /* eslint-disable no-invalid-this, valid-jsdoc */
  3286. /**
  3287. * IE 9-11 polyfill for Element.closest():
  3288. * @private
  3289. */
  3290. function closestPolyfill(el, s) {
  3291. var ElementProto = win.Element.prototype, elementMatches = ElementProto.matches ||
  3292. ElementProto.msMatchesSelector ||
  3293. ElementProto.webkitMatchesSelector, ret = null;
  3294. if (ElementProto.closest) {
  3295. ret = ElementProto.closest.call(el, s);
  3296. }
  3297. else {
  3298. do {
  3299. if (elementMatches.call(el, s)) {
  3300. return el;
  3301. }
  3302. el = el.parentElement || el.parentNode;
  3303. } while (el !== null && el.nodeType === 1);
  3304. }
  3305. return ret;
  3306. }
  3307. /**
  3308. * @private
  3309. * @interface bindingsUtils
  3310. */
  3311. var bindingsUtils = {
  3312. /**
  3313. * Update size of background (rect) in some annotations: Measure, Simple
  3314. * Rect.
  3315. *
  3316. * @private
  3317. * @function Highcharts.NavigationBindingsUtilsObject.updateRectSize
  3318. *
  3319. * @param {Highcharts.PointerEventObject} event
  3320. * Normalized browser event
  3321. *
  3322. * @param {Highcharts.Annotation} annotation
  3323. * Annotation to be updated
  3324. */
  3325. updateRectSize: function (event, annotation) {
  3326. var chart = annotation.chart, options = annotation.options.typeOptions, coords = chart.pointer.getCoordinates(event), width = coords.xAxis[0].value - options.point.x, height = options.point.y - coords.yAxis[0].value;
  3327. annotation.update({
  3328. typeOptions: {
  3329. background: {
  3330. width: chart.inverted ? height : width,
  3331. height: chart.inverted ? width : height
  3332. }
  3333. }
  3334. });
  3335. },
  3336. /**
  3337. * Get field type according to value
  3338. *
  3339. * @private
  3340. * @function Highcharts.NavigationBindingsUtilsObject.getFieldType
  3341. *
  3342. * @param {'boolean'|'number'|'string'} value
  3343. * Atomic type (one of: string, number, boolean)
  3344. *
  3345. * @return {'checkbox'|'number'|'text'}
  3346. * Field type (one of: text, number, checkbox)
  3347. */
  3348. getFieldType: function (value) {
  3349. return {
  3350. 'string': 'text',
  3351. 'number': 'number',
  3352. 'boolean': 'checkbox'
  3353. }[typeof value];
  3354. }
  3355. };
  3356. /**
  3357. * @private
  3358. */
  3359. var NavigationBindings = /** @class */ (function () {
  3360. /* *
  3361. *
  3362. * Constructors
  3363. *
  3364. * */
  3365. function NavigationBindings(chart, options) {
  3366. this.boundClassNames = void 0;
  3367. this.selectedButton = void 0;
  3368. this.chart = chart;
  3369. this.options = options;
  3370. this.eventsToUnbind = [];
  3371. this.container = doc.getElementsByClassName(this.options.bindingsClassName || '');
  3372. }
  3373. // Private properties added by bindings:
  3374. // Active (selected) annotation that is editted through popup/forms
  3375. // activeAnnotation: Annotation
  3376. // Holder for current step, used on mouse move to update bound object
  3377. // mouseMoveEvent: function () {}
  3378. // Next event in `step` array to be called on chart's click
  3379. // nextEvent: function () {}
  3380. // Index in the `step` array of the current event
  3381. // stepIndex: 0
  3382. // Flag to determine if current binding has steps
  3383. // steps: true|false
  3384. // Bindings holder for all events
  3385. // selectedButton: {}
  3386. // Holder for user options, returned from `start` event, and passed on to
  3387. // `step`'s' and `end`.
  3388. // currentUserDetails: {}
  3389. /* *
  3390. *
  3391. * Functions
  3392. *
  3393. * */
  3394. /**
  3395. * Initi all events conencted to NavigationBindings.
  3396. *
  3397. * @private
  3398. * @function Highcharts.NavigationBindings#initEvents
  3399. */
  3400. NavigationBindings.prototype.initEvents = function () {
  3401. var navigation = this, chart = navigation.chart, bindingsContainer = navigation.container, options = navigation.options;
  3402. // Shorthand object for getting events for buttons:
  3403. navigation.boundClassNames = {};
  3404. objectEach((options.bindings || {}), function (value) {
  3405. navigation.boundClassNames[value.className] = value;
  3406. });
  3407. // Handle multiple containers with the same class names:
  3408. [].forEach.call(bindingsContainer, function (subContainer) {
  3409. navigation.eventsToUnbind.push(addEvent(subContainer, 'click', function (event) {
  3410. var bindings = navigation.getButtonEvents(subContainer, event);
  3411. if (bindings) {
  3412. navigation.bindingsButtonClick(bindings.button, bindings.events, event);
  3413. }
  3414. }));
  3415. });
  3416. objectEach(options.events || {}, function (callback, eventName) {
  3417. if (isFunction(callback)) {
  3418. navigation.eventsToUnbind.push(addEvent(navigation, eventName, callback));
  3419. }
  3420. });
  3421. navigation.eventsToUnbind.push(addEvent(chart.container, 'click', function (e) {
  3422. if (!chart.cancelClick &&
  3423. chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) {
  3424. navigation.bindingsChartClick(this, e);
  3425. }
  3426. }));
  3427. navigation.eventsToUnbind.push(addEvent(chart.container, H.isTouchDevice ? 'touchmove' : 'mousemove', function (e) {
  3428. navigation.bindingsContainerMouseMove(this, e);
  3429. }));
  3430. };
  3431. /**
  3432. * Common chart.update() delegation, shared between bindings and exporting.
  3433. *
  3434. * @private
  3435. * @function Highcharts.NavigationBindings#initUpdate
  3436. */
  3437. NavigationBindings.prototype.initUpdate = function () {
  3438. var navigation = this;
  3439. chartNavigationMixin.addUpdate(function (options) {
  3440. navigation.update(options);
  3441. }, this.chart);
  3442. };
  3443. /**
  3444. * Hook for click on a button, method selcts/unselects buttons,
  3445. * then calls `bindings.init` callback.
  3446. *
  3447. * @private
  3448. * @function Highcharts.NavigationBindings#bindingsButtonClick
  3449. *
  3450. * @param {Highcharts.HTMLDOMElement} [button]
  3451. * Clicked button
  3452. *
  3453. * @param {object} events
  3454. * Events passed down from bindings (`init`, `start`, `step`, `end`)
  3455. *
  3456. * @param {Highcharts.PointerEventObject} clickEvent
  3457. * Browser's click event
  3458. */
  3459. NavigationBindings.prototype.bindingsButtonClick = function (button, events, clickEvent) {
  3460. var navigation = this, chart = navigation.chart;
  3461. if (navigation.selectedButtonElement) {
  3462. fireEvent(navigation, 'deselectButton', { button: navigation.selectedButtonElement });
  3463. if (navigation.nextEvent) {
  3464. // Remove in-progress annotations adders:
  3465. if (navigation.currentUserDetails &&
  3466. navigation.currentUserDetails.coll === 'annotations') {
  3467. chart.removeAnnotation(navigation.currentUserDetails);
  3468. }
  3469. navigation.mouseMoveEvent = navigation.nextEvent = false;
  3470. }
  3471. }
  3472. navigation.selectedButton = events;
  3473. navigation.selectedButtonElement = button;
  3474. fireEvent(navigation, 'selectButton', { button: button });
  3475. // Call "init" event, for example to open modal window
  3476. if (events.init) {
  3477. events.init.call(navigation, button, clickEvent);
  3478. }
  3479. if (events.start || events.steps) {
  3480. chart.renderer.boxWrapper.addClass(PREFIX + 'draw-mode');
  3481. }
  3482. };
  3483. /**
  3484. * Hook for click on a chart, first click on a chart calls `start` event,
  3485. * then on all subsequent clicks iterate over `steps` array.
  3486. * When finished, calls `end` event.
  3487. *
  3488. * @private
  3489. * @function Highcharts.NavigationBindings#bindingsChartClick
  3490. *
  3491. * @param {Highcharts.Chart} chart
  3492. * Chart that click was performed on.
  3493. *
  3494. * @param {Highcharts.PointerEventObject} clickEvent
  3495. * Browser's click event.
  3496. */
  3497. NavigationBindings.prototype.bindingsChartClick = function (chart, clickEvent) {
  3498. var navigation = this, chart = navigation.chart, selectedButton = navigation.selectedButton, svgContainer = chart.renderer.boxWrapper;
  3499. // Click outside popups, should close them and deselect the annotation
  3500. if (navigation.activeAnnotation &&
  3501. !clickEvent.activeAnnotation &&
  3502. // Element could be removed in the child action, e.g. button
  3503. clickEvent.target.parentNode &&
  3504. // TO DO: Polyfill for IE11?
  3505. !closestPolyfill(clickEvent.target, '.' + PREFIX + 'popup')) {
  3506. fireEvent(navigation, 'closePopup');
  3507. navigation.deselectAnnotation();
  3508. }
  3509. if (!selectedButton || !selectedButton.start) {
  3510. return;
  3511. }
  3512. if (!navigation.nextEvent) {
  3513. // Call init method:
  3514. navigation.currentUserDetails = selectedButton.start.call(navigation, clickEvent);
  3515. // If steps exists (e.g. Annotations), bind them:
  3516. if (selectedButton.steps) {
  3517. navigation.stepIndex = 0;
  3518. navigation.steps = true;
  3519. navigation.mouseMoveEvent = navigation.nextEvent =
  3520. selectedButton.steps[navigation.stepIndex];
  3521. }
  3522. else {
  3523. fireEvent(navigation, 'deselectButton', { button: navigation.selectedButtonElement });
  3524. svgContainer.removeClass(PREFIX + 'draw-mode');
  3525. navigation.steps = false;
  3526. navigation.selectedButton = null;
  3527. // First click is also the last one:
  3528. if (selectedButton.end) {
  3529. selectedButton.end.call(navigation, clickEvent, navigation.currentUserDetails);
  3530. }
  3531. }
  3532. }
  3533. else {
  3534. navigation.nextEvent(clickEvent, navigation.currentUserDetails);
  3535. if (navigation.steps) {
  3536. navigation.stepIndex++;
  3537. if (selectedButton.steps[navigation.stepIndex]) {
  3538. // If we have more steps, bind them one by one:
  3539. navigation.mouseMoveEvent = navigation.nextEvent =
  3540. selectedButton.steps[navigation.stepIndex];
  3541. }
  3542. else {
  3543. fireEvent(navigation, 'deselectButton', { button: navigation.selectedButtonElement });
  3544. svgContainer.removeClass(PREFIX + 'draw-mode');
  3545. // That was the last step, call end():
  3546. if (selectedButton.end) {
  3547. selectedButton.end.call(navigation, clickEvent, navigation.currentUserDetails);
  3548. }
  3549. navigation.nextEvent = false;
  3550. navigation.mouseMoveEvent = false;
  3551. navigation.selectedButton = null;
  3552. }
  3553. }
  3554. }
  3555. };
  3556. /**
  3557. * Hook for mouse move on a chart's container. It calls current step.
  3558. *
  3559. * @private
  3560. * @function Highcharts.NavigationBindings#bindingsContainerMouseMove
  3561. *
  3562. * @param {Highcharts.HTMLDOMElement} container
  3563. * Chart's container.
  3564. *
  3565. * @param {global.Event} moveEvent
  3566. * Browser's move event.
  3567. */
  3568. NavigationBindings.prototype.bindingsContainerMouseMove = function (_container, moveEvent) {
  3569. if (this.mouseMoveEvent) {
  3570. this.mouseMoveEvent(moveEvent, this.currentUserDetails);
  3571. }
  3572. };
  3573. /**
  3574. * Translate fields (e.g. `params.period` or `marker.styles.color`) to
  3575. * Highcharts options object (e.g. `{ params: { period } }`).
  3576. *
  3577. * @private
  3578. * @function Highcharts.NavigationBindings#fieldsToOptions<T>
  3579. *
  3580. * @param {Highcharts.Dictionary<string>} fields
  3581. * Fields from popup form.
  3582. *
  3583. * @param {T} config
  3584. * Default config to be modified.
  3585. *
  3586. * @return {T}
  3587. * Modified config
  3588. */
  3589. NavigationBindings.prototype.fieldsToOptions = function (fields, config) {
  3590. objectEach(fields, function (value, field) {
  3591. var parsedValue = parseFloat(value), path = field.split('.'), parent = config, pathLength = path.length - 1;
  3592. // If it's a number (not "format" options), parse it:
  3593. if (isNumber(parsedValue) &&
  3594. !value.match(/px/g) &&
  3595. !field.match(/format/g)) {
  3596. value = parsedValue;
  3597. }
  3598. // Remove empty strings or values like 0
  3599. if (value !== '' && value !== 'undefined') {
  3600. path.forEach(function (name, index) {
  3601. var nextName = pick(path[index + 1], '');
  3602. if (pathLength === index) {
  3603. // Last index, put value:
  3604. parent[name] = value;
  3605. }
  3606. else if (!parent[name]) {
  3607. // Create middle property:
  3608. parent[name] = nextName.match(/\d/g) ? [] : {};
  3609. parent = parent[name];
  3610. }
  3611. else {
  3612. // Jump into next property
  3613. parent = parent[name];
  3614. }
  3615. });
  3616. }
  3617. });
  3618. return config;
  3619. };
  3620. /**
  3621. * Shorthand method to deselect an annotation.
  3622. *
  3623. * @function Highcharts.NavigationBindings#deselectAnnotation
  3624. */
  3625. NavigationBindings.prototype.deselectAnnotation = function () {
  3626. if (this.activeAnnotation) {
  3627. this.activeAnnotation.setControlPointsVisibility(false);
  3628. this.activeAnnotation = false;
  3629. }
  3630. };
  3631. /**
  3632. * Generates API config for popup in the same format as options for
  3633. * Annotation object.
  3634. *
  3635. * @function Highcharts.NavigationBindings#annotationToFields
  3636. *
  3637. * @param {Highcharts.Annotation} annotation
  3638. * Annotations object
  3639. *
  3640. * @return {Highcharts.Dictionary<string>}
  3641. * Annotation options to be displayed in popup box
  3642. */
  3643. NavigationBindings.prototype.annotationToFields = function (annotation) {
  3644. var options = annotation.options, editables = NavigationBindings.annotationsEditable, nestedEditables = editables.nestedOptions, getFieldType = this.utils.getFieldType, type = pick(options.type, options.shapes && options.shapes[0] &&
  3645. options.shapes[0].type, options.labels && options.labels[0] &&
  3646. options.labels[0].itemType, 'label'), nonEditables = NavigationBindings.annotationsNonEditable[options.langKey] || [], visualOptions = {
  3647. langKey: options.langKey,
  3648. type: type
  3649. };
  3650. /**
  3651. * Nested options traversing. Method goes down to the options and copies
  3652. * allowed options (with values) to new object, which is last parameter:
  3653. * "parent".
  3654. *
  3655. * @private
  3656. *
  3657. * @param {*} option
  3658. * Atomic type or object/array
  3659. *
  3660. * @param {string} key
  3661. * Option name, for example "visible" or "x", "y"
  3662. *
  3663. * @param {object} parentEditables
  3664. * Editables from NavigationBindings.annotationsEditable
  3665. *
  3666. * @param {object} parent
  3667. * Where new options will be assigned
  3668. */
  3669. function traverse(option, key, parentEditables, parent) {
  3670. var nextParent;
  3671. if (parentEditables &&
  3672. nonEditables.indexOf(key) === -1 &&
  3673. ((parentEditables.indexOf &&
  3674. parentEditables.indexOf(key)) >= 0 ||
  3675. parentEditables[key] || // nested array
  3676. parentEditables === true // simple array
  3677. )) {
  3678. // Roots:
  3679. if (isArray(option)) {
  3680. parent[key] = [];
  3681. option.forEach(function (arrayOption, i) {
  3682. if (!isObject(arrayOption)) {
  3683. // Simple arrays, e.g. [String, Number, Boolean]
  3684. traverse(arrayOption, 0, nestedEditables[key], parent[key]);
  3685. }
  3686. else {
  3687. // Advanced arrays, e.g. [Object, Object]
  3688. parent[key][i] = {};
  3689. objectEach(arrayOption, function (nestedOption, nestedKey) {
  3690. traverse(nestedOption, nestedKey, nestedEditables[key], parent[key][i]);
  3691. });
  3692. }
  3693. });
  3694. }
  3695. else if (isObject(option)) {
  3696. nextParent = {};
  3697. if (isArray(parent)) {
  3698. parent.push(nextParent);
  3699. nextParent[key] = {};
  3700. nextParent = nextParent[key];
  3701. }
  3702. else {
  3703. parent[key] = nextParent;
  3704. }
  3705. objectEach(option, function (nestedOption, nestedKey) {
  3706. traverse(nestedOption, nestedKey, key === 0 ? parentEditables : nestedEditables[key], nextParent);
  3707. });
  3708. }
  3709. else {
  3710. // Leaf:
  3711. if (key === 'format') {
  3712. parent[key] = [
  3713. format(option, annotation.labels[0].points[0]).toString(),
  3714. 'text'
  3715. ];
  3716. }
  3717. else if (isArray(parent)) {
  3718. parent.push([option, getFieldType(option)]);
  3719. }
  3720. else {
  3721. parent[key] = [option, getFieldType(option)];
  3722. }
  3723. }
  3724. }
  3725. }
  3726. objectEach(options, function (option, key) {
  3727. if (key === 'typeOptions') {
  3728. visualOptions[key] = {};
  3729. objectEach(options[key], function (typeOption, typeKey) {
  3730. traverse(typeOption, typeKey, nestedEditables, visualOptions[key], true);
  3731. });
  3732. }
  3733. else {
  3734. traverse(option, key, editables[type], visualOptions);
  3735. }
  3736. });
  3737. return visualOptions;
  3738. };
  3739. /**
  3740. * Get all class names for all parents in the element. Iterates until finds
  3741. * main container.
  3742. *
  3743. * @function Highcharts.NavigationBindings#getClickedClassNames
  3744. *
  3745. * @param {Highcharts.HTMLDOMElement}
  3746. * Container that event is bound to.
  3747. *
  3748. * @param {global.Event} event
  3749. * Browser's event.
  3750. *
  3751. * @return {Array<Array<string, Highcharts.HTMLDOMElement>>}
  3752. * Array of class names with corresponding elements
  3753. */
  3754. NavigationBindings.prototype.getClickedClassNames = function (container, event) {
  3755. var element = event.target, classNames = [], elemClassName;
  3756. while (element) {
  3757. elemClassName = attr(element, 'class');
  3758. if (elemClassName) {
  3759. classNames = classNames.concat(elemClassName
  3760. .split(' ')
  3761. .map(function (name) {
  3762. return [
  3763. name,
  3764. element
  3765. ];
  3766. }));
  3767. }
  3768. element = element.parentNode;
  3769. if (element === container) {
  3770. return classNames;
  3771. }
  3772. }
  3773. return classNames;
  3774. };
  3775. /**
  3776. * Get events bound to a button. It's a custom event delegation to find all
  3777. * events connected to the element.
  3778. *
  3779. * @private
  3780. * @function Highcharts.NavigationBindings#getButtonEvents
  3781. *
  3782. * @param {Highcharts.HTMLDOMElement} container
  3783. * Container that event is bound to.
  3784. *
  3785. * @param {global.Event} event
  3786. * Browser's event.
  3787. *
  3788. * @return {object}
  3789. * Object with events (init, start, steps, and end)
  3790. */
  3791. NavigationBindings.prototype.getButtonEvents = function (container, event) {
  3792. var navigation = this, classNames = this.getClickedClassNames(container, event), bindings;
  3793. classNames.forEach(function (className) {
  3794. if (navigation.boundClassNames[className[0]] && !bindings) {
  3795. bindings = {
  3796. events: navigation.boundClassNames[className[0]],
  3797. button: className[1]
  3798. };
  3799. }
  3800. });
  3801. return bindings;
  3802. };
  3803. /**
  3804. * Bindings are just events, so the whole update process is simply
  3805. * removing old events and adding new ones.
  3806. *
  3807. * @private
  3808. * @function Highcharts.NavigationBindings#update
  3809. */
  3810. NavigationBindings.prototype.update = function (options) {
  3811. this.options = merge(true, this.options, options);
  3812. this.removeEvents();
  3813. this.initEvents();
  3814. };
  3815. /**
  3816. * Remove all events created in the navigation.
  3817. *
  3818. * @private
  3819. * @function Highcharts.NavigationBindings#removeEvents
  3820. */
  3821. NavigationBindings.prototype.removeEvents = function () {
  3822. this.eventsToUnbind.forEach(function (unbinder) {
  3823. unbinder();
  3824. });
  3825. };
  3826. NavigationBindings.prototype.destroy = function () {
  3827. this.removeEvents();
  3828. };
  3829. /* *
  3830. *
  3831. * Static Properties
  3832. *
  3833. * */
  3834. // Define which options from annotations should show up in edit box:
  3835. NavigationBindings.annotationsEditable = {
  3836. // `typeOptions` are always available
  3837. // Nested and shared options:
  3838. nestedOptions: {
  3839. labelOptions: ['style', 'format', 'backgroundColor'],
  3840. labels: ['style'],
  3841. label: ['style'],
  3842. style: ['fontSize', 'color'],
  3843. background: ['fill', 'strokeWidth', 'stroke'],
  3844. innerBackground: ['fill', 'strokeWidth', 'stroke'],
  3845. outerBackground: ['fill', 'strokeWidth', 'stroke'],
  3846. shapeOptions: ['fill', 'strokeWidth', 'stroke'],
  3847. shapes: ['fill', 'strokeWidth', 'stroke'],
  3848. line: ['strokeWidth', 'stroke'],
  3849. backgroundColors: [true],
  3850. connector: ['fill', 'strokeWidth', 'stroke'],
  3851. crosshairX: ['strokeWidth', 'stroke'],
  3852. crosshairY: ['strokeWidth', 'stroke']
  3853. },
  3854. // Simple shapes:
  3855. circle: ['shapes'],
  3856. verticalLine: [],
  3857. label: ['labelOptions'],
  3858. // Measure
  3859. measure: ['background', 'crosshairY', 'crosshairX'],
  3860. // Others:
  3861. fibonacci: [],
  3862. tunnel: ['background', 'line', 'height'],
  3863. pitchfork: ['innerBackground', 'outerBackground'],
  3864. rect: ['shapes'],
  3865. // Crooked lines, elliots, arrows etc:
  3866. crookedLine: [],
  3867. basicAnnotation: []
  3868. };
  3869. // Define non editable fields per annotation, for example Rectangle inherits
  3870. // options from Measure, but crosshairs are not available
  3871. NavigationBindings.annotationsNonEditable = {
  3872. rectangle: ['crosshairX', 'crosshairY', 'label']
  3873. };
  3874. return NavigationBindings;
  3875. }());
  3876. /**
  3877. * General utils for bindings
  3878. *
  3879. * @private
  3880. * @name Highcharts.NavigationBindings.utils
  3881. * @type {bindingsUtils}
  3882. */
  3883. NavigationBindings.prototype.utils = bindingsUtils;
  3884. H.Chart.prototype.initNavigationBindings = function () {
  3885. var chart = this, options = chart.options;
  3886. if (options && options.navigation && options.navigation.bindings) {
  3887. chart.navigationBindings = new NavigationBindings(chart, options.navigation);
  3888. chart.navigationBindings.initEvents();
  3889. chart.navigationBindings.initUpdate();
  3890. }
  3891. };
  3892. addEvent(H.Chart, 'load', function () {
  3893. this.initNavigationBindings();
  3894. });
  3895. addEvent(H.Chart, 'destroy', function () {
  3896. if (this.navigationBindings) {
  3897. this.navigationBindings.destroy();
  3898. }
  3899. });
  3900. addEvent(NavigationBindings, 'deselectButton', function () {
  3901. this.selectedButtonElement = null;
  3902. });
  3903. addEvent(Annotation, 'remove', function () {
  3904. if (this.chart.navigationBindings) {
  3905. this.chart.navigationBindings.deselectAnnotation();
  3906. }
  3907. });
  3908. /**
  3909. * Show edit-annotation form:
  3910. * @private
  3911. */
  3912. function selectableAnnotation(annotationType) {
  3913. var originalClick = annotationType.prototype.defaultOptions.events &&
  3914. annotationType.prototype.defaultOptions.events.click;
  3915. /**
  3916. * @private
  3917. */
  3918. function selectAndshowPopup(event) {
  3919. var annotation = this, navigation = annotation.chart.navigationBindings, prevAnnotation = navigation.activeAnnotation;
  3920. if (originalClick) {
  3921. originalClick.call(annotation, event);
  3922. }
  3923. if (prevAnnotation !== annotation) {
  3924. // Select current:
  3925. navigation.deselectAnnotation();
  3926. navigation.activeAnnotation = annotation;
  3927. annotation.setControlPointsVisibility(true);
  3928. fireEvent(navigation, 'showPopup', {
  3929. annotation: annotation,
  3930. formType: 'annotation-toolbar',
  3931. options: navigation.annotationToFields(annotation),
  3932. onSubmit: function (data) {
  3933. var config = {}, typeOptions;
  3934. if (data.actionType === 'remove') {
  3935. navigation.activeAnnotation = false;
  3936. navigation.chart.removeAnnotation(annotation);
  3937. }
  3938. else {
  3939. navigation.fieldsToOptions(data.fields, config);
  3940. navigation.deselectAnnotation();
  3941. typeOptions = config.typeOptions;
  3942. if (annotation.options.type === 'measure') {
  3943. // Manually disable crooshars according to
  3944. // stroke width of the shape:
  3945. typeOptions.crosshairY.enabled =
  3946. typeOptions.crosshairY.strokeWidth !== 0;
  3947. typeOptions.crosshairX.enabled =
  3948. typeOptions.crosshairX.strokeWidth !== 0;
  3949. }
  3950. annotation.update(config);
  3951. }
  3952. }
  3953. });
  3954. }
  3955. else {
  3956. // Deselect current:
  3957. navigation.deselectAnnotation();
  3958. fireEvent(navigation, 'closePopup');
  3959. }
  3960. // Let bubble event to chart.click:
  3961. event.activeAnnotation = true;
  3962. }
  3963. merge(true, annotationType.prototype.defaultOptions.events, {
  3964. click: selectAndshowPopup
  3965. });
  3966. }
  3967. if (H.Annotation) {
  3968. // Basic shapes:
  3969. selectableAnnotation(Annotation);
  3970. // Advanced annotations:
  3971. objectEach(Annotation.types, function (annotationType) {
  3972. selectableAnnotation(annotationType);
  3973. });
  3974. }
  3975. setOptions({
  3976. /**
  3977. * @optionparent lang
  3978. *
  3979. * @private
  3980. */
  3981. lang: {
  3982. /**
  3983. * Configure the Popup strings in the chart. Requires the
  3984. * `annotations.js` or `annotations-advanced.src.js` module to be
  3985. * loaded.
  3986. *
  3987. * @since 7.0.0
  3988. * @product highcharts highstock
  3989. */
  3990. navigation: {
  3991. /**
  3992. * Translations for all field names used in popup.
  3993. *
  3994. * @product highcharts highstock
  3995. */
  3996. popup: {
  3997. simpleShapes: 'Simple shapes',
  3998. lines: 'Lines',
  3999. circle: 'Circle',
  4000. rectangle: 'Rectangle',
  4001. label: 'Label',
  4002. shapeOptions: 'Shape options',
  4003. typeOptions: 'Details',
  4004. fill: 'Fill',
  4005. format: 'Text',
  4006. strokeWidth: 'Line width',
  4007. stroke: 'Line color',
  4008. title: 'Title',
  4009. name: 'Name',
  4010. labelOptions: 'Label options',
  4011. labels: 'Labels',
  4012. backgroundColor: 'Background color',
  4013. backgroundColors: 'Background colors',
  4014. borderColor: 'Border color',
  4015. borderRadius: 'Border radius',
  4016. borderWidth: 'Border width',
  4017. style: 'Style',
  4018. padding: 'Padding',
  4019. fontSize: 'Font size',
  4020. color: 'Color',
  4021. height: 'Height',
  4022. shapes: 'Shape options'
  4023. }
  4024. }
  4025. },
  4026. /**
  4027. * @optionparent navigation
  4028. * @product highcharts highstock
  4029. *
  4030. * @private
  4031. */
  4032. navigation: {
  4033. /**
  4034. * A CSS class name where all bindings will be attached to. Multiple
  4035. * charts on the same page should have separate class names to prevent
  4036. * duplicating events.
  4037. *
  4038. * Default value of versions < 7.0.4 `highcharts-bindings-wrapper`
  4039. *
  4040. * @since 7.0.0
  4041. * @type {string}
  4042. */
  4043. bindingsClassName: 'highcharts-bindings-container',
  4044. /**
  4045. * Bindings definitions for custom HTML buttons. Each binding implements
  4046. * simple event-driven interface:
  4047. *
  4048. * - `className`: classname used to bind event to
  4049. *
  4050. * - `init`: initial event, fired on button click
  4051. *
  4052. * - `start`: fired on first click on a chart
  4053. *
  4054. * - `steps`: array of sequential events fired one after another on each
  4055. * of users clicks
  4056. *
  4057. * - `end`: last event to be called after last step event
  4058. *
  4059. * @type {Highcharts.Dictionary<Highcharts.NavigationBindingsOptionsObject>|*}
  4060. * @sample stock/stocktools/stocktools-thresholds
  4061. * Custom bindings in Highstock
  4062. * @since 7.0.0
  4063. * @product highcharts highstock
  4064. */
  4065. bindings: {
  4066. /**
  4067. * A circle annotation bindings. Includes `start` and one event in
  4068. * `steps` array.
  4069. *
  4070. * @type {Highcharts.NavigationBindingsOptionsObject}
  4071. * @default {"className": "highcharts-circle-annotation", "start": function() {}, "steps": [function() {}], "annotationsOptions": {}}
  4072. */
  4073. circleAnnotation: {
  4074. /** @ignore-option */
  4075. className: 'highcharts-circle-annotation',
  4076. /** @ignore-option */
  4077. start: function (e) {
  4078. var coords = this.chart.pointer.getCoordinates(e), navigation = this.chart.options.navigation;
  4079. return this.chart.addAnnotation(merge({
  4080. langKey: 'circle',
  4081. type: 'basicAnnotation',
  4082. shapes: [{
  4083. type: 'circle',
  4084. point: {
  4085. xAxis: 0,
  4086. yAxis: 0,
  4087. x: coords.xAxis[0].value,
  4088. y: coords.yAxis[0].value
  4089. },
  4090. r: 5
  4091. }]
  4092. }, navigation
  4093. .annotationsOptions, navigation
  4094. .bindings
  4095. .circleAnnotation
  4096. .annotationsOptions));
  4097. },
  4098. /** @ignore-option */
  4099. steps: [
  4100. function (e, annotation) {
  4101. var point = annotation.options.shapes[0].point, x = this.chart.xAxis[0].toPixels(point.x), y = this.chart.yAxis[0].toPixels(point.y), inverted = this.chart.inverted, distance = Math.max(Math.sqrt(Math.pow(inverted ? y - e.chartX : x - e.chartX, 2) +
  4102. Math.pow(inverted ? x - e.chartY : y - e.chartY, 2)), 5);
  4103. annotation.update({
  4104. shapes: [{
  4105. r: distance
  4106. }]
  4107. });
  4108. }
  4109. ]
  4110. },
  4111. /**
  4112. * A rectangle annotation bindings. Includes `start` and one event
  4113. * in `steps` array.
  4114. *
  4115. * @type {Highcharts.NavigationBindingsOptionsObject}
  4116. * @default {"className": "highcharts-rectangle-annotation", "start": function() {}, "steps": [function() {}], "annotationsOptions": {}}
  4117. */
  4118. rectangleAnnotation: {
  4119. /** @ignore-option */
  4120. className: 'highcharts-rectangle-annotation',
  4121. /** @ignore-option */
  4122. start: function (e) {
  4123. var coords = this.chart.pointer.getCoordinates(e), navigation = this.chart.options.navigation, x = coords.xAxis[0].value, y = coords.yAxis[0].value;
  4124. return this.chart.addAnnotation(merge({
  4125. langKey: 'rectangle',
  4126. type: 'basicAnnotation',
  4127. shapes: [{
  4128. type: 'path',
  4129. points: [{
  4130. xAxis: 0,
  4131. yAxis: 0,
  4132. x: x,
  4133. y: y
  4134. }, {
  4135. xAxis: 0,
  4136. yAxis: 0,
  4137. x: x,
  4138. y: y
  4139. }, {
  4140. xAxis: 0,
  4141. yAxis: 0,
  4142. x: x,
  4143. y: y
  4144. }, {
  4145. xAxis: 0,
  4146. yAxis: 0,
  4147. x: x,
  4148. y: y
  4149. }]
  4150. }]
  4151. }, navigation
  4152. .annotationsOptions, navigation
  4153. .bindings
  4154. .rectangleAnnotation
  4155. .annotationsOptions));
  4156. },
  4157. /** @ignore-option */
  4158. steps: [
  4159. function (e, annotation) {
  4160. var points = annotation.options.shapes[0].points, coords = this.chart.pointer.getCoordinates(e), x = coords.xAxis[0].value, y = coords.yAxis[0].value;
  4161. // Top right point
  4162. points[1].x = x;
  4163. // Bottom right point (cursor position)
  4164. points[2].x = x;
  4165. points[2].y = y;
  4166. // Bottom left
  4167. points[3].y = y;
  4168. annotation.update({
  4169. shapes: [{
  4170. points: points
  4171. }]
  4172. });
  4173. }
  4174. ]
  4175. },
  4176. /**
  4177. * A label annotation bindings. Includes `start` event only.
  4178. *
  4179. * @type {Highcharts.NavigationBindingsOptionsObject}
  4180. * @default {"className": "highcharts-label-annotation", "start": function() {}, "steps": [function() {}], "annotationsOptions": {}}
  4181. */
  4182. labelAnnotation: {
  4183. /** @ignore-option */
  4184. className: 'highcharts-label-annotation',
  4185. /** @ignore-option */
  4186. start: function (e) {
  4187. var coords = this.chart.pointer.getCoordinates(e), navigation = this.chart.options.navigation;
  4188. return this.chart.addAnnotation(merge({
  4189. langKey: 'label',
  4190. type: 'basicAnnotation',
  4191. labelOptions: {
  4192. format: '{y:.2f}'
  4193. },
  4194. labels: [{
  4195. point: {
  4196. xAxis: 0,
  4197. yAxis: 0,
  4198. x: coords.xAxis[0].value,
  4199. y: coords.yAxis[0].value
  4200. },
  4201. overflow: 'none',
  4202. crop: true
  4203. }]
  4204. }, navigation
  4205. .annotationsOptions, navigation
  4206. .bindings
  4207. .labelAnnotation
  4208. .annotationsOptions));
  4209. }
  4210. }
  4211. },
  4212. /**
  4213. * Path where Highcharts will look for icons. Change this to use icons
  4214. * from a different server.
  4215. *
  4216. * @type {string}
  4217. * @default https://code.highcharts.com/8.1.2/gfx/stock-icons/
  4218. * @since 7.1.3
  4219. * @apioption navigation.iconsURL
  4220. */
  4221. /**
  4222. * A `showPopup` event. Fired when selecting for example an annotation.
  4223. *
  4224. * @type {Function}
  4225. * @apioption navigation.events.showPopup
  4226. */
  4227. /**
  4228. * A `closePopup` event. Fired when Popup should be hidden, for example
  4229. * when clicking on an annotation again.
  4230. *
  4231. * @type {Function}
  4232. * @apioption navigation.events.closePopup
  4233. */
  4234. /**
  4235. * Event fired on a button click.
  4236. *
  4237. * @type {Function}
  4238. * @sample highcharts/annotations/gui/
  4239. * Change icon in a dropddown on event
  4240. * @sample highcharts/annotations/gui-buttons/
  4241. * Change button class on event
  4242. * @apioption navigation.events.selectButton
  4243. */
  4244. /**
  4245. * Event fired when button state should change, for example after
  4246. * adding an annotation.
  4247. *
  4248. * @type {Function}
  4249. * @sample highcharts/annotations/gui/
  4250. * Change icon in a dropddown on event
  4251. * @sample highcharts/annotations/gui-buttons/
  4252. * Change button class on event
  4253. * @apioption navigation.events.deselectButton
  4254. */
  4255. /**
  4256. * Events to communicate between Stock Tools and custom GUI.
  4257. *
  4258. * @since 7.0.0
  4259. * @product highcharts highstock
  4260. * @optionparent navigation.events
  4261. */
  4262. events: {},
  4263. /**
  4264. * Additional options to be merged into all annotations.
  4265. *
  4266. * @sample stock/stocktools/navigation-annotation-options
  4267. * Set red color of all line annotations
  4268. *
  4269. * @type {Highcharts.AnnotationsOptions}
  4270. * @extends annotations
  4271. * @exclude crookedLine, elliottWave, fibonacci, infinityLine,
  4272. * measure, pitchfork, tunnel, verticalLine, basicAnnotation
  4273. * @apioption navigation.annotationsOptions
  4274. */
  4275. annotationsOptions: {}
  4276. }
  4277. });
  4278. return NavigationBindings;
  4279. });
  4280. _registerModule(_modules, 'annotations/popup.js', [_modules['parts/Globals.js'], _modules['annotations/navigationBindings.js'], _modules['parts/Pointer.js'], _modules['parts/Utilities.js']], function (H, NavigationBindings, Pointer, U) {
  4281. /* *
  4282. *
  4283. * Popup generator for Stock tools
  4284. *
  4285. * (c) 2009-2017 Sebastian Bochan
  4286. *
  4287. * License: www.highcharts.com/license
  4288. *
  4289. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  4290. *
  4291. * */
  4292. var addEvent = U.addEvent, createElement = U.createElement, defined = U.defined, getOptions = U.getOptions, isArray = U.isArray, isObject = U.isObject, isString = U.isString, objectEach = U.objectEach, pick = U.pick, wrap = U.wrap;
  4293. var indexFilter = /\d/g, PREFIX = 'highcharts-', DIV = 'div', INPUT = 'input', LABEL = 'label', BUTTON = 'button', SELECT = 'select', OPTION = 'option', SPAN = 'span', UL = 'ul', LI = 'li', H3 = 'h3';
  4294. /* eslint-disable no-invalid-this, valid-jsdoc */
  4295. // onContainerMouseDown blocks internal popup events, due to e.preventDefault.
  4296. // Related issue #4606
  4297. wrap(Pointer.prototype, 'onContainerMouseDown', function (proceed, e) {
  4298. var popupClass = e.target && e.target.className;
  4299. // elements is not in popup
  4300. if (!(isString(popupClass) &&
  4301. popupClass.indexOf(PREFIX + 'popup-field') >= 0)) {
  4302. proceed.apply(this, Array.prototype.slice.call(arguments, 1));
  4303. }
  4304. });
  4305. H.Popup = function (parentDiv, iconsURL) {
  4306. this.init(parentDiv, iconsURL);
  4307. };
  4308. H.Popup.prototype = {
  4309. /**
  4310. * Initialize the popup. Create base div and add close button.
  4311. * @private
  4312. * @param {Highcharts.HTMLDOMElement} parentDiv
  4313. * Container where popup should be placed
  4314. * @param {string} iconsURL
  4315. * Icon URL
  4316. */
  4317. init: function (parentDiv, iconsURL) {
  4318. // create popup div
  4319. this.container = createElement(DIV, {
  4320. className: PREFIX + 'popup'
  4321. }, null, parentDiv);
  4322. this.lang = this.getLangpack();
  4323. this.iconsURL = iconsURL;
  4324. // add close button
  4325. this.addCloseBtn();
  4326. },
  4327. /**
  4328. * Create HTML element and attach click event (close popup).
  4329. * @private
  4330. */
  4331. addCloseBtn: function () {
  4332. var _self = this, closeBtn;
  4333. // create close popup btn
  4334. closeBtn = createElement(DIV, {
  4335. className: PREFIX + 'popup-close'
  4336. }, null, this.container);
  4337. closeBtn.style['background-image'] = 'url(' +
  4338. this.iconsURL + 'close.svg)';
  4339. ['click', 'touchstart'].forEach(function (eventName) {
  4340. addEvent(closeBtn, eventName, function () {
  4341. _self.closePopup();
  4342. });
  4343. });
  4344. },
  4345. /**
  4346. * Create two columns (divs) in HTML.
  4347. * @private
  4348. * @param {Highcharts.HTMLDOMElement} container
  4349. * Container of columns
  4350. * @return {Highcharts.Dictionary<Highcharts.HTMLDOMElement>}
  4351. * Reference to two HTML columns (lhsCol, rhsCol)
  4352. */
  4353. addColsContainer: function (container) {
  4354. var rhsCol, lhsCol;
  4355. // left column
  4356. lhsCol = createElement(DIV, {
  4357. className: PREFIX + 'popup-lhs-col'
  4358. }, null, container);
  4359. // right column
  4360. rhsCol = createElement(DIV, {
  4361. className: PREFIX + 'popup-rhs-col'
  4362. }, null, container);
  4363. // wrapper content
  4364. createElement(DIV, {
  4365. className: PREFIX + 'popup-rhs-col-wrapper'
  4366. }, null, rhsCol);
  4367. return {
  4368. lhsCol: lhsCol,
  4369. rhsCol: rhsCol
  4370. };
  4371. },
  4372. /**
  4373. * Create input with label.
  4374. * @private
  4375. * @param {string} option
  4376. * Chain of fields i.e params.styles.fontSize
  4377. * @param {string} type
  4378. * Indicator type
  4379. * @param {Highhcharts.HTMLDOMElement}
  4380. * Container where elements should be added
  4381. * @param {string} value
  4382. * Default value of input i.e period value is 14, extracted from
  4383. * defaultOptions (ADD mode) or series options (EDIT mode)
  4384. */
  4385. addInput: function (option, type, parentDiv, value) {
  4386. var optionParamList = option.split('.'), optionName = optionParamList[optionParamList.length - 1], lang = this.lang, inputName = PREFIX + type + '-' + optionName;
  4387. if (!inputName.match(indexFilter)) {
  4388. // add label
  4389. createElement(LABEL, {
  4390. innerHTML: lang[optionName] || optionName,
  4391. htmlFor: inputName
  4392. }, null, parentDiv);
  4393. }
  4394. // add input
  4395. createElement(INPUT, {
  4396. name: inputName,
  4397. value: value[0],
  4398. type: value[1],
  4399. className: PREFIX + 'popup-field'
  4400. }, null, parentDiv).setAttribute(PREFIX + 'data-name', option);
  4401. },
  4402. /**
  4403. * Create button.
  4404. * @private
  4405. * @param {Highcharts.HTMLDOMElement} parentDiv
  4406. * Container where elements should be added
  4407. * @param {string} label
  4408. * Text placed as button label
  4409. * @param {string} type
  4410. * add | edit | remove
  4411. * @param {Function} callback
  4412. * On click callback
  4413. * @param {Highcharts.HTMLDOMElement} fieldsDiv
  4414. * Container where inputs are generated
  4415. * @return {Highcharts.HTMLDOMElement}
  4416. * HTML button
  4417. */
  4418. addButton: function (parentDiv, label, type, callback, fieldsDiv) {
  4419. var _self = this, closePopup = this.closePopup, getFields = this.getFields, button;
  4420. button = createElement(BUTTON, {
  4421. innerHTML: label
  4422. }, null, parentDiv);
  4423. ['click', 'touchstart'].forEach(function (eventName) {
  4424. addEvent(button, eventName, function () {
  4425. closePopup.call(_self);
  4426. return callback(getFields(fieldsDiv, type));
  4427. });
  4428. });
  4429. return button;
  4430. },
  4431. /**
  4432. * Get values from all inputs and create JSON.
  4433. * @private
  4434. * @param {Highcharts.HTMLDOMElement} - container where inputs are created
  4435. * @param {string} - add | edit | remove
  4436. * @return {Highcharts.PopupFieldsObject} - fields
  4437. */
  4438. getFields: function (parentDiv, type) {
  4439. var inputList = parentDiv.querySelectorAll('input'), optionSeries = '#' + PREFIX + 'select-series > option:checked', optionVolume = '#' + PREFIX + 'select-volume > option:checked', linkedTo = parentDiv.querySelectorAll(optionSeries)[0], volumeTo = parentDiv.querySelectorAll(optionVolume)[0], seriesId, param, fieldsOutput;
  4440. fieldsOutput = {
  4441. actionType: type,
  4442. linkedTo: linkedTo && linkedTo.getAttribute('value'),
  4443. fields: {}
  4444. };
  4445. [].forEach.call(inputList, function (input) {
  4446. param = input.getAttribute(PREFIX + 'data-name');
  4447. seriesId = input.getAttribute(PREFIX + 'data-series-id');
  4448. // params
  4449. if (seriesId) {
  4450. fieldsOutput.seriesId = input.value;
  4451. }
  4452. else if (param) {
  4453. fieldsOutput.fields[param] = input.value;
  4454. }
  4455. else {
  4456. // type like sma / ema
  4457. fieldsOutput.type = input.value;
  4458. }
  4459. });
  4460. if (volumeTo) {
  4461. fieldsOutput.fields['params.volumeSeriesID'] = volumeTo.getAttribute('value');
  4462. }
  4463. return fieldsOutput;
  4464. },
  4465. /**
  4466. * Reset content of the current popup and show.
  4467. * @private
  4468. */
  4469. showPopup: function () {
  4470. var popupDiv = this.container, toolbarClass = PREFIX + 'annotation-toolbar', popupCloseBtn = popupDiv
  4471. .querySelectorAll('.' + PREFIX + 'popup-close')[0];
  4472. // reset content
  4473. popupDiv.innerHTML = '';
  4474. // reset toolbar styles if exists
  4475. if (popupDiv.className.indexOf(toolbarClass) >= 0) {
  4476. popupDiv.classList.remove(toolbarClass);
  4477. // reset toolbar inline styles
  4478. popupDiv.removeAttribute('style');
  4479. }
  4480. // add close button
  4481. popupDiv.appendChild(popupCloseBtn);
  4482. popupDiv.style.display = 'block';
  4483. },
  4484. /**
  4485. * Hide popup.
  4486. * @private
  4487. */
  4488. closePopup: function () {
  4489. this.popup.container.style.display = 'none';
  4490. },
  4491. /**
  4492. * Create content and show popup.
  4493. * @private
  4494. * @param {string} - type of popup i.e indicators
  4495. * @param {Highcharts.Chart} - chart
  4496. * @param {Highcharts.AnnotationsOptions} - options
  4497. * @param {Function} - on click callback
  4498. */
  4499. showForm: function (type, chart, options, callback) {
  4500. this.popup = chart.navigationBindings.popup;
  4501. // show blank popup
  4502. this.showPopup();
  4503. // indicator form
  4504. if (type === 'indicators') {
  4505. this.indicators.addForm.call(this, chart, options, callback);
  4506. }
  4507. // annotation small toolbar
  4508. if (type === 'annotation-toolbar') {
  4509. this.annotations.addToolbar.call(this, chart, options, callback);
  4510. }
  4511. // annotation edit form
  4512. if (type === 'annotation-edit') {
  4513. this.annotations.addForm.call(this, chart, options, callback);
  4514. }
  4515. // flags form - add / edit
  4516. if (type === 'flag') {
  4517. this.annotations.addForm.call(this, chart, options, callback, true);
  4518. }
  4519. },
  4520. /**
  4521. * Return lang definitions for popup.
  4522. * @private
  4523. * @return {Highcharts.Dictionary<string>} - elements translations.
  4524. */
  4525. getLangpack: function () {
  4526. return getOptions().lang.navigation.popup;
  4527. },
  4528. annotations: {
  4529. /**
  4530. * Create annotation simple form. It contains two buttons
  4531. * (edit / remove) and text label.
  4532. * @private
  4533. * @param {Highcharts.Chart} - chart
  4534. * @param {Highcharts.AnnotationsOptions} - options
  4535. * @param {Function} - on click callback
  4536. */
  4537. addToolbar: function (chart, options, callback) {
  4538. var _self = this, lang = this.lang, popupDiv = this.popup.container, showForm = this.showForm, toolbarClass = PREFIX + 'annotation-toolbar', button;
  4539. // set small size
  4540. if (popupDiv.className.indexOf(toolbarClass) === -1) {
  4541. popupDiv.className += ' ' + toolbarClass;
  4542. }
  4543. // set position
  4544. popupDiv.style.top = chart.plotTop + 10 + 'px';
  4545. // create label
  4546. createElement(SPAN, {
  4547. innerHTML: pick(
  4548. // Advanced annotations:
  4549. lang[options.langKey] || options.langKey,
  4550. // Basic shapes:
  4551. options.shapes && options.shapes[0].type)
  4552. }, null, popupDiv);
  4553. // add buttons
  4554. button = this.addButton(popupDiv, lang.removeButton || 'remove', 'remove', callback, popupDiv);
  4555. button.className += ' ' + PREFIX + 'annotation-remove-button';
  4556. button.style['background-image'] = 'url(' +
  4557. this.iconsURL + 'destroy.svg)';
  4558. button = this.addButton(popupDiv, lang.editButton || 'edit', 'edit', function () {
  4559. showForm.call(_self, 'annotation-edit', chart, options, callback);
  4560. }, popupDiv);
  4561. button.className += ' ' + PREFIX + 'annotation-edit-button';
  4562. button.style['background-image'] = 'url(' +
  4563. this.iconsURL + 'edit.svg)';
  4564. },
  4565. /**
  4566. * Create annotation simple form.
  4567. * It contains fields with param names.
  4568. * @private
  4569. * @param {Highcharts.Chart} chart
  4570. * Chart
  4571. * @param {Object} options
  4572. * Options
  4573. * @param {Function} callback
  4574. * On click callback
  4575. * @param {boolean} [isInit]
  4576. * If it is a form declared for init annotation
  4577. */
  4578. addForm: function (chart, options, callback, isInit) {
  4579. var popupDiv = this.popup.container, lang = this.lang, bottomRow, lhsCol;
  4580. // create title of annotations
  4581. lhsCol = createElement('h2', {
  4582. innerHTML: lang[options.langKey] || options.langKey,
  4583. className: PREFIX + 'popup-main-title'
  4584. }, null, popupDiv);
  4585. // left column
  4586. lhsCol = createElement(DIV, {
  4587. className: PREFIX + 'popup-lhs-col ' + PREFIX + 'popup-lhs-full'
  4588. }, null, popupDiv);
  4589. bottomRow = createElement(DIV, {
  4590. className: PREFIX + 'popup-bottom-row'
  4591. }, null, popupDiv);
  4592. this.annotations.addFormFields.call(this, lhsCol, chart, '', options, [], true);
  4593. this.addButton(bottomRow, isInit ?
  4594. (lang.addButton || 'add') :
  4595. (lang.saveButton || 'save'), isInit ? 'add' : 'save', callback, popupDiv);
  4596. },
  4597. /**
  4598. * Create annotation's form fields.
  4599. * @private
  4600. * @param {Highcharts.HTMLDOMElement} parentDiv
  4601. * Div where inputs are placed
  4602. * @param {Highcharts.Chart} chart
  4603. * Chart
  4604. * @param {string} parentNode
  4605. * Name of parent to create chain of names
  4606. * @param {Highcharts.AnnotationsOptions} options
  4607. * Options
  4608. * @param {Array<unknown>} storage
  4609. * Array where all items are stored
  4610. * @param {boolean} [isRoot]
  4611. * Recursive flag for root
  4612. */
  4613. addFormFields: function (parentDiv, chart, parentNode, options, storage, isRoot) {
  4614. var _self = this, addFormFields = this.annotations.addFormFields, addInput = this.addInput, lang = this.lang, parentFullName, titleName;
  4615. objectEach(options, function (value, option) {
  4616. // create name like params.styles.fontSize
  4617. parentFullName = parentNode !== '' ?
  4618. parentNode + '.' + option : option;
  4619. if (isObject(value)) {
  4620. if (
  4621. // value is object of options
  4622. !isArray(value) ||
  4623. // array of objects with params. i.e labels in Fibonacci
  4624. (isArray(value) && isObject(value[0]))) {
  4625. titleName = lang[option] || option;
  4626. if (!titleName.match(indexFilter)) {
  4627. storage.push([
  4628. true,
  4629. titleName,
  4630. parentDiv
  4631. ]);
  4632. }
  4633. addFormFields.call(_self, parentDiv, chart, parentFullName, value, storage, false);
  4634. }
  4635. else {
  4636. storage.push([
  4637. _self,
  4638. parentFullName,
  4639. 'annotation',
  4640. parentDiv,
  4641. value
  4642. ]);
  4643. }
  4644. }
  4645. });
  4646. if (isRoot) {
  4647. storage = storage.sort(function (a) {
  4648. return a[1].match(/format/g) ? -1 : 1;
  4649. });
  4650. storage.forEach(function (genInput) {
  4651. if (genInput[0] === true) {
  4652. createElement(SPAN, {
  4653. className: PREFIX + 'annotation-title',
  4654. innerHTML: genInput[1]
  4655. }, null, genInput[2]);
  4656. }
  4657. else {
  4658. addInput.apply(genInput[0], genInput.splice(1));
  4659. }
  4660. });
  4661. }
  4662. }
  4663. },
  4664. indicators: {
  4665. /**
  4666. * Create indicator's form. It contains two tabs (ADD and EDIT) with
  4667. * content.
  4668. * @private
  4669. */
  4670. addForm: function (chart, _options, callback) {
  4671. var tabsContainers, indicators = this.indicators, lang = this.lang, buttonParentDiv;
  4672. // add tabs
  4673. this.tabs.init.call(this, chart);
  4674. // get all tabs content divs
  4675. tabsContainers = this.popup.container
  4676. .querySelectorAll('.' + PREFIX + 'tab-item-content');
  4677. // ADD tab
  4678. this.addColsContainer(tabsContainers[0]);
  4679. indicators.addIndicatorList.call(this, chart, tabsContainers[0], 'add');
  4680. buttonParentDiv = tabsContainers[0]
  4681. .querySelectorAll('.' + PREFIX + 'popup-rhs-col')[0];
  4682. this.addButton(buttonParentDiv, lang.addButton || 'add', 'add', callback, buttonParentDiv);
  4683. // EDIT tab
  4684. this.addColsContainer(tabsContainers[1]);
  4685. indicators.addIndicatorList.call(this, chart, tabsContainers[1], 'edit');
  4686. buttonParentDiv = tabsContainers[1]
  4687. .querySelectorAll('.' + PREFIX + 'popup-rhs-col')[0];
  4688. this.addButton(buttonParentDiv, lang.saveButton || 'save', 'edit', callback, buttonParentDiv);
  4689. this.addButton(buttonParentDiv, lang.removeButton || 'remove', 'remove', callback, buttonParentDiv);
  4690. },
  4691. /**
  4692. * Create HTML list of all indicators (ADD mode) or added indicators
  4693. * (EDIT mode).
  4694. * @private
  4695. */
  4696. addIndicatorList: function (chart, parentDiv, listType) {
  4697. var _self = this, lhsCol = parentDiv.querySelectorAll('.' + PREFIX + 'popup-lhs-col')[0], rhsCol = parentDiv.querySelectorAll('.' + PREFIX + 'popup-rhs-col')[0], isEdit = listType === 'edit', series = (isEdit ?
  4698. chart.series : // EDIT mode
  4699. chart.options.plotOptions // ADD mode
  4700. ), addFormFields = this.indicators.addFormFields, rhsColWrapper, indicatorList, item;
  4701. // create wrapper for list
  4702. indicatorList = createElement(UL, {
  4703. className: PREFIX + 'indicator-list'
  4704. }, null, lhsCol);
  4705. rhsColWrapper = rhsCol
  4706. .querySelectorAll('.' + PREFIX + 'popup-rhs-col-wrapper')[0];
  4707. objectEach(series, function (serie, value) {
  4708. var seriesOptions = serie.options;
  4709. if (serie.params ||
  4710. seriesOptions && seriesOptions.params) {
  4711. var indicatorNameType = _self.indicators.getNameType(serie, value), indicatorType = indicatorNameType.type;
  4712. item = createElement(LI, {
  4713. className: PREFIX + 'indicator-list',
  4714. innerHTML: indicatorNameType.name
  4715. }, null, indicatorList);
  4716. ['click', 'touchstart'].forEach(function (eventName) {
  4717. addEvent(item, eventName, function () {
  4718. addFormFields.call(_self, chart, isEdit ? serie : series[indicatorType], indicatorNameType.type, rhsColWrapper);
  4719. // add hidden input with series.id
  4720. if (isEdit && serie.options) {
  4721. createElement(INPUT, {
  4722. type: 'hidden',
  4723. name: PREFIX + 'id-' + indicatorType,
  4724. value: serie.options.id
  4725. }, null, rhsColWrapper)
  4726. .setAttribute(PREFIX + 'data-series-id', serie.options.id);
  4727. }
  4728. });
  4729. });
  4730. }
  4731. });
  4732. // select first item from the list
  4733. if (indicatorList.childNodes.length > 0) {
  4734. indicatorList.childNodes[0].click();
  4735. }
  4736. },
  4737. /**
  4738. * Extract full name and type of requested indicator.
  4739. * @private
  4740. * @param {Highcharts.Series} series
  4741. * Series which name is needed. (EDIT mode - defaultOptions.series, ADD
  4742. * mode - indicator series).
  4743. * @param {string} - indicator type like: sma, ema, etc.
  4744. * @return {Object} - series name and type like: sma, ema, etc.
  4745. */
  4746. getNameType: function (series, type) {
  4747. var options = series.options, seriesTypes = H.seriesTypes,
  4748. // add mode
  4749. seriesName = seriesTypes[type] &&
  4750. seriesTypes[type].prototype.nameBase || type.toUpperCase(), seriesType = type;
  4751. // edit
  4752. if (options && options.type) {
  4753. seriesType = series.options.type;
  4754. seriesName = series.name;
  4755. }
  4756. return {
  4757. name: seriesName,
  4758. type: seriesType
  4759. };
  4760. },
  4761. /**
  4762. * List all series with unique ID. Its mandatory for indicators to set
  4763. * correct linking.
  4764. * @private
  4765. * @param {string} type
  4766. * Indicator type like: sma, ema, etc.
  4767. * @param {string} optionName
  4768. * Type of select i.e series or volume.
  4769. * @param {Highcharts.Chart} chart
  4770. * Chart
  4771. * @param {Highcharts.HTMLDOMElement} parentDiv
  4772. * Element where created HTML list is added
  4773. * @param {string} selectedOption
  4774. * optional param for default value in dropdown
  4775. */
  4776. listAllSeries: function (type, optionName, chart, parentDiv, selectedOption) {
  4777. var selectName = PREFIX + optionName + '-type-' + type, lang = this.lang, selectBox, seriesOptions;
  4778. createElement(LABEL, {
  4779. innerHTML: lang[optionName] || optionName,
  4780. htmlFor: selectName
  4781. }, null, parentDiv);
  4782. // select type
  4783. selectBox = createElement(SELECT, {
  4784. name: selectName,
  4785. className: PREFIX + 'popup-field'
  4786. }, null, parentDiv);
  4787. selectBox.setAttribute('id', PREFIX + 'select-' + optionName);
  4788. // list all series which have id - mandatory for creating indicator
  4789. chart.series.forEach(function (serie) {
  4790. seriesOptions = serie.options;
  4791. if (!seriesOptions.params &&
  4792. seriesOptions.id &&
  4793. seriesOptions.id !== PREFIX + 'navigator-series') {
  4794. createElement(OPTION, {
  4795. innerHTML: seriesOptions.name || seriesOptions.id,
  4796. value: seriesOptions.id
  4797. }, null, selectBox);
  4798. }
  4799. });
  4800. if (defined(selectedOption)) {
  4801. selectBox.value = selectedOption;
  4802. }
  4803. },
  4804. /**
  4805. * Create typical inputs for chosen indicator. Fields are extracted from
  4806. * defaultOptions (ADD mode) or current indicator (ADD mode). Two extra
  4807. * fields are added:
  4808. * - hidden input - contains indicator type (required for callback)
  4809. * - select - list of series which can be linked with indicator
  4810. * @private
  4811. * @param {Highcharts.Chart} chart
  4812. * Chart
  4813. * @param {Highcharts.Series} series
  4814. * Indicator
  4815. * @param {string} seriesType
  4816. * Indicator type like: sma, ema, etc.
  4817. * @param {Highcharts.HTMLDOMElement} rhsColWrapper
  4818. * Element where created HTML list is added
  4819. */
  4820. addFormFields: function (chart, series, seriesType, rhsColWrapper) {
  4821. var fields = series.params || series.options.params, getNameType = this.indicators.getNameType;
  4822. // reset current content
  4823. rhsColWrapper.innerHTML = '';
  4824. // create title (indicator name in the right column)
  4825. createElement(H3, {
  4826. className: PREFIX + 'indicator-title',
  4827. innerHTML: getNameType(series, seriesType).name
  4828. }, null, rhsColWrapper);
  4829. // input type
  4830. createElement(INPUT, {
  4831. type: 'hidden',
  4832. name: PREFIX + 'type-' + seriesType,
  4833. value: seriesType
  4834. }, null, rhsColWrapper);
  4835. // list all series with id
  4836. this.indicators.listAllSeries.call(this, seriesType, 'series', chart, rhsColWrapper, series.linkedParent && fields.volumeSeriesID);
  4837. if (fields.volumeSeriesID) {
  4838. this.indicators.listAllSeries.call(this, seriesType, 'volume', chart, rhsColWrapper, series.linkedParent && series.linkedParent.options.id);
  4839. }
  4840. // add param fields
  4841. this.indicators.addParamInputs.call(this, chart, 'params', fields, seriesType, rhsColWrapper);
  4842. },
  4843. /**
  4844. * Recurent function which lists all fields, from params object and
  4845. * create them as inputs. Each input has unique `data-name` attribute,
  4846. * which keeps chain of fields i.e params.styles.fontSize.
  4847. * @private
  4848. * @param {Highcharts.Chart} chart
  4849. * Chart
  4850. * @param {string} parentNode
  4851. * Name of parent to create chain of names
  4852. * @param {Highcharts.PopupFieldsDictionary<string>} fields
  4853. * Params which are based for input create
  4854. * @param {string} type
  4855. * Indicator type like: sma, ema, etc.
  4856. * @param {Highcharts.HTMLDOMElement} parentDiv
  4857. * Element where created HTML list is added
  4858. */
  4859. addParamInputs: function (chart, parentNode, fields, type, parentDiv) {
  4860. var _self = this, addParamInputs = this.indicators.addParamInputs, addInput = this.addInput, parentFullName;
  4861. objectEach(fields, function (value, fieldName) {
  4862. // create name like params.styles.fontSize
  4863. parentFullName = parentNode + '.' + fieldName;
  4864. if (isObject(value)) {
  4865. addParamInputs.call(_self, chart, parentFullName, value, type, parentDiv);
  4866. }
  4867. else if (
  4868. // skip volume field which is created by addFormFields
  4869. parentFullName !== 'params.volumeSeriesID') {
  4870. addInput.call(_self, parentFullName, type, parentDiv, [value, 'text'] // all inputs are text type
  4871. );
  4872. }
  4873. });
  4874. },
  4875. /**
  4876. * Get amount of indicators added to chart.
  4877. * @private
  4878. * @return {number} - Amount of indicators
  4879. */
  4880. getAmount: function () {
  4881. var series = this.series, counter = 0;
  4882. series.forEach(function (serie) {
  4883. var seriesOptions = serie.options;
  4884. if (serie.params ||
  4885. seriesOptions && seriesOptions.params) {
  4886. counter++;
  4887. }
  4888. });
  4889. return counter;
  4890. }
  4891. },
  4892. tabs: {
  4893. /**
  4894. * Init tabs. Create tab menu items, tabs containers
  4895. * @private
  4896. * @param {Highcharts.Chart} chart
  4897. * Reference to current chart
  4898. */
  4899. init: function (chart) {
  4900. var tabs = this.tabs, indicatorsCount = this.indicators.getAmount.call(chart), firstTab; // run by default
  4901. // create menu items
  4902. firstTab = tabs.addMenuItem.call(this, 'add');
  4903. tabs.addMenuItem.call(this, 'edit', indicatorsCount);
  4904. // create tabs containers
  4905. tabs.addContentItem.call(this, 'add');
  4906. tabs.addContentItem.call(this, 'edit');
  4907. tabs.switchTabs.call(this, indicatorsCount);
  4908. // activate first tab
  4909. tabs.selectTab.call(this, firstTab, 0);
  4910. },
  4911. /**
  4912. * Create tab menu item
  4913. * @private
  4914. * @param {string} tabName
  4915. * `add` or `edit`
  4916. * @param {number} [disableTab]
  4917. * Disable tab when 0
  4918. * @return {Highcharts.HTMLDOMElement}
  4919. * Created HTML tab-menu element
  4920. */
  4921. addMenuItem: function (tabName, disableTab) {
  4922. var popupDiv = this.popup.container, className = PREFIX + 'tab-item', lang = this.lang, menuItem;
  4923. if (disableTab === 0) {
  4924. className += ' ' + PREFIX + 'tab-disabled';
  4925. }
  4926. // tab 1
  4927. menuItem = createElement(SPAN, {
  4928. innerHTML: lang[tabName + 'Button'] || tabName,
  4929. className: className
  4930. }, null, popupDiv);
  4931. menuItem.setAttribute(PREFIX + 'data-tab-type', tabName);
  4932. return menuItem;
  4933. },
  4934. /**
  4935. * Create tab content
  4936. * @private
  4937. * @return {HTMLDOMElement} - created HTML tab-content element
  4938. */
  4939. addContentItem: function () {
  4940. var popupDiv = this.popup.container;
  4941. return createElement(DIV, {
  4942. className: PREFIX + 'tab-item-content'
  4943. }, null, popupDiv);
  4944. },
  4945. /**
  4946. * Add click event to each tab
  4947. * @private
  4948. * @param {number} disableTab
  4949. * Disable tab when 0
  4950. */
  4951. switchTabs: function (disableTab) {
  4952. var _self = this, popupDiv = this.popup.container, tabs = popupDiv.querySelectorAll('.' + PREFIX + 'tab-item'), dataParam;
  4953. tabs.forEach(function (tab, i) {
  4954. dataParam = tab.getAttribute(PREFIX + 'data-tab-type');
  4955. if (dataParam === 'edit' && disableTab === 0) {
  4956. return;
  4957. }
  4958. ['click', 'touchstart'].forEach(function (eventName) {
  4959. addEvent(tab, eventName, function () {
  4960. // reset class on other elements
  4961. _self.tabs.deselectAll.call(_self);
  4962. _self.tabs.selectTab.call(_self, this, i);
  4963. });
  4964. });
  4965. });
  4966. },
  4967. /**
  4968. * Set tab as visible
  4969. * @private
  4970. * @param {globals.Element} - current tab
  4971. * @param {number} - Index of tab in menu
  4972. */
  4973. selectTab: function (tab, index) {
  4974. var allTabs = this.popup.container
  4975. .querySelectorAll('.' + PREFIX + 'tab-item-content');
  4976. tab.className += ' ' + PREFIX + 'tab-item-active';
  4977. allTabs[index].className += ' ' + PREFIX + 'tab-item-show';
  4978. },
  4979. /**
  4980. * Set all tabs as invisible.
  4981. * @private
  4982. */
  4983. deselectAll: function () {
  4984. var popupDiv = this.popup.container, tabs = popupDiv
  4985. .querySelectorAll('.' + PREFIX + 'tab-item'), tabsContent = popupDiv
  4986. .querySelectorAll('.' + PREFIX + 'tab-item-content'), i;
  4987. for (i = 0; i < tabs.length; i++) {
  4988. tabs[i].classList.remove(PREFIX + 'tab-item-active');
  4989. tabsContent[i].classList.remove(PREFIX + 'tab-item-show');
  4990. }
  4991. }
  4992. }
  4993. };
  4994. addEvent(NavigationBindings, 'showPopup', function (config) {
  4995. if (!this.popup) {
  4996. // Add popup to main container
  4997. this.popup = new H.Popup(this.chart.container, (this.chart.options.navigation.iconsURL ||
  4998. (this.chart.options.stockTools &&
  4999. this.chart.options.stockTools.gui.iconsURL) ||
  5000. 'https://code.highcharts.com/8.1.2/gfx/stock-icons/'));
  5001. }
  5002. this.popup.showForm(config.formType, this.chart, config.options, config.onSubmit);
  5003. });
  5004. addEvent(NavigationBindings, 'closePopup', function () {
  5005. if (this.popup) {
  5006. this.popup.closePopup();
  5007. }
  5008. });
  5009. });
  5010. _registerModule(_modules, 'masters/modules/annotations.src.js', [], function () {
  5011. });
  5012. }));