panel.js 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569
  1. /*!
  2. * AngularJS Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v1.1.8-master-aba7b2b
  6. */
  7. (function( window, angular, undefined ){
  8. "use strict";
  9. /**
  10. * @ngdoc module
  11. * @name material.components.panel
  12. */
  13. MdPanelService['$inject'] = ["presets", "$rootElement", "$rootScope", "$injector", "$window"];
  14. angular
  15. .module('material.components.panel', [
  16. 'material.core',
  17. 'material.components.backdrop'
  18. ])
  19. .provider('$mdPanel', MdPanelProvider);
  20. /*****************************************************************************
  21. * PUBLIC DOCUMENTATION *
  22. *****************************************************************************/
  23. /**
  24. * @ngdoc service
  25. * @name $mdPanelProvider
  26. * @module material.components.panel
  27. *
  28. * @description
  29. * `$mdPanelProvider` allows users to create configuration presets that will be
  30. * stored within a cached presets object. When the configuration is needed, the
  31. * user can request the preset by passing it as the first parameter in the
  32. * `$mdPanel.create` or `$mdPanel.open` methods.
  33. *
  34. * @usage
  35. * <hljs lang="js">
  36. * (function(angular, undefined) {
  37. * 'use strict';
  38. *
  39. * angular
  40. * .module('demoApp', ['ngMaterial'])
  41. * .config(DemoConfig)
  42. * .controller('DemoCtrl', DemoCtrl)
  43. * .controller('DemoMenuCtrl', DemoMenuCtrl);
  44. *
  45. * function DemoConfig($mdPanelProvider) {
  46. * $mdPanelProvider.definePreset('demoPreset', {
  47. * attachTo: angular.element(document.body),
  48. * controller: DemoMenuCtrl,
  49. * controllerAs: 'ctrl',
  50. * template: '' +
  51. * '<div class="menu-panel" md-whiteframe="4">' +
  52. * ' <div class="menu-content">' +
  53. * ' <div class="menu-item" ng-repeat="item in ctrl.items">' +
  54. * ' <button class="md-button">' +
  55. * ' <span>{{item}}</span>' +
  56. * ' </button>' +
  57. * ' </div>' +
  58. * ' <md-divider></md-divider>' +
  59. * ' <div class="menu-item">' +
  60. * ' <button class="md-button" ng-click="ctrl.closeMenu()">' +
  61. * ' <span>Close Menu</span>' +
  62. * ' </button>' +
  63. * ' </div>' +
  64. * ' </div>' +
  65. * '</div>',
  66. * panelClass: 'menu-panel-container',
  67. * focusOnOpen: false,
  68. * zIndex: 100,
  69. * propagateContainerEvents: true,
  70. * groupName: 'menus'
  71. * });
  72. * }
  73. *
  74. * function PanelProviderCtrl($mdPanel) {
  75. * this.navigation = {
  76. * name: 'navigation',
  77. * items: [
  78. * 'Home',
  79. * 'About',
  80. * 'Contact'
  81. * ]
  82. * };
  83. * this.favorites = {
  84. * name: 'favorites',
  85. * items: [
  86. * 'Add to Favorites'
  87. * ]
  88. * };
  89. * this.more = {
  90. * name: 'more',
  91. * items: [
  92. * 'Account',
  93. * 'Sign Out'
  94. * ]
  95. * };
  96. *
  97. * $mdPanel.newPanelGroup('menus', {
  98. * maxOpen: 2
  99. * });
  100. *
  101. * this.showMenu = function($event, menu) {
  102. * $mdPanel.open('demoPreset', {
  103. * id: 'menu_' + menu.name,
  104. * position: $mdPanel.newPanelPosition()
  105. * .relativeTo($event.srcElement)
  106. * .addPanelPosition(
  107. * $mdPanel.xPosition.ALIGN_START,
  108. * $mdPanel.yPosition.BELOW
  109. * ),
  110. * locals: {
  111. * items: menu.items
  112. * },
  113. * openFrom: $event
  114. * });
  115. * };
  116. * }
  117. *
  118. * function PanelMenuCtrl(mdPanelRef) {
  119. * // The controller is provided with an import named 'mdPanelRef'
  120. * this.closeMenu = function() {
  121. * mdPanelRef && mdPanelRef.close();
  122. * };
  123. * }
  124. * })(angular);
  125. * </hljs>
  126. */
  127. /**
  128. * @ngdoc method
  129. * @name $mdPanelProvider#definePreset
  130. * @description
  131. * Takes the passed in preset name and preset configuration object and adds it
  132. * to the `_presets` object of the provider. This `_presets` object is then
  133. * passed along to the `$mdPanel` service.
  134. *
  135. * @param {string} name Preset name.
  136. * @param {!Object} preset Specific configuration object that can contain any
  137. * and all of the parameters avaialble within the `$mdPanel.create` method.
  138. * However, parameters that pertain to id, position, animation, and user
  139. * interaction are not allowed and will be removed from the preset
  140. * configuration.
  141. */
  142. /*****************************************************************************
  143. * MdPanel Service *
  144. *****************************************************************************/
  145. /**
  146. * @ngdoc service
  147. * @name $mdPanel
  148. * @module material.components.panel
  149. *
  150. * @description
  151. * `$mdPanel` is a robust, low-level service for creating floating panels on
  152. * the screen. It can be used to implement tooltips, dialogs, pop-ups, etc.
  153. *
  154. * The following types, referenced below, have separate documentation:
  155. * - <a href="api/type/MdPanelAnimation">MdPanelAnimation</a> from `$mdPanel.newPanelAnimation()`
  156. * - <a href="api/type/MdPanelPosition">MdPanelPosition</a> from `$mdPanel.newPanelPosition()`
  157. * - <a href="api/type/MdPanelRef">MdPanelRef</a> from the `$mdPanel.open()` Promise or
  158. * injected in the panel's controller
  159. *
  160. * @usage
  161. * <hljs lang="js">
  162. * (function(angular, undefined) {
  163. * 'use strict';
  164. *
  165. * angular
  166. * .module('demoApp', ['ngMaterial'])
  167. * .controller('DemoDialogController', DialogController)
  168. * .controller('DemoCtrl', function($mdPanel) {
  169. *
  170. * var panelRef;
  171. *
  172. * function showPanel($event) {
  173. * var panelPosition = $mdPanel.newPanelPosition()
  174. * .absolute()
  175. * .top('50%')
  176. * .left('50%');
  177. *
  178. * var panelAnimation = $mdPanel.newPanelAnimation()
  179. * .openFrom($event)
  180. * .duration(200)
  181. * .closeTo('.show-button')
  182. * .withAnimation($mdPanel.animation.SCALE);
  183. *
  184. * var config = {
  185. * attachTo: angular.element(document.body),
  186. * controller: DialogController,
  187. * controllerAs: 'ctrl',
  188. * position: panelPosition,
  189. * animation: panelAnimation,
  190. * targetEvent: $event,
  191. * templateUrl: 'dialog-template.html',
  192. * clickOutsideToClose: true,
  193. * escapeToClose: true,
  194. * focusOnOpen: true
  195. * };
  196. *
  197. * $mdPanel.open(config)
  198. * .then(function(result) {
  199. * panelRef = result;
  200. * });
  201. * }
  202. * }
  203. *
  204. * function DialogController(MdPanelRef) {
  205. * function closeDialog() {
  206. * if (MdPanelRef) MdPanelRef.close();
  207. * }
  208. * }
  209. * })(angular);
  210. * </hljs>
  211. */
  212. /**
  213. * @ngdoc method
  214. * @name $mdPanel#create
  215. * @description
  216. * Creates a panel with the specified options.
  217. *
  218. * @param config {!Object=} Specific configuration object that may contain the
  219. * following properties:
  220. *
  221. * - `id` - `{string=}`: An ID to track the panel by. When an ID is provided,
  222. * the created panel is added to a tracked panels object. Any subsequent
  223. * requests made to create a panel with that ID are ignored. This is useful
  224. * in having the panel service not open multiple panels from the same user
  225. * interaction when there is no backdrop and events are propagated. Defaults
  226. * to an arbitrary string that is not tracked.
  227. * - `template` - `{string=}`: HTML template to show in the panel. This
  228. * **must** be trusted HTML with respect to AngularJS’s
  229. * [$sce service](https://docs.angularjs.org/api/ng/service/$sce).
  230. * - `templateUrl` - `{string=}`: The URL that will be used as the content of
  231. * the panel.
  232. * - `contentElement` - `{(string|!angular.JQLite|!Element)=}`: Pre-compiled
  233. * element to be used as the panel's content.
  234. * - `controller` - `{(function|string)=}`: The controller to associate with
  235. * the panel. The controller can inject a reference to the returned
  236. * panelRef, which allows the panel to be closed, hidden, and shown. Any
  237. * fields passed in through locals or resolve will be bound to the
  238. * controller.
  239. * - `controllerAs` - `{string=}`: An alias to assign the controller to on
  240. * the scope.
  241. * - `bindToController` - `{boolean=}`: Binds locals to the controller
  242. * instead of passing them in. Defaults to true, as this is a best
  243. * practice.
  244. * - `locals` - `{Object=}`: An object containing key/value pairs. The keys
  245. * will be used as names of values to inject into the controller. For
  246. * example, `locals: {three: 3}` would inject `three` into the controller,
  247. * with the value 3. 'mdPanelRef' is a reserved key, and will always
  248. * be set to the created MdPanelRef instance.
  249. * - `resolve` - `{Object=}`: Similar to locals, except it takes promises as
  250. * values. The panel will not open until all of the promises resolve.
  251. * - `attachTo` - `{(string|!angular.JQLite|!Element)=}`: The element to
  252. * attach the panel to. Defaults to appending to the root element of the
  253. * application.
  254. * - `propagateContainerEvents` - `{boolean=}`: Whether pointer or touch
  255. * events should be allowed to propagate 'go through' the container, aka the
  256. * wrapper, of the panel. Defaults to false.
  257. * - `panelClass` - `{string=}`: A css class to apply to the panel element.
  258. * This class should define any borders, box-shadow, etc. for the panel.
  259. * - `zIndex` - `{number=}`: The z-index to place the panel at.
  260. * Defaults to 80.
  261. * - `position` - `{MdPanelPosition=}`: An MdPanelPosition object that
  262. * specifies the alignment of the panel. For more information, see
  263. * `MdPanelPosition`.
  264. * - `clickOutsideToClose` - `{boolean=}`: Whether the user can click
  265. * outside the panel to close it. Defaults to false.
  266. * - `escapeToClose` - `{boolean=}`: Whether the user can press escape to
  267. * close the panel. Defaults to false.
  268. * - `onCloseSuccess` - `{function(!panelRef, string)=}`: Function that is
  269. * called after the close successfully finishes. The first parameter passed
  270. * into this function is the current panelRef and the 2nd is an optional
  271. * string explaining the close reason. The currently supported closeReasons
  272. * can be found in the MdPanelRef.closeReasons enum. These are by default
  273. * passed along by the panel.
  274. * - `trapFocus` - `{boolean=}`: Whether focus should be trapped within the
  275. * panel. If `trapFocus` is true, the user will not be able to interact
  276. * with the rest of the page until the panel is dismissed. Defaults to
  277. * false.
  278. * - `focusOnOpen` - `{boolean=}`: An option to override focus behavior on
  279. * open. Only disable if focusing some other way, as focus management is
  280. * required for panels to be accessible. Defaults to true.
  281. * - `fullscreen` - `{boolean=}`: Whether the panel should be full screen.
  282. * Applies the class `._md-panel-fullscreen` to the panel on open. Defaults
  283. * to false.
  284. * - `animation` - `{MdPanelAnimation=}`: An MdPanelAnimation object that
  285. * specifies the animation of the panel. For more information, see
  286. * `MdPanelAnimation`.
  287. * - `hasBackdrop` - `{boolean=}`: Whether there should be an opaque backdrop
  288. * behind the panel. Defaults to false.
  289. * - `disableParentScroll` - `{boolean=}`: Whether the user can scroll the
  290. * page behind the panel. Defaults to false.
  291. * - `onDomAdded` - `{function=}`: Callback function used to announce when
  292. * the panel is added to the DOM.
  293. * - `onOpenComplete` - `{function=}`: Callback function used to announce
  294. * when the open() action is finished.
  295. * - `onRemoving` - `{function=}`: Callback function used to announce the
  296. * close/hide() action is starting.
  297. * - `onDomRemoved` - `{function=}`: Callback function used to announce when
  298. * the panel is removed from the DOM.
  299. * - `origin` - `{(string|!angular.JQLite|!Element)=}`: The element to focus
  300. * on when the panel closes. This is commonly the element which triggered
  301. * the opening of the panel. If you do not use `origin`, you need to control
  302. * the focus manually.
  303. * - `groupName` - `{(string|!Array<string>)=}`: A group name or an array of
  304. * group names. The group name is used for creating a group of panels. The
  305. * group is used for configuring the number of open panels and identifying
  306. * specific behaviors for groups. For instance, all tooltips could be
  307. * identified using the same groupName.
  308. *
  309. * @returns {!MdPanelRef} panelRef
  310. */
  311. /**
  312. * @ngdoc method
  313. * @name $mdPanel#open
  314. * @description
  315. * Calls the create method above, then opens the panel. This is a shortcut for
  316. * creating and then calling open manually. If custom methods need to be
  317. * called when the panel is added to the DOM or opened, do not use this method.
  318. * Instead create the panel, chain promises on the domAdded and openComplete
  319. * methods, and call open from the returned panelRef.
  320. *
  321. * @param {!Object=} config Specific configuration object that may contain
  322. * the properties defined in `$mdPanel.create`.
  323. * @returns {!angular.$q.Promise<!MdPanelRef>} panelRef A promise that resolves
  324. * to an instance of the panel.
  325. */
  326. /**
  327. * @ngdoc method
  328. * @name $mdPanel#newPanelPosition
  329. * @description
  330. * Returns a new instance of the MdPanelPosition object. Use this to create
  331. * the position config object.
  332. *
  333. * @returns {!MdPanelPosition} panelPosition
  334. */
  335. /**
  336. * @ngdoc method
  337. * @name $mdPanel#newPanelAnimation
  338. * @description
  339. * Returns a new instance of the MdPanelAnimation object. Use this to create
  340. * the animation config object.
  341. *
  342. * @returns {!MdPanelAnimation} panelAnimation
  343. */
  344. /**
  345. * @ngdoc method
  346. * @name $mdPanel#newPanelGroup
  347. * @description
  348. * Creates a panel group and adds it to a tracked list of panel groups.
  349. *
  350. * @param {string} groupName Name of the group to create.
  351. * @param {!Object=} config Specific configuration object that may contain the
  352. * following properties:
  353. *
  354. * - `maxOpen` - `{number=}`: The maximum number of panels that are allowed to
  355. * be open within a defined panel group.
  356. *
  357. * @returns {!Object<string,
  358. * {panels: !Array<!MdPanelRef>,
  359. * openPanels: !Array<!MdPanelRef>,
  360. * maxOpen: number}>} panelGroup
  361. */
  362. /**
  363. * @ngdoc method
  364. * @name $mdPanel#setGroupMaxOpen
  365. * @description
  366. * Sets the maximum number of panels in a group that can be opened at a given
  367. * time.
  368. *
  369. * @param {string} groupName The name of the group to configure.
  370. * @param {number} maxOpen The maximum number of panels that can be
  371. * opened. Infinity can be passed in to remove the maxOpen limit.
  372. */
  373. /*****************************************************************************
  374. * MdPanelRef *
  375. *****************************************************************************/
  376. /**
  377. * @ngdoc type
  378. * @name MdPanelRef
  379. * @module material.components.panel
  380. * @description
  381. * A reference to a created panel. This reference contains a unique id for the
  382. * panel, along with the following properties:
  383. *
  384. * - `id` - `{string}`: The unique id for the panel. This id is used to track
  385. * when a panel was interacted with.
  386. * - `config` - `{!Object=}`: The entire config object that was used in
  387. * create.
  388. * - `isAttached` - `{boolean}`: Whether the panel is attached to the DOM.
  389. * Visibility to the user does not factor into isAttached.
  390. * - `panelContainer` - `{angular.JQLite}`: The wrapper element containing the
  391. * panel. This property is added in order to have access to the `addClass`,
  392. * `removeClass`, `toggleClass`, etc methods.
  393. * - `panelEl` - `{angular.JQLite}`: The panel element. This property is added
  394. * in order to have access to the `addClass`, `removeClass`, `toggleClass`,
  395. * etc methods.
  396. */
  397. /**
  398. * @ngdoc method
  399. * @name MdPanelRef#open
  400. * @description
  401. * Attaches and shows the panel.
  402. *
  403. * @returns {!angular.$q.Promise} A promise that is resolved when the panel is
  404. * opened.
  405. */
  406. /**
  407. * @ngdoc method
  408. * @name MdPanelRef#close
  409. * @description
  410. * Hides and detaches the panel. Note that this will **not** destroy the panel.
  411. * If you don't intend on using the panel again, call the {@link #destroy
  412. * destroy} method afterwards.
  413. *
  414. * @returns {!angular.$q.Promise} A promise that is resolved when the panel is
  415. * closed.
  416. */
  417. /**
  418. * @ngdoc method
  419. * @name MdPanelRef#attach
  420. * @description
  421. * Create the panel elements and attach them to the DOM. The panel will be
  422. * hidden by default.
  423. *
  424. * @returns {!angular.$q.Promise} A promise that is resolved when the panel is
  425. * attached.
  426. */
  427. /**
  428. * @ngdoc method
  429. * @name MdPanelRef#detach
  430. * @description
  431. * Removes the panel from the DOM. This will NOT hide the panel before removing
  432. * it.
  433. *
  434. * @returns {!angular.$q.Promise} A promise that is resolved when the panel is
  435. * detached.
  436. */
  437. /**
  438. * @ngdoc method
  439. * @name MdPanelRef#show
  440. * @description
  441. * Shows the panel.
  442. *
  443. * @returns {!angular.$q.Promise} A promise that is resolved when the panel has
  444. * shown and animations are completed.
  445. */
  446. /**
  447. * @ngdoc method
  448. * @name MdPanelRef#hide
  449. * @description
  450. * Hides the panel.
  451. *
  452. * @returns {!angular.$q.Promise} A promise that is resolved when the panel has
  453. * hidden and animations are completed.
  454. */
  455. /**
  456. * @ngdoc method
  457. * @name MdPanelRef#destroy
  458. * @description
  459. * Destroys the panel. The panel cannot be opened again after this is called.
  460. */
  461. /**
  462. * @ngdoc method
  463. * @name MdPanelRef#addClass
  464. * @deprecated
  465. * This method is in the process of being deprecated in favor of using the panel
  466. * and container JQLite elements that are referenced in the MdPanelRef object.
  467. * Full deprecation is scheduled for material 1.2.
  468. * @description
  469. * Adds a class to the panel. DO NOT use this hide/show the panel.
  470. *
  471. * @param {string} newClass class to be added.
  472. * @param {boolean} toElement Whether or not to add the class to the panel
  473. * element instead of the container.
  474. */
  475. /**
  476. * @ngdoc method
  477. * @name MdPanelRef#removeClass
  478. * @deprecated
  479. * This method is in the process of being deprecated in favor of using the panel
  480. * and container JQLite elements that are referenced in the MdPanelRef object.
  481. * Full deprecation is scheduled for material 1.2.
  482. * @description
  483. * Removes a class from the panel. DO NOT use this to hide/show the panel.
  484. *
  485. * @param {string} oldClass Class to be removed.
  486. * @param {boolean} fromElement Whether or not to remove the class from the
  487. * panel element instead of the container.
  488. */
  489. /**
  490. * @ngdoc method
  491. * @name MdPanelRef#toggleClass
  492. * @deprecated
  493. * This method is in the process of being deprecated in favor of using the panel
  494. * and container JQLite elements that are referenced in the MdPanelRef object.
  495. * Full deprecation is scheduled for material 1.2.
  496. * @description
  497. * Toggles a class on the panel. DO NOT use this to hide/show the panel.
  498. *
  499. * @param {string} toggleClass Class to be toggled.
  500. * @param {boolean} onElement Whether or not to remove the class from the panel
  501. * element instead of the container.
  502. */
  503. /**
  504. * @ngdoc method
  505. * @name MdPanelRef#updatePosition
  506. * @description
  507. * Updates the position configuration of a panel. Use this to update the
  508. * position of a panel that is open, without having to close and re-open the
  509. * panel.
  510. *
  511. * @param {!MdPanelPosition} position
  512. */
  513. /**
  514. * @ngdoc method
  515. * @name MdPanelRef#addToGroup
  516. * @description
  517. * Adds a panel to a group if the panel does not exist within the group already.
  518. * A panel can only exist within a single group.
  519. *
  520. * @param {string} groupName The name of the group to add the panel to.
  521. */
  522. /**
  523. * @ngdoc method
  524. * @name MdPanelRef#removeFromGroup
  525. * @description
  526. * Removes a panel from a group if the panel exists within that group. The group
  527. * must be created ahead of time.
  528. *
  529. * @param {string} groupName The name of the group.
  530. */
  531. /**
  532. * @ngdoc method
  533. * @name MdPanelRef#registerInterceptor
  534. * @description
  535. * Registers an interceptor with the panel. The callback should return a promise,
  536. * which will allow the action to continue when it gets resolved, or will
  537. * prevent an action if it is rejected. The interceptors are called sequentially
  538. * and it reverse order. `type` must be one of the following
  539. * values available on `$mdPanel.interceptorTypes`:
  540. * * `CLOSE` - Gets called before the panel begins closing.
  541. *
  542. * @param {string} type Type of interceptor.
  543. * @param {!angular.$q.Promise<any>} callback Callback to be registered.
  544. * @returns {!MdPanelRef}
  545. */
  546. /**
  547. * @ngdoc method
  548. * @name MdPanelRef#removeInterceptor
  549. * @description
  550. * Removes a registered interceptor.
  551. *
  552. * @param {string} type Type of interceptor to be removed.
  553. * @param {function(): !angular.$q.Promise<any>} callback Interceptor to be removed.
  554. * @returns {!MdPanelRef}
  555. */
  556. /**
  557. * @ngdoc method
  558. * @name MdPanelRef#removeAllInterceptors
  559. * @description
  560. * Removes all interceptors. If a type is supplied, only the
  561. * interceptors of that type will be cleared.
  562. *
  563. * @param {string=} type Type of interceptors to be removed.
  564. * @returns {!MdPanelRef}
  565. */
  566. /**
  567. * @ngdoc method
  568. * @name MdPanelRef#updateAnimation
  569. * @description
  570. * Updates the animation configuration for a panel. You can use this to change
  571. * the panel's animation without having to re-create it.
  572. *
  573. * @param {!MdPanelAnimation} animation
  574. */
  575. /*****************************************************************************
  576. * MdPanelPosition *
  577. *****************************************************************************/
  578. /**
  579. * @ngdoc type
  580. * @name MdPanelPosition
  581. * @module material.components.panel
  582. * @description
  583. *
  584. * Object for configuring the position of the panel.
  585. *
  586. * @usage
  587. *
  588. * #### Centering the panel
  589. *
  590. * <hljs lang="js">
  591. * new MdPanelPosition().absolute().center();
  592. * </hljs>
  593. *
  594. * #### Overlapping the panel with an element
  595. *
  596. * <hljs lang="js">
  597. * new MdPanelPosition()
  598. * .relativeTo(someElement)
  599. * .addPanelPosition(
  600. * $mdPanel.xPosition.ALIGN_START,
  601. * $mdPanel.yPosition.ALIGN_TOPS
  602. * );
  603. * </hljs>
  604. *
  605. * #### Aligning the panel with the bottom of an element
  606. *
  607. * <hljs lang="js">
  608. * new MdPanelPosition()
  609. * .relativeTo(someElement)
  610. * .addPanelPosition($mdPanel.xPosition.CENTER, $mdPanel.yPosition.BELOW);
  611. * </hljs>
  612. */
  613. /**
  614. * @ngdoc method
  615. * @name MdPanelPosition#absolute
  616. * @description
  617. * Positions the panel absolutely relative to the parent element. If the parent
  618. * is document.body, this is equivalent to positioning the panel absolutely
  619. * within the viewport.
  620. *
  621. * @returns {!MdPanelPosition}
  622. */
  623. /**
  624. * @ngdoc method
  625. * @name MdPanelPosition#relativeTo
  626. * @description
  627. * Positions the panel relative to a specific element.
  628. *
  629. * @param {string|!Element|!angular.JQLite} element Query selector, DOM element,
  630. * or angular element to position the panel with respect to.
  631. * @returns {!MdPanelPosition}
  632. */
  633. /**
  634. * @ngdoc method
  635. * @name MdPanelPosition#top
  636. * @description
  637. * Sets the value of `top` for the panel. Clears any previously set vertical
  638. * position.
  639. *
  640. * @param {string=} top Value of `top`. Defaults to '0'.
  641. * @returns {!MdPanelPosition}
  642. */
  643. /**
  644. * @ngdoc method
  645. * @name MdPanelPosition#bottom
  646. * @description
  647. * Sets the value of `bottom` for the panel. Clears any previously set vertical
  648. * position.
  649. *
  650. * @param {string=} bottom Value of `bottom`. Defaults to '0'.
  651. * @returns {!MdPanelPosition}
  652. */
  653. /**
  654. * @ngdoc method
  655. * @name MdPanelPosition#start
  656. * @description
  657. * Sets the panel to the start of the page - `left` if `ltr` or `right` for
  658. * `rtl`. Clears any previously set horizontal position.
  659. *
  660. * @param {string=} start Value of position. Defaults to '0'.
  661. * @returns {!MdPanelPosition}
  662. */
  663. /**
  664. * @ngdoc method
  665. * @name MdPanelPosition#end
  666. * @description
  667. * Sets the panel to the end of the page - `right` if `ltr` or `left` for `rtl`.
  668. * Clears any previously set horizontal position.
  669. *
  670. * @param {string=} end Value of position. Defaults to '0'.
  671. * @returns {!MdPanelPosition}
  672. */
  673. /**
  674. * @ngdoc method
  675. * @name MdPanelPosition#left
  676. * @description
  677. * Sets the value of `left` for the panel. Clears any previously set
  678. * horizontal position.
  679. *
  680. * @param {string=} left Value of `left`. Defaults to '0'.
  681. * @returns {!MdPanelPosition}
  682. */
  683. /**
  684. * @ngdoc method
  685. * @name MdPanelPosition#right
  686. * @description
  687. * Sets the value of `right` for the panel. Clears any previously set
  688. * horizontal position.
  689. *
  690. * @param {string=} right Value of `right`. Defaults to '0'.
  691. * @returns {!MdPanelPosition}
  692. */
  693. /**
  694. * @ngdoc method
  695. * @name MdPanelPosition#centerHorizontally
  696. * @description
  697. * Centers the panel horizontally in the viewport. Clears any previously set
  698. * horizontal position.
  699. *
  700. * @returns {!MdPanelPosition}
  701. */
  702. /**
  703. * @ngdoc method
  704. * @name MdPanelPosition#centerVertically
  705. * @description
  706. * Centers the panel vertically in the viewport. Clears any previously set
  707. * vertical position.
  708. *
  709. * @returns {!MdPanelPosition}
  710. */
  711. /**
  712. * @ngdoc method
  713. * @name MdPanelPosition#center
  714. * @description
  715. * Centers the panel horizontally and vertically in the viewport. This is
  716. * equivalent to calling both `centerHorizontally` and `centerVertically`.
  717. * Clears any previously set horizontal and vertical positions.
  718. *
  719. * @returns {!MdPanelPosition}
  720. */
  721. /**
  722. * @ngdoc method
  723. * @name MdPanelPosition#addPanelPosition
  724. * @description
  725. * Sets the x and y position for the panel relative to another element. Can be
  726. * called multiple times to specify an ordered list of panel positions. The
  727. * first position which allows the panel to be completely on-screen will be
  728. * chosen; the last position will be chose whether it is on-screen or not.
  729. *
  730. * xPosition must be one of the following values available on
  731. * $mdPanel.xPosition:
  732. *
  733. *
  734. * CENTER | ALIGN_START | ALIGN_END | OFFSET_START | OFFSET_END
  735. *
  736. * <pre>
  737. * *************
  738. * * *
  739. * * PANEL *
  740. * * *
  741. * *************
  742. * A B C D E
  743. *
  744. * A: OFFSET_START (for LTR displays)
  745. * B: ALIGN_START (for LTR displays)
  746. * C: CENTER
  747. * D: ALIGN_END (for LTR displays)
  748. * E: OFFSET_END (for LTR displays)
  749. * </pre>
  750. *
  751. * yPosition must be one of the following values available on
  752. * $mdPanel.yPosition:
  753. *
  754. * CENTER | ALIGN_TOPS | ALIGN_BOTTOMS | ABOVE | BELOW
  755. *
  756. * <pre>
  757. * F
  758. * G *************
  759. * * *
  760. * H * PANEL *
  761. * * *
  762. * I *************
  763. * J
  764. *
  765. * F: BELOW
  766. * G: ALIGN_TOPS
  767. * H: CENTER
  768. * I: ALIGN_BOTTOMS
  769. * J: ABOVE
  770. * </pre>
  771. *
  772. * @param {string} xPosition
  773. * @param {string} yPosition
  774. * @returns {!MdPanelPosition}
  775. */
  776. /**
  777. * @ngdoc method
  778. * @name MdPanelPosition#withOffsetX
  779. * @description
  780. * Sets the value of the offset in the x-direction.
  781. *
  782. * @param {string|number} offsetX
  783. * @returns {!MdPanelPosition}
  784. */
  785. /**
  786. * @ngdoc method
  787. * @name MdPanelPosition#withOffsetY
  788. * @description
  789. * Sets the value of the offset in the y-direction.
  790. *
  791. * @param {string|number} offsetY
  792. * @returns {!MdPanelPosition}
  793. */
  794. /*****************************************************************************
  795. * MdPanelAnimation *
  796. *****************************************************************************/
  797. /**
  798. * @ngdoc type
  799. * @name MdPanelAnimation
  800. * @module material.components.panel
  801. * @description
  802. * Animation configuration object. To use, create an MdPanelAnimation with the
  803. * desired properties, then pass the object as part of $mdPanel creation.
  804. *
  805. * @usage
  806. *
  807. * <hljs lang="js">
  808. * var panelAnimation = new MdPanelAnimation()
  809. * .openFrom(myButtonEl)
  810. * .duration(1337)
  811. * .closeTo('.my-button')
  812. * .withAnimation($mdPanel.animation.SCALE);
  813. *
  814. * $mdPanel.create({
  815. * animation: panelAnimation
  816. * });
  817. * </hljs>
  818. */
  819. /**
  820. * @ngdoc method
  821. * @name MdPanelAnimation#openFrom
  822. * @description
  823. * Specifies where to start the open animation. `openFrom` accepts a
  824. * click event object, query selector, DOM element, or a Rect object that
  825. * is used to determine the bounds. When passed a click event, the location
  826. * of the click will be used as the position to start the animation.
  827. *
  828. * @param {string|!Element|!Event|{top: number, left: number}}
  829. * @returns {!MdPanelAnimation}
  830. */
  831. /**
  832. * @ngdoc method
  833. * @name MdPanelAnimation#closeTo
  834. * @description
  835. * Specifies where to animate the panel close. `closeTo` accepts a
  836. * query selector, DOM element, or a Rect object that is used to determine
  837. * the bounds.
  838. *
  839. * @param {string|!Element|{top: number, left: number}}
  840. * @returns {!MdPanelAnimation}
  841. */
  842. /**
  843. * @ngdoc method
  844. * @name MdPanelAnimation#withAnimation
  845. * @description
  846. * Specifies the animation class.
  847. *
  848. * There are several default animations that can be used: `$mdPanel.animation.`
  849. * - `SLIDE`: The panel slides in and out from the specified
  850. * elements. It will not fade in or out.
  851. * - `SCALE`: The panel scales in and out. Slide and fade are
  852. * included in this animation.
  853. * - `FADE`: The panel fades in and out.
  854. *
  855. * Custom classes will by default fade in and out unless
  856. * `transition: opacity 1ms` is added to the to custom class.
  857. *
  858. * @param {string|{open: string, close: string}} cssClass
  859. * @returns {!MdPanelAnimation}
  860. */
  861. /**
  862. * @ngdoc method
  863. * @name MdPanelAnimation#duration
  864. * @description
  865. * Specifies the duration of the animation in milliseconds. The `duration`
  866. * method accepts either a number or an object with separate open and close
  867. * durations.
  868. *
  869. * @param {number|{open: number, close: number}} duration
  870. * @returns {!MdPanelAnimation}
  871. */
  872. /*****************************************************************************
  873. * PUBLIC DOCUMENTATION *
  874. *****************************************************************************/
  875. var MD_PANEL_Z_INDEX = 80;
  876. var MD_PANEL_HIDDEN = '_md-panel-hidden';
  877. var FOCUS_TRAP_TEMPLATE = angular.element(
  878. '<div class="_md-panel-focus-trap" tabindex="0"></div>');
  879. var _presets = {};
  880. /**
  881. * A provider that is used for creating presets for the panel API.
  882. * @final @constructor ngInject
  883. */
  884. function MdPanelProvider() {
  885. return {
  886. 'definePreset': definePreset,
  887. 'getAllPresets': getAllPresets,
  888. 'clearPresets': clearPresets,
  889. '$get': $getProvider()
  890. };
  891. }
  892. /**
  893. * Takes the passed in panel configuration object and adds it to the `_presets`
  894. * object at the specified name.
  895. * @param {string} name Name of the preset to set.
  896. * @param {!Object} preset Specific configuration object that can contain any
  897. * and all of the parameters available within the `$mdPanel.create` method.
  898. * However, parameters that pertain to id, position, animation, and user
  899. * interaction are not allowed and will be removed from the preset
  900. * configuration.
  901. */
  902. function definePreset(name, preset) {
  903. if (!name || !preset) {
  904. throw new Error('mdPanelProvider: The panel preset definition is ' +
  905. 'malformed. The name and preset object are required.');
  906. } else if (_presets.hasOwnProperty(name)) {
  907. throw new Error('mdPanelProvider: The panel preset you have requested ' +
  908. 'has already been defined.');
  909. }
  910. // Delete any property on the preset that is not allowed.
  911. delete preset.id;
  912. delete preset.position;
  913. delete preset.animation;
  914. _presets[name] = preset;
  915. }
  916. /**
  917. * Gets a clone of the `_presets`.
  918. * @return {!Object}
  919. */
  920. function getAllPresets() {
  921. return angular.copy(_presets);
  922. }
  923. /**
  924. * Clears all of the stored presets.
  925. */
  926. function clearPresets() {
  927. _presets = {};
  928. }
  929. /**
  930. * Represents the `$get` method of the AngularJS provider. From here, a new
  931. * reference to the MdPanelService is returned where the needed arguments are
  932. * passed in including the MdPanelProvider `_presets`.
  933. * @param {!Object} _presets
  934. * @param {!angular.JQLite} $rootElement
  935. * @param {!angular.Scope} $rootScope
  936. * @param {!angular.$injector} $injector
  937. * @param {!angular.$window} $window
  938. */
  939. function $getProvider() {
  940. return [
  941. '$rootElement', '$rootScope', '$injector', '$window',
  942. function($rootElement, $rootScope, $injector, $window) {
  943. return new MdPanelService(_presets, $rootElement, $rootScope,
  944. $injector, $window);
  945. }
  946. ];
  947. }
  948. /*****************************************************************************
  949. * MdPanel Service *
  950. *****************************************************************************/
  951. /**
  952. * A service that is used for controlling/displaying panels on the screen.
  953. * @param {!Object} presets
  954. * @param {!angular.JQLite} $rootElement
  955. * @param {!angular.Scope} $rootScope
  956. * @param {!angular.$injector} $injector
  957. * @param {!angular.$window} $window
  958. * @final @constructor ngInject
  959. */
  960. function MdPanelService(presets, $rootElement, $rootScope, $injector, $window) {
  961. /**
  962. * Default config options for the panel.
  963. * Anything angular related needs to be done later. Therefore
  964. * scope: $rootScope.$new(true),
  965. * attachTo: $rootElement,
  966. * are added later.
  967. * @private {!Object}
  968. */
  969. this._defaultConfigOptions = {
  970. bindToController: true,
  971. clickOutsideToClose: false,
  972. disableParentScroll: false,
  973. escapeToClose: false,
  974. focusOnOpen: true,
  975. fullscreen: false,
  976. hasBackdrop: false,
  977. propagateContainerEvents: false,
  978. transformTemplate: angular.bind(this, this._wrapTemplate),
  979. trapFocus: false,
  980. zIndex: MD_PANEL_Z_INDEX
  981. };
  982. /** @private {!Object} */
  983. this._config = {};
  984. /** @private {!Object} */
  985. this._presets = presets;
  986. /** @private @const */
  987. this._$rootElement = $rootElement;
  988. /** @private @const */
  989. this._$rootScope = $rootScope;
  990. /** @private @const */
  991. this._$injector = $injector;
  992. /** @private @const */
  993. this._$window = $window;
  994. /** @private @const */
  995. this._$mdUtil = this._$injector.get('$mdUtil');
  996. /** @private {!Object<string, !MdPanelRef>} */
  997. this._trackedPanels = {};
  998. /**
  999. * @private {!Object<string,
  1000. * {panels: !Array<!MdPanelRef>,
  1001. * openPanels: !Array<!MdPanelRef>,
  1002. * maxOpen: number}>}
  1003. */
  1004. this._groups = Object.create(null);
  1005. /**
  1006. * Default animations that can be used within the panel.
  1007. * @type {enum}
  1008. */
  1009. this.animation = MdPanelAnimation.animation;
  1010. /**
  1011. * Possible values of xPosition for positioning the panel relative to
  1012. * another element.
  1013. * @type {enum}
  1014. */
  1015. this.xPosition = MdPanelPosition.xPosition;
  1016. /**
  1017. * Possible values of yPosition for positioning the panel relative to
  1018. * another element.
  1019. * @type {enum}
  1020. */
  1021. this.yPosition = MdPanelPosition.yPosition;
  1022. /**
  1023. * Possible values for the interceptors that can be registered on a panel.
  1024. * @type {enum}
  1025. */
  1026. this.interceptorTypes = MdPanelRef.interceptorTypes;
  1027. /**
  1028. * Possible values for closing of a panel.
  1029. * @type {enum}
  1030. */
  1031. this.closeReasons = MdPanelRef.closeReasons;
  1032. /**
  1033. * Possible values of absolute position.
  1034. * @type {enum}
  1035. */
  1036. this.absPosition = MdPanelPosition.absPosition;
  1037. }
  1038. /**
  1039. * Creates a panel with the specified options.
  1040. * @param {string=} preset Name of a preset configuration that can be used to
  1041. * extend the panel configuration.
  1042. * @param {!Object=} config Configuration object for the panel.
  1043. * @returns {!MdPanelRef}
  1044. */
  1045. MdPanelService.prototype.create = function(preset, config) {
  1046. if (typeof preset === 'string') {
  1047. preset = this._getPresetByName(preset);
  1048. } else if (typeof preset === 'object' &&
  1049. (angular.isUndefined(config) || !config)) {
  1050. config = preset;
  1051. preset = {};
  1052. }
  1053. preset = preset || {};
  1054. config = config || {};
  1055. // If the passed-in config contains an ID and the ID is within _trackedPanels,
  1056. // return the tracked panel after updating its config with the passed-in
  1057. // config.
  1058. if (angular.isDefined(config.id) && this._trackedPanels[config.id]) {
  1059. var trackedPanel = this._trackedPanels[config.id];
  1060. angular.extend(trackedPanel.config, config);
  1061. return trackedPanel;
  1062. }
  1063. // Combine the passed-in config, the _defaultConfigOptions, and the preset
  1064. // configuration into the `_config`.
  1065. this._config = angular.extend({
  1066. // If no ID is set within the passed-in config, then create an arbitrary ID.
  1067. id: config.id || 'panel_' + this._$mdUtil.nextUid(),
  1068. scope: this._$rootScope.$new(true),
  1069. attachTo: this._$rootElement
  1070. }, this._defaultConfigOptions, config, preset);
  1071. // Create the panelRef and add it to the `_trackedPanels` object.
  1072. var panelRef = new MdPanelRef(this._config, this._$injector);
  1073. this._trackedPanels[config.id] = panelRef;
  1074. // Add the panel to each of its requested groups.
  1075. if (this._config.groupName) {
  1076. if (angular.isString(this._config.groupName)) {
  1077. this._config.groupName = [this._config.groupName];
  1078. }
  1079. angular.forEach(this._config.groupName, function(group) {
  1080. panelRef.addToGroup(group);
  1081. });
  1082. }
  1083. this._config.scope.$on('$destroy', angular.bind(panelRef, panelRef.detach));
  1084. return panelRef;
  1085. };
  1086. /**
  1087. * Creates and opens a panel with the specified options.
  1088. * @param {string=} preset Name of a preset configuration that can be used to
  1089. * extend the panel configuration.
  1090. * @param {!Object=} config Configuration object for the panel.
  1091. * @returns {!angular.$q.Promise<!MdPanelRef>} The panel created from create.
  1092. */
  1093. MdPanelService.prototype.open = function(preset, config) {
  1094. var panelRef = this.create(preset, config);
  1095. return panelRef.open().then(function() {
  1096. return panelRef;
  1097. });
  1098. };
  1099. /**
  1100. * Gets a specific preset configuration object saved within `_presets`.
  1101. * @param {string} preset Name of the preset to search for.
  1102. * @returns {!Object} The preset configuration object.
  1103. */
  1104. MdPanelService.prototype._getPresetByName = function(preset) {
  1105. if (!this._presets[preset]) {
  1106. throw new Error('mdPanel: The panel preset configuration that you ' +
  1107. 'requested does not exist. Use the $mdPanelProvider to create a ' +
  1108. 'preset before requesting one.');
  1109. }
  1110. return this._presets[preset];
  1111. };
  1112. /**
  1113. * Returns a new instance of the MdPanelPosition. Use this to create the
  1114. * positioning object.
  1115. * @returns {!MdPanelPosition}
  1116. */
  1117. MdPanelService.prototype.newPanelPosition = function() {
  1118. return new MdPanelPosition(this._$injector);
  1119. };
  1120. /**
  1121. * Returns a new instance of the MdPanelAnimation. Use this to create the
  1122. * animation object.
  1123. * @returns {!MdPanelAnimation}
  1124. */
  1125. MdPanelService.prototype.newPanelAnimation = function() {
  1126. return new MdPanelAnimation(this._$injector);
  1127. };
  1128. /**
  1129. * Creates a panel group and adds it to a tracked list of panel groups.
  1130. * @param groupName {string} Name of the group to create.
  1131. * @param config {!Object=} Specific configuration object that may contain the
  1132. * following properties:
  1133. *
  1134. * - `maxOpen` - `{number=}`: The maximum number of panels that are allowed
  1135. * open within a defined panel group.
  1136. *
  1137. * @returns {!Object<string,
  1138. * {panels: !Array<!MdPanelRef>,
  1139. * openPanels: !Array<!MdPanelRef>,
  1140. * maxOpen: number}>} panelGroup
  1141. */
  1142. MdPanelService.prototype.newPanelGroup = function(groupName, config) {
  1143. if (!this._groups[groupName]) {
  1144. config = config || {};
  1145. var group = {
  1146. panels: [],
  1147. openPanels: [],
  1148. maxOpen: config.maxOpen > 0 ? config.maxOpen : Infinity
  1149. };
  1150. this._groups[groupName] = group;
  1151. }
  1152. return this._groups[groupName];
  1153. };
  1154. /**
  1155. * Sets the maximum number of panels in a group that can be opened at a given
  1156. * time.
  1157. * @param {string} groupName The name of the group to configure.
  1158. * @param {number} maxOpen The maximum number of panels that can be
  1159. * opened. Infinity can be passed in to remove the maxOpen limit.
  1160. */
  1161. MdPanelService.prototype.setGroupMaxOpen = function(groupName, maxOpen) {
  1162. if (this._groups[groupName]) {
  1163. this._groups[groupName].maxOpen = maxOpen;
  1164. } else {
  1165. throw new Error('mdPanel: Group does not exist yet. Call newPanelGroup().');
  1166. }
  1167. };
  1168. /**
  1169. * Determines if the current number of open panels within a group exceeds the
  1170. * limit of allowed open panels.
  1171. * @param {string} groupName The name of the group to check.
  1172. * @returns {boolean} true if open count does exceed maxOpen and false if not.
  1173. * @private
  1174. */
  1175. MdPanelService.prototype._openCountExceedsMaxOpen = function(groupName) {
  1176. if (this._groups[groupName]) {
  1177. var group = this._groups[groupName];
  1178. return group.maxOpen > 0 && group.openPanels.length > group.maxOpen;
  1179. }
  1180. return false;
  1181. };
  1182. /**
  1183. * Closes the first open panel within a specific group.
  1184. * @param {string} groupName The name of the group.
  1185. * @private
  1186. */
  1187. MdPanelService.prototype._closeFirstOpenedPanel = function(groupName) {
  1188. this._groups[groupName].openPanels[0].close();
  1189. };
  1190. /**
  1191. * Wraps the users template in two elements, md-panel-outer-wrapper, which
  1192. * covers the entire attachTo element, and md-panel, which contains only the
  1193. * template. This allows the panel control over positioning, animations,
  1194. * and similar properties.
  1195. * @param {string} origTemplate The original template.
  1196. * @returns {string} The wrapped template.
  1197. * @private
  1198. */
  1199. MdPanelService.prototype._wrapTemplate = function(origTemplate) {
  1200. var template = origTemplate || '';
  1201. // The panel should be initially rendered offscreen so we can calculate
  1202. // height and width for positioning.
  1203. return '' +
  1204. '<div class="md-panel-outer-wrapper">' +
  1205. ' <div class="md-panel _md-panel-offscreen">' + template + '</div>' +
  1206. '</div>';
  1207. };
  1208. /**
  1209. * Wraps a content element in a md-panel-outer wrapper and
  1210. * positions it off-screen. Allows for proper control over positoning
  1211. * and animations.
  1212. * @param {!angular.JQLite} contentElement Element to be wrapped.
  1213. * @return {!angular.JQLite} Wrapper element.
  1214. * @private
  1215. */
  1216. MdPanelService.prototype._wrapContentElement = function(contentElement) {
  1217. var wrapper = angular.element('<div class="md-panel-outer-wrapper">');
  1218. contentElement.addClass('md-panel _md-panel-offscreen');
  1219. wrapper.append(contentElement);
  1220. return wrapper;
  1221. };
  1222. /*****************************************************************************
  1223. * MdPanelRef *
  1224. *****************************************************************************/
  1225. /**
  1226. * A reference to a created panel. This reference contains a unique id for the
  1227. * panel, along with properties/functions used to control the panel.
  1228. * @param {!Object} config
  1229. * @param {!angular.$injector} $injector
  1230. * @final @constructor
  1231. */
  1232. function MdPanelRef(config, $injector) {
  1233. // Injected variables.
  1234. /** @private @const {!angular.$q} */
  1235. this._$q = $injector.get('$q');
  1236. /** @private @const {!angular.$mdCompiler} */
  1237. this._$mdCompiler = $injector.get('$mdCompiler');
  1238. /** @private @const {!angular.$mdConstant} */
  1239. this._$mdConstant = $injector.get('$mdConstant');
  1240. /** @private @const {!angular.$mdUtil} */
  1241. this._$mdUtil = $injector.get('$mdUtil');
  1242. /** @private @const {!angular.$mdTheming} */
  1243. this._$mdTheming = $injector.get('$mdTheming');
  1244. /** @private @const {!angular.Scope} */
  1245. this._$rootScope = $injector.get('$rootScope');
  1246. /** @private @const {!angular.$animate} */
  1247. this._$animate = $injector.get('$animate');
  1248. /** @private @const {!MdPanelRef} */
  1249. this._$mdPanel = $injector.get('$mdPanel');
  1250. /** @private @const {!angular.$log} */
  1251. this._$log = $injector.get('$log');
  1252. /** @private @const {!angular.$window} */
  1253. this._$window = $injector.get('$window');
  1254. /** @private @const {!Function} */
  1255. this._$$rAF = $injector.get('$$rAF');
  1256. // Public variables.
  1257. /**
  1258. * Unique id for the panelRef.
  1259. * @type {string}
  1260. */
  1261. this.id = config.id;
  1262. /** @type {!Object} */
  1263. this.config = config;
  1264. /** @type {!angular.JQLite|undefined} */
  1265. this.panelContainer;
  1266. /** @type {!angular.JQLite|undefined} */
  1267. this.panelEl;
  1268. /**
  1269. * Whether the panel is attached. This is synchronous. When attach is called,
  1270. * isAttached is set to true. When detach is called, isAttached is set to
  1271. * false.
  1272. * @type {boolean}
  1273. */
  1274. this.isAttached = false;
  1275. // Private variables.
  1276. /** @private {Array<function()>} */
  1277. this._removeListeners = [];
  1278. /** @private {!angular.JQLite|undefined} */
  1279. this._topFocusTrap;
  1280. /** @private {!angular.JQLite|undefined} */
  1281. this._bottomFocusTrap;
  1282. /** @private {!$mdPanel|undefined} */
  1283. this._backdropRef;
  1284. /** @private {Function?} */
  1285. this._restoreScroll = null;
  1286. /**
  1287. * Keeps track of all the panel interceptors.
  1288. * @private {!Object}
  1289. */
  1290. this._interceptors = Object.create(null);
  1291. /**
  1292. * Cleanup function, provided by `$mdCompiler` and assigned after the element
  1293. * has been compiled. When `contentElement` is used, the function is used to
  1294. * restore the element to it's proper place in the DOM.
  1295. * @private {!Function}
  1296. */
  1297. this._compilerCleanup = null;
  1298. /**
  1299. * Cache for saving and restoring element inline styles, CSS classes etc.
  1300. * @type {{styles: string, classes: string}}
  1301. */
  1302. this._restoreCache = {
  1303. styles: '',
  1304. classes: ''
  1305. };
  1306. }
  1307. MdPanelRef.interceptorTypes = {
  1308. CLOSE: 'onClose'
  1309. };
  1310. /**
  1311. * Opens an already created and configured panel. If the panel is already
  1312. * visible, does nothing.
  1313. * @returns {!angular.$q.Promise<!MdPanelRef>} A promise that is resolved when
  1314. * the panel is opened and animations finish.
  1315. */
  1316. MdPanelRef.prototype.open = function() {
  1317. var self = this;
  1318. return this._$q(function(resolve, reject) {
  1319. var done = self._done(resolve, self);
  1320. var show = self._simpleBind(self.show, self);
  1321. var checkGroupMaxOpen = function() {
  1322. if (self.config.groupName) {
  1323. angular.forEach(self.config.groupName, function(group) {
  1324. if (self._$mdPanel._openCountExceedsMaxOpen(group)) {
  1325. self._$mdPanel._closeFirstOpenedPanel(group);
  1326. }
  1327. });
  1328. }
  1329. };
  1330. self.attach()
  1331. .then(show)
  1332. .then(checkGroupMaxOpen)
  1333. .then(done)
  1334. .catch(reject);
  1335. });
  1336. };
  1337. /**
  1338. * Closes the panel.
  1339. * @param {string} closeReason The event type that triggered the close.
  1340. * @returns {!angular.$q.Promise<!MdPanelRef>} A promise that is resolved when
  1341. * the panel is closed and animations finish.
  1342. */
  1343. MdPanelRef.prototype.close = function(closeReason) {
  1344. var self = this;
  1345. return this._$q(function(resolve, reject) {
  1346. self._callInterceptors(MdPanelRef.interceptorTypes.CLOSE).then(function() {
  1347. var done = self._done(resolve, self);
  1348. var detach = self._simpleBind(self.detach, self);
  1349. var onCloseSuccess = self.config['onCloseSuccess'] || angular.noop;
  1350. onCloseSuccess = angular.bind(self, onCloseSuccess, self, closeReason);
  1351. self.hide()
  1352. .then(detach)
  1353. .then(done)
  1354. .then(onCloseSuccess)
  1355. .catch(reject);
  1356. }, reject);
  1357. });
  1358. };
  1359. /**
  1360. * Attaches the panel. The panel will be hidden afterwards.
  1361. * @returns {!angular.$q.Promise<!MdPanelRef>} A promise that is resolved when
  1362. * the panel is attached.
  1363. */
  1364. MdPanelRef.prototype.attach = function() {
  1365. if (this.isAttached && this.panelEl) {
  1366. return this._$q.when(this);
  1367. }
  1368. var self = this;
  1369. return this._$q(function(resolve, reject) {
  1370. var done = self._done(resolve, self);
  1371. var onDomAdded = self.config['onDomAdded'] || angular.noop;
  1372. var addListeners = function(response) {
  1373. self.isAttached = true;
  1374. self._addEventListeners();
  1375. return response;
  1376. };
  1377. self._$q.all([
  1378. self._createBackdrop(),
  1379. self._createPanel()
  1380. .then(addListeners)
  1381. .catch(reject)
  1382. ]).then(onDomAdded)
  1383. .then(done)
  1384. .catch(reject);
  1385. });
  1386. };
  1387. /**
  1388. * Only detaches the panel. Will NOT hide the panel first.
  1389. * @returns {!angular.$q.Promise<!MdPanelRef>} A promise that is resolved when
  1390. * the panel is detached.
  1391. */
  1392. MdPanelRef.prototype.detach = function() {
  1393. if (!this.isAttached) {
  1394. return this._$q.when(this);
  1395. }
  1396. var self = this;
  1397. var onDomRemoved = self.config['onDomRemoved'] || angular.noop;
  1398. var detachFn = function() {
  1399. self._removeEventListeners();
  1400. // Remove the focus traps that we added earlier for keeping focus within
  1401. // the panel.
  1402. if (self._topFocusTrap && self._topFocusTrap.parentNode) {
  1403. self._topFocusTrap.parentNode.removeChild(self._topFocusTrap);
  1404. }
  1405. if (self._bottomFocusTrap && self._bottomFocusTrap.parentNode) {
  1406. self._bottomFocusTrap.parentNode.removeChild(self._bottomFocusTrap);
  1407. }
  1408. if (self._restoreCache.classes) {
  1409. self.panelEl[0].className = self._restoreCache.classes;
  1410. }
  1411. // Either restore the saved styles or clear the ones set by mdPanel.
  1412. self.panelEl[0].style.cssText = self._restoreCache.styles || '';
  1413. self._compilerCleanup();
  1414. self.panelContainer.remove();
  1415. self.isAttached = false;
  1416. return self._$q.when(self);
  1417. };
  1418. if (this._restoreScroll) {
  1419. this._restoreScroll();
  1420. this._restoreScroll = null;
  1421. }
  1422. return this._$q(function(resolve, reject) {
  1423. var done = self._done(resolve, self);
  1424. self._$q.all([
  1425. detachFn(),
  1426. self._backdropRef ? self._backdropRef.detach() : true
  1427. ]).then(onDomRemoved)
  1428. .then(done)
  1429. .catch(reject);
  1430. });
  1431. };
  1432. /**
  1433. * Destroys the panel. The Panel cannot be opened again after this.
  1434. */
  1435. MdPanelRef.prototype.destroy = function() {
  1436. var self = this;
  1437. if (this.config.groupName) {
  1438. angular.forEach(this.config.groupName, function(group) {
  1439. self.removeFromGroup(group);
  1440. });
  1441. }
  1442. this.config.scope.$destroy();
  1443. this.config.locals = null;
  1444. this.config.onDomAdded = null;
  1445. this.config.onDomRemoved = null;
  1446. this.config.onRemoving = null;
  1447. this.config.onOpenComplete = null;
  1448. this._interceptors = null;
  1449. };
  1450. /**
  1451. * Shows the panel.
  1452. * @returns {!angular.$q.Promise<!MdPanelRef>} A promise that is resolved when
  1453. * the panel has shown and animations finish.
  1454. */
  1455. MdPanelRef.prototype.show = function() {
  1456. if (!this.panelContainer) {
  1457. return this._$q(function(resolve, reject) {
  1458. reject('mdPanel: Panel does not exist yet. Call open() or attach().');
  1459. });
  1460. }
  1461. if (!this.panelContainer.hasClass(MD_PANEL_HIDDEN)) {
  1462. return this._$q.when(this);
  1463. }
  1464. var self = this;
  1465. var animatePromise = function() {
  1466. self.panelContainer.removeClass(MD_PANEL_HIDDEN);
  1467. return self._animateOpen();
  1468. };
  1469. return this._$q(function(resolve, reject) {
  1470. var done = self._done(resolve, self);
  1471. var onOpenComplete = self.config['onOpenComplete'] || angular.noop;
  1472. var addToGroupOpen = function() {
  1473. if (self.config.groupName) {
  1474. angular.forEach(self.config.groupName, function(group) {
  1475. self._$mdPanel._groups[group].openPanels.push(self);
  1476. });
  1477. }
  1478. };
  1479. self._$q.all([
  1480. self._backdropRef ? self._backdropRef.show() : self,
  1481. animatePromise().then(function() { self._focusOnOpen(); }, reject)
  1482. ]).then(onOpenComplete)
  1483. .then(addToGroupOpen)
  1484. .then(done)
  1485. .catch(reject);
  1486. });
  1487. };
  1488. /**
  1489. * Hides the panel.
  1490. * @returns {!angular.$q.Promise<!MdPanelRef>} A promise that is resolved when
  1491. * the panel has hidden and animations finish.
  1492. */
  1493. MdPanelRef.prototype.hide = function() {
  1494. if (!this.panelContainer) {
  1495. return this._$q(function(resolve, reject) {
  1496. reject('mdPanel: Panel does not exist yet. Call open() or attach().');
  1497. });
  1498. }
  1499. if (this.panelContainer.hasClass(MD_PANEL_HIDDEN)) {
  1500. return this._$q.when(this);
  1501. }
  1502. var self = this;
  1503. return this._$q(function(resolve, reject) {
  1504. var done = self._done(resolve, self);
  1505. var onRemoving = self.config['onRemoving'] || angular.noop;
  1506. var hidePanel = function() {
  1507. self.panelContainer.addClass(MD_PANEL_HIDDEN);
  1508. };
  1509. var removeFromGroupOpen = function() {
  1510. if (self.config.groupName) {
  1511. var group, index;
  1512. angular.forEach(self.config.groupName, function(group) {
  1513. group = self._$mdPanel._groups[group];
  1514. index = group.openPanels.indexOf(self);
  1515. if (index > -1) {
  1516. group.openPanels.splice(index, 1);
  1517. }
  1518. });
  1519. }
  1520. };
  1521. var focusOnOrigin = function() {
  1522. var origin = self.config['origin'];
  1523. if (origin) {
  1524. getElement(origin).focus();
  1525. }
  1526. };
  1527. self._$q.all([
  1528. self._backdropRef ? self._backdropRef.hide() : self,
  1529. self._animateClose()
  1530. .then(onRemoving)
  1531. .then(hidePanel)
  1532. .then(removeFromGroupOpen)
  1533. .then(focusOnOrigin)
  1534. .catch(reject)
  1535. ]).then(done, reject);
  1536. });
  1537. };
  1538. /**
  1539. * Add a class to the panel. DO NOT use this to hide/show the panel.
  1540. * @deprecated
  1541. * This method is in the process of being deprecated in favor of using the panel
  1542. * and container JQLite elements that are referenced in the MdPanelRef object.
  1543. * Full deprecation is scheduled for material 1.2.
  1544. *
  1545. * @param {string} newClass Class to be added.
  1546. * @param {boolean} toElement Whether or not to add the class to the panel
  1547. * element instead of the container.
  1548. */
  1549. MdPanelRef.prototype.addClass = function(newClass, toElement) {
  1550. this._$log.warn(
  1551. 'mdPanel: The addClass method is in the process of being deprecated. ' +
  1552. 'Full deprecation is scheduled for the AngularJS Material 1.2 release. ' +
  1553. 'To achieve the same results, use the panelContainer or panelEl ' +
  1554. 'JQLite elements that are referenced in MdPanelRef.');
  1555. if (!this.panelContainer) {
  1556. throw new Error(
  1557. 'mdPanel: Panel does not exist yet. Call open() or attach().');
  1558. }
  1559. if (!toElement && !this.panelContainer.hasClass(newClass)) {
  1560. this.panelContainer.addClass(newClass);
  1561. } else if (toElement && !this.panelEl.hasClass(newClass)) {
  1562. this.panelEl.addClass(newClass);
  1563. }
  1564. };
  1565. /**
  1566. * Remove a class from the panel. DO NOT use this to hide/show the panel.
  1567. * @deprecated
  1568. * This method is in the process of being deprecated in favor of using the panel
  1569. * and container JQLite elements that are referenced in the MdPanelRef object.
  1570. * Full deprecation is scheduled for material 1.2.
  1571. *
  1572. * @param {string} oldClass Class to be removed.
  1573. * @param {boolean} fromElement Whether or not to remove the class from the
  1574. * panel element instead of the container.
  1575. */
  1576. MdPanelRef.prototype.removeClass = function(oldClass, fromElement) {
  1577. this._$log.warn(
  1578. 'mdPanel: The removeClass method is in the process of being deprecated. ' +
  1579. 'Full deprecation is scheduled for the AngularJS Material 1.2 release. ' +
  1580. 'To achieve the same results, use the panelContainer or panelEl ' +
  1581. 'JQLite elements that are referenced in MdPanelRef.');
  1582. if (!this.panelContainer) {
  1583. throw new Error(
  1584. 'mdPanel: Panel does not exist yet. Call open() or attach().');
  1585. }
  1586. if (!fromElement && this.panelContainer.hasClass(oldClass)) {
  1587. this.panelContainer.removeClass(oldClass);
  1588. } else if (fromElement && this.panelEl.hasClass(oldClass)) {
  1589. this.panelEl.removeClass(oldClass);
  1590. }
  1591. };
  1592. /**
  1593. * Toggle a class on the panel. DO NOT use this to hide/show the panel.
  1594. * @deprecated
  1595. * This method is in the process of being deprecated in favor of using the panel
  1596. * and container JQLite elements that are referenced in the MdPanelRef object.
  1597. * Full deprecation is scheduled for material 1.2.
  1598. *
  1599. * @param {string} toggleClass The class to toggle.
  1600. * @param {boolean} onElement Whether or not to toggle the class on the panel
  1601. * element instead of the container.
  1602. */
  1603. MdPanelRef.prototype.toggleClass = function(toggleClass, onElement) {
  1604. this._$log.warn(
  1605. 'mdPanel: The toggleClass method is in the process of being deprecated. ' +
  1606. 'Full deprecation is scheduled for the AngularJS Material 1.2 release. ' +
  1607. 'To achieve the same results, use the panelContainer or panelEl ' +
  1608. 'JQLite elements that are referenced in MdPanelRef.');
  1609. if (!this.panelContainer) {
  1610. throw new Error(
  1611. 'mdPanel: Panel does not exist yet. Call open() or attach().');
  1612. }
  1613. if (!onElement) {
  1614. this.panelContainer.toggleClass(toggleClass);
  1615. } else {
  1616. this.panelEl.toggleClass(toggleClass);
  1617. }
  1618. };
  1619. /**
  1620. * Compiles the panel, according to the passed in config and appends it to
  1621. * the DOM. Helps normalize differences in the compilation process between
  1622. * using a string template and a content element.
  1623. * @returns {!angular.$q.Promise<!MdPanelRef>} Promise that is resolved when
  1624. * the element has been compiled and added to the DOM.
  1625. * @private
  1626. */
  1627. MdPanelRef.prototype._compile = function() {
  1628. var self = this;
  1629. // Compile the element via $mdCompiler. Note that when using a
  1630. // contentElement, the element isn't actually being compiled, rather the
  1631. // compiler saves it's place in the DOM and provides a way of restoring it.
  1632. return self._$mdCompiler.compile(self.config).then(function(compileData) {
  1633. var config = self.config;
  1634. if (config.contentElement) {
  1635. var panelEl = compileData.element;
  1636. // Since mdPanel modifies the inline styles and CSS classes, we need
  1637. // to save them in order to be able to restore on close.
  1638. self._restoreCache.styles = panelEl[0].style.cssText;
  1639. self._restoreCache.classes = panelEl[0].className;
  1640. self.panelContainer = self._$mdPanel._wrapContentElement(panelEl);
  1641. self.panelEl = panelEl;
  1642. } else {
  1643. self.panelContainer = compileData.link(config['scope']);
  1644. self.panelEl = angular.element(
  1645. self.panelContainer[0].querySelector('.md-panel')
  1646. );
  1647. }
  1648. // Save a reference to the cleanup function from the compiler.
  1649. self._compilerCleanup = compileData.cleanup;
  1650. // Attach the panel to the proper place in the DOM.
  1651. getElement(self.config['attachTo']).append(self.panelContainer);
  1652. return self;
  1653. });
  1654. };
  1655. /**
  1656. * Creates a panel and adds it to the dom.
  1657. * @returns {!angular.$q.Promise} A promise that is resolved when the panel is
  1658. * created.
  1659. * @private
  1660. */
  1661. MdPanelRef.prototype._createPanel = function() {
  1662. var self = this;
  1663. return this._$q(function(resolve, reject) {
  1664. if (!self.config.locals) {
  1665. self.config.locals = {};
  1666. }
  1667. self.config.locals.mdPanelRef = self;
  1668. self._compile().then(function() {
  1669. if (self.config['disableParentScroll']) {
  1670. self._restoreScroll = self._$mdUtil.disableScrollAround(
  1671. null,
  1672. self.panelContainer,
  1673. { disableScrollMask: true }
  1674. );
  1675. }
  1676. // Add a custom CSS class to the panel element.
  1677. if (self.config['panelClass']) {
  1678. self.panelEl.addClass(self.config['panelClass']);
  1679. }
  1680. // Handle click and touch events for the panel container.
  1681. if (self.config['propagateContainerEvents']) {
  1682. self.panelContainer.css('pointer-events', 'none');
  1683. self.panelEl.css('pointer-events', 'all');
  1684. }
  1685. // Panel may be outside the $rootElement, tell ngAnimate to animate
  1686. // regardless.
  1687. if (self._$animate.pin) {
  1688. self._$animate.pin(
  1689. self.panelContainer,
  1690. getElement(self.config['attachTo'])
  1691. );
  1692. }
  1693. self._configureTrapFocus();
  1694. self._addStyles().then(function() {
  1695. resolve(self);
  1696. }, reject);
  1697. }, reject);
  1698. });
  1699. };
  1700. /**
  1701. * Adds the styles for the panel, such as positioning and z-index. Also,
  1702. * themes the panel element and panel container using `$mdTheming`.
  1703. * @returns {!angular.$q.Promise<!MdPanelRef>}
  1704. * @private
  1705. */
  1706. MdPanelRef.prototype._addStyles = function() {
  1707. var self = this;
  1708. return this._$q(function(resolve) {
  1709. self.panelContainer.css('z-index', self.config['zIndex']);
  1710. self.panelEl.css('z-index', self.config['zIndex'] + 1);
  1711. var hideAndResolve = function() {
  1712. // Theme the element and container.
  1713. self._setTheming();
  1714. // Remove offscreen class and add hidden class.
  1715. self.panelEl.removeClass('_md-panel-offscreen');
  1716. self.panelContainer.addClass(MD_PANEL_HIDDEN);
  1717. resolve(self);
  1718. };
  1719. if (self.config['fullscreen']) {
  1720. self.panelEl.addClass('_md-panel-fullscreen');
  1721. hideAndResolve();
  1722. return; // Don't setup positioning.
  1723. }
  1724. var positionConfig = self.config['position'];
  1725. if (!positionConfig) {
  1726. hideAndResolve();
  1727. return; // Don't setup positioning.
  1728. }
  1729. // Wait for angular to finish processing the template
  1730. self._$rootScope['$$postDigest'](function() {
  1731. // Position it correctly. This is necessary so that the panel will have a
  1732. // defined height and width.
  1733. self._updatePosition(true);
  1734. // Theme the element and container.
  1735. self._setTheming();
  1736. resolve(self);
  1737. });
  1738. });
  1739. };
  1740. /**
  1741. * Sets the `$mdTheming` classes on the `panelContainer` and `panelEl`.
  1742. * @private
  1743. */
  1744. MdPanelRef.prototype._setTheming = function() {
  1745. this._$mdTheming(this.panelEl);
  1746. this._$mdTheming(this.panelContainer);
  1747. };
  1748. /**
  1749. * Updates the position configuration of a panel
  1750. * @param {!MdPanelPosition} position
  1751. */
  1752. MdPanelRef.prototype.updatePosition = function(position) {
  1753. if (!this.panelContainer) {
  1754. throw new Error(
  1755. 'mdPanel: Panel does not exist yet. Call open() or attach().');
  1756. }
  1757. this.config['position'] = position;
  1758. this._updatePosition();
  1759. };
  1760. /**
  1761. * Calculates and updates the position of the panel.
  1762. * @param {boolean=} init
  1763. * @private
  1764. */
  1765. MdPanelRef.prototype._updatePosition = function(init) {
  1766. var positionConfig = this.config['position'];
  1767. if (positionConfig) {
  1768. positionConfig._setPanelPosition(this.panelEl);
  1769. // Hide the panel now that position is known.
  1770. if (init) {
  1771. this.panelEl.removeClass('_md-panel-offscreen');
  1772. this.panelContainer.addClass(MD_PANEL_HIDDEN);
  1773. }
  1774. this.panelEl.css(
  1775. MdPanelPosition.absPosition.TOP,
  1776. positionConfig.getTop()
  1777. );
  1778. this.panelEl.css(
  1779. MdPanelPosition.absPosition.BOTTOM,
  1780. positionConfig.getBottom()
  1781. );
  1782. this.panelEl.css(
  1783. MdPanelPosition.absPosition.LEFT,
  1784. positionConfig.getLeft()
  1785. );
  1786. this.panelEl.css(
  1787. MdPanelPosition.absPosition.RIGHT,
  1788. positionConfig.getRight()
  1789. );
  1790. }
  1791. };
  1792. /**
  1793. * Focuses on the panel or the first focus target.
  1794. * @private
  1795. */
  1796. MdPanelRef.prototype._focusOnOpen = function() {
  1797. if (this.config['focusOnOpen']) {
  1798. // Wait for the template to finish rendering to guarantee md-autofocus has
  1799. // finished adding the class md-autofocus, otherwise the focusable element
  1800. // isn't available to focus.
  1801. var self = this;
  1802. this._$rootScope['$$postDigest'](function() {
  1803. var target = self._$mdUtil.findFocusTarget(self.panelEl) ||
  1804. self.panelEl;
  1805. target.focus();
  1806. });
  1807. }
  1808. };
  1809. /**
  1810. * Shows the backdrop.
  1811. * @returns {!angular.$q.Promise} A promise that is resolved when the backdrop
  1812. * is created and attached.
  1813. * @private
  1814. */
  1815. MdPanelRef.prototype._createBackdrop = function() {
  1816. if (this.config.hasBackdrop) {
  1817. if (!this._backdropRef) {
  1818. var backdropAnimation = this._$mdPanel.newPanelAnimation()
  1819. .openFrom(this.config.attachTo)
  1820. .withAnimation({
  1821. open: '_md-opaque-enter',
  1822. close: '_md-opaque-leave'
  1823. });
  1824. if (this.config.animation) {
  1825. backdropAnimation.duration(this.config.animation._rawDuration);
  1826. }
  1827. var backdropConfig = {
  1828. animation: backdropAnimation,
  1829. attachTo: this.config.attachTo,
  1830. focusOnOpen: false,
  1831. panelClass: '_md-panel-backdrop',
  1832. zIndex: this.config.zIndex - 1
  1833. };
  1834. this._backdropRef = this._$mdPanel.create(backdropConfig);
  1835. }
  1836. if (!this._backdropRef.isAttached) {
  1837. return this._backdropRef.attach();
  1838. }
  1839. }
  1840. };
  1841. /**
  1842. * Listen for escape keys and outside clicks to auto close.
  1843. * @private
  1844. */
  1845. MdPanelRef.prototype._addEventListeners = function() {
  1846. this._configureEscapeToClose();
  1847. this._configureClickOutsideToClose();
  1848. this._configureScrollListener();
  1849. };
  1850. /**
  1851. * Remove event listeners added in _addEventListeners.
  1852. * @private
  1853. */
  1854. MdPanelRef.prototype._removeEventListeners = function() {
  1855. this._removeListeners && this._removeListeners.forEach(function(removeFn) {
  1856. removeFn();
  1857. });
  1858. this._removeListeners = [];
  1859. };
  1860. /**
  1861. * Setup the escapeToClose event listeners.
  1862. * @private
  1863. */
  1864. MdPanelRef.prototype._configureEscapeToClose = function() {
  1865. if (this.config['escapeToClose']) {
  1866. var parentTarget = getElement(this.config['attachTo']);
  1867. var self = this;
  1868. var keyHandlerFn = function(ev) {
  1869. if (ev.keyCode === self._$mdConstant.KEY_CODE.ESCAPE) {
  1870. ev.stopPropagation();
  1871. ev.preventDefault();
  1872. self.close(MdPanelRef.closeReasons.ESCAPE);
  1873. }
  1874. };
  1875. // Add keydown listeners
  1876. this.panelContainer.on('keydown', keyHandlerFn);
  1877. parentTarget.on('keydown', keyHandlerFn);
  1878. // Queue remove listeners function
  1879. this._removeListeners.push(function() {
  1880. self.panelContainer.off('keydown', keyHandlerFn);
  1881. parentTarget.off('keydown', keyHandlerFn);
  1882. });
  1883. }
  1884. };
  1885. /**
  1886. * Setup the clickOutsideToClose event listeners.
  1887. * @private
  1888. */
  1889. MdPanelRef.prototype._configureClickOutsideToClose = function() {
  1890. if (this.config['clickOutsideToClose']) {
  1891. var target = this.config['propagateContainerEvents'] ?
  1892. angular.element(document.body) :
  1893. this.panelContainer;
  1894. var sourceEl;
  1895. // Keep track of the element on which the mouse originally went down
  1896. // so that we can only close the backdrop when the 'click' started on it.
  1897. // A simple 'click' handler does not work, it sets the target object as the
  1898. // element the mouse went down on.
  1899. var mousedownHandler = function(ev) {
  1900. sourceEl = ev.target;
  1901. };
  1902. // We check if our original element and the target is the backdrop
  1903. // because if the original was the backdrop and the target was inside the
  1904. // panel we don't want to panel to close.
  1905. var self = this;
  1906. var mouseupHandler = function(ev) {
  1907. if (self.config['propagateContainerEvents']) {
  1908. // We check if the sourceEl of the event is the panel element or one
  1909. // of it's children. If it is not, then close the panel.
  1910. if (sourceEl !== self.panelEl[0] && !self.panelEl[0].contains(sourceEl)) {
  1911. self.close();
  1912. }
  1913. } else if (sourceEl === target[0] && ev.target === target[0]) {
  1914. ev.stopPropagation();
  1915. ev.preventDefault();
  1916. self.close(MdPanelRef.closeReasons.CLICK_OUTSIDE);
  1917. }
  1918. };
  1919. // Add listeners
  1920. target.on('mousedown', mousedownHandler);
  1921. target.on('mouseup', mouseupHandler);
  1922. // Queue remove listeners function
  1923. this._removeListeners.push(function() {
  1924. target.off('mousedown', mousedownHandler);
  1925. target.off('mouseup', mouseupHandler);
  1926. });
  1927. }
  1928. };
  1929. /**
  1930. * Configures the listeners for updating the panel position on scroll.
  1931. * @private
  1932. */
  1933. MdPanelRef.prototype._configureScrollListener = function() {
  1934. // No need to bind the event if scrolling is disabled.
  1935. if (!this.config['disableParentScroll']) {
  1936. var updatePosition = angular.bind(this, this._updatePosition);
  1937. var debouncedUpdatePosition = this._$$rAF.throttle(updatePosition);
  1938. var self = this;
  1939. var onScroll = function() {
  1940. debouncedUpdatePosition();
  1941. };
  1942. // Add listeners.
  1943. this._$window.addEventListener('scroll', onScroll, true);
  1944. // Queue remove listeners function.
  1945. this._removeListeners.push(function() {
  1946. self._$window.removeEventListener('scroll', onScroll, true);
  1947. });
  1948. }
  1949. };
  1950. /**
  1951. * Setup the focus traps. These traps will wrap focus when tabbing past the
  1952. * panel. When shift-tabbing, the focus will stick in place.
  1953. * @private
  1954. */
  1955. MdPanelRef.prototype._configureTrapFocus = function() {
  1956. // Focus doesn't remain inside of the panel without this.
  1957. this.panelEl.attr('tabIndex', '-1');
  1958. if (this.config['trapFocus']) {
  1959. var element = this.panelEl;
  1960. // Set up elements before and after the panel to capture focus and
  1961. // redirect back into the panel.
  1962. this._topFocusTrap = FOCUS_TRAP_TEMPLATE.clone()[0];
  1963. this._bottomFocusTrap = FOCUS_TRAP_TEMPLATE.clone()[0];
  1964. // When focus is about to move out of the panel, we want to intercept it
  1965. // and redirect it back to the panel element.
  1966. var focusHandler = function() {
  1967. element.focus();
  1968. };
  1969. this._topFocusTrap.addEventListener('focus', focusHandler);
  1970. this._bottomFocusTrap.addEventListener('focus', focusHandler);
  1971. // Queue remove listeners function
  1972. this._removeListeners.push(this._simpleBind(function() {
  1973. this._topFocusTrap.removeEventListener('focus', focusHandler);
  1974. this._bottomFocusTrap.removeEventListener('focus', focusHandler);
  1975. }, this));
  1976. // The top focus trap inserted immediately before the md-panel element (as
  1977. // a sibling). The bottom focus trap inserted immediately after the
  1978. // md-panel element (as a sibling).
  1979. element[0].parentNode.insertBefore(this._topFocusTrap, element[0]);
  1980. element.after(this._bottomFocusTrap);
  1981. }
  1982. };
  1983. /**
  1984. * Updates the animation of a panel.
  1985. * @param {!MdPanelAnimation} animation
  1986. */
  1987. MdPanelRef.prototype.updateAnimation = function(animation) {
  1988. this.config['animation'] = animation;
  1989. if (this._backdropRef) {
  1990. this._backdropRef.config.animation.duration(animation._rawDuration);
  1991. }
  1992. };
  1993. /**
  1994. * Animate the panel opening.
  1995. * @returns {!angular.$q.Promise} A promise that is resolved when the panel has
  1996. * animated open.
  1997. * @private
  1998. */
  1999. MdPanelRef.prototype._animateOpen = function() {
  2000. this.panelContainer.addClass('md-panel-is-showing');
  2001. var animationConfig = this.config['animation'];
  2002. if (!animationConfig) {
  2003. // Promise is in progress, return it.
  2004. this.panelContainer.addClass('_md-panel-shown');
  2005. return this._$q.when(this);
  2006. }
  2007. var self = this;
  2008. return this._$q(function(resolve) {
  2009. var done = self._done(resolve, self);
  2010. var warnAndOpen = function() {
  2011. self._$log.warn(
  2012. 'mdPanel: MdPanel Animations failed. ' +
  2013. 'Showing panel without animating.');
  2014. done();
  2015. };
  2016. animationConfig.animateOpen(self.panelEl)
  2017. .then(done, warnAndOpen);
  2018. });
  2019. };
  2020. /**
  2021. * Animate the panel closing.
  2022. * @returns {!angular.$q.Promise} A promise that is resolved when the panel has
  2023. * animated closed.
  2024. * @private
  2025. */
  2026. MdPanelRef.prototype._animateClose = function() {
  2027. var animationConfig = this.config['animation'];
  2028. if (!animationConfig) {
  2029. this.panelContainer.removeClass('md-panel-is-showing');
  2030. this.panelContainer.removeClass('_md-panel-shown');
  2031. return this._$q.when(this);
  2032. }
  2033. var self = this;
  2034. return this._$q(function(resolve) {
  2035. var done = function() {
  2036. self.panelContainer.removeClass('md-panel-is-showing');
  2037. resolve(self);
  2038. };
  2039. var warnAndClose = function() {
  2040. self._$log.warn(
  2041. 'mdPanel: MdPanel Animations failed. ' +
  2042. 'Hiding panel without animating.');
  2043. done();
  2044. };
  2045. animationConfig.animateClose(self.panelEl)
  2046. .then(done, warnAndClose);
  2047. });
  2048. };
  2049. /**
  2050. * Registers a interceptor with the panel. The callback should return a promise,
  2051. * which will allow the action to continue when it gets resolved, or will
  2052. * prevent an action if it is rejected.
  2053. * @param {string} type Type of interceptor.
  2054. * @param {!angular.$q.Promise<!any>} callback Callback to be registered.
  2055. * @returns {!MdPanelRef}
  2056. */
  2057. MdPanelRef.prototype.registerInterceptor = function(type, callback) {
  2058. var error = null;
  2059. if (!angular.isString(type)) {
  2060. error = 'Interceptor type must be a string, instead got ' + typeof type;
  2061. } else if (!angular.isFunction(callback)) {
  2062. error = 'Interceptor callback must be a function, instead got ' + typeof callback;
  2063. }
  2064. if (error) {
  2065. throw new Error('MdPanel: ' + error);
  2066. }
  2067. var interceptors = this._interceptors[type] = this._interceptors[type] || [];
  2068. if (interceptors.indexOf(callback) === -1) {
  2069. interceptors.push(callback);
  2070. }
  2071. return this;
  2072. };
  2073. /**
  2074. * Removes a registered interceptor.
  2075. * @param {string} type Type of interceptor to be removed.
  2076. * @param {Function} callback Interceptor to be removed.
  2077. * @returns {!MdPanelRef}
  2078. */
  2079. MdPanelRef.prototype.removeInterceptor = function(type, callback) {
  2080. var index = this._interceptors[type] ?
  2081. this._interceptors[type].indexOf(callback) : -1;
  2082. if (index > -1) {
  2083. this._interceptors[type].splice(index, 1);
  2084. }
  2085. return this;
  2086. };
  2087. /**
  2088. * Removes all interceptors.
  2089. * @param {string=} type Type of interceptors to be removed.
  2090. * If ommited, all interceptors types will be removed.
  2091. * @returns {!MdPanelRef}
  2092. */
  2093. MdPanelRef.prototype.removeAllInterceptors = function(type) {
  2094. if (type) {
  2095. this._interceptors[type] = [];
  2096. } else {
  2097. this._interceptors = Object.create(null);
  2098. }
  2099. return this;
  2100. };
  2101. /**
  2102. * Invokes all the interceptors of a certain type sequantially in
  2103. * reverse order. Works in a similar way to `$q.all`, except it
  2104. * respects the order of the functions.
  2105. * @param {string} type Type of interceptors to be invoked.
  2106. * @returns {!angular.$q.Promise<!MdPanelRef>}
  2107. * @private
  2108. */
  2109. MdPanelRef.prototype._callInterceptors = function(type) {
  2110. var self = this;
  2111. var $q = self._$q;
  2112. var interceptors = self._interceptors && self._interceptors[type] || [];
  2113. return interceptors.reduceRight(function(promise, interceptor) {
  2114. var isPromiseLike = interceptor && angular.isFunction(interceptor.then);
  2115. var response = isPromiseLike ? interceptor : null;
  2116. /**
  2117. * For interceptors to reject/cancel subsequent portions of the chain, simply
  2118. * return a `$q.reject(<value>)`
  2119. */
  2120. return promise.then(function() {
  2121. if (!response) {
  2122. try {
  2123. response = interceptor(self);
  2124. } catch(e) {
  2125. response = $q.reject(e);
  2126. }
  2127. }
  2128. return response;
  2129. });
  2130. }, $q.resolve(self));
  2131. };
  2132. /**
  2133. * Faster, more basic than angular.bind
  2134. * http://jsperf.com/angular-bind-vs-custom-vs-native
  2135. * @param {function} callback
  2136. * @param {!Object} self
  2137. * @return {function} Callback function with a bound self.
  2138. */
  2139. MdPanelRef.prototype._simpleBind = function(callback, self) {
  2140. return function(value) {
  2141. return callback.apply(self, value);
  2142. };
  2143. };
  2144. /**
  2145. * @param {function} callback
  2146. * @param {!Object} self
  2147. * @return {function} Callback function with a self param.
  2148. */
  2149. MdPanelRef.prototype._done = function(callback, self) {
  2150. return function() {
  2151. callback(self);
  2152. };
  2153. };
  2154. /**
  2155. * Adds a panel to a group if the panel does not exist within the group already.
  2156. * A panel can only exist within a single group.
  2157. * @param {string} groupName The name of the group.
  2158. */
  2159. MdPanelRef.prototype.addToGroup = function(groupName) {
  2160. if (!this._$mdPanel._groups[groupName]) {
  2161. this._$mdPanel.newPanelGroup(groupName);
  2162. }
  2163. var group = this._$mdPanel._groups[groupName];
  2164. var index = group.panels.indexOf(this);
  2165. if (index < 0) {
  2166. group.panels.push(this);
  2167. }
  2168. };
  2169. /**
  2170. * Removes a panel from a group if the panel exists within that group. The group
  2171. * must be created ahead of time.
  2172. * @param {string} groupName The name of the group.
  2173. */
  2174. MdPanelRef.prototype.removeFromGroup = function(groupName) {
  2175. if (!this._$mdPanel._groups[groupName]) {
  2176. throw new Error('mdPanel: The group ' + groupName + ' does not exist.');
  2177. }
  2178. var group = this._$mdPanel._groups[groupName];
  2179. var index = group.panels.indexOf(this);
  2180. if (index > -1) {
  2181. group.panels.splice(index, 1);
  2182. }
  2183. };
  2184. /**
  2185. * Possible default closeReasons for the close function.
  2186. * @enum {string}
  2187. */
  2188. MdPanelRef.closeReasons = {
  2189. CLICK_OUTSIDE: 'clickOutsideToClose',
  2190. ESCAPE: 'escapeToClose',
  2191. };
  2192. /*****************************************************************************
  2193. * MdPanelPosition *
  2194. *****************************************************************************/
  2195. /**
  2196. * Position configuration object. To use, create an MdPanelPosition with the
  2197. * desired properties, then pass the object as part of $mdPanel creation.
  2198. *
  2199. * Example:
  2200. *
  2201. * var panelPosition = new MdPanelPosition()
  2202. * .relativeTo(myButtonEl)
  2203. * .addPanelPosition(
  2204. * $mdPanel.xPosition.CENTER,
  2205. * $mdPanel.yPosition.ALIGN_TOPS
  2206. * );
  2207. *
  2208. * $mdPanel.create({
  2209. * position: panelPosition
  2210. * });
  2211. *
  2212. * @param {!angular.$injector} $injector
  2213. * @final @constructor
  2214. */
  2215. function MdPanelPosition($injector) {
  2216. /** @private @const {!angular.$window} */
  2217. this._$window = $injector.get('$window');
  2218. /** @private {boolean} */
  2219. this._isRTL = $injector.get('$mdUtil').bidi() === 'rtl';
  2220. /** @private @const {!angular.$mdConstant} */
  2221. this._$mdConstant = $injector.get('$mdConstant');
  2222. /** @private {boolean} */
  2223. this._absolute = false;
  2224. /** @private {!angular.JQLite} */
  2225. this._relativeToEl;
  2226. /** @private {string} */
  2227. this._top = '';
  2228. /** @private {string} */
  2229. this._bottom = '';
  2230. /** @private {string} */
  2231. this._left = '';
  2232. /** @private {string} */
  2233. this._right = '';
  2234. /** @private {!Array<string>} */
  2235. this._translateX = [];
  2236. /** @private {!Array<string>} */
  2237. this._translateY = [];
  2238. /** @private {!Array<{x:string, y:string}>} */
  2239. this._positions = [];
  2240. /** @private {?{x:string, y:string}} */
  2241. this._actualPosition;
  2242. }
  2243. /**
  2244. * Possible values of xPosition.
  2245. * @enum {string}
  2246. */
  2247. MdPanelPosition.xPosition = {
  2248. CENTER: 'center',
  2249. ALIGN_START: 'align-start',
  2250. ALIGN_END: 'align-end',
  2251. OFFSET_START: 'offset-start',
  2252. OFFSET_END: 'offset-end'
  2253. };
  2254. /**
  2255. * Possible values of yPosition.
  2256. * @enum {string}
  2257. */
  2258. MdPanelPosition.yPosition = {
  2259. CENTER: 'center',
  2260. ALIGN_TOPS: 'align-tops',
  2261. ALIGN_BOTTOMS: 'align-bottoms',
  2262. ABOVE: 'above',
  2263. BELOW: 'below'
  2264. };
  2265. /**
  2266. * Possible values of absolute position.
  2267. * @enum {string}
  2268. */
  2269. MdPanelPosition.absPosition = {
  2270. TOP: 'top',
  2271. RIGHT: 'right',
  2272. BOTTOM: 'bottom',
  2273. LEFT: 'left'
  2274. };
  2275. /**
  2276. * Margin between the edges of a panel and the viewport.
  2277. * @const {number}
  2278. */
  2279. MdPanelPosition.viewportMargin = 8;
  2280. /**
  2281. * Sets absolute positioning for the panel.
  2282. * @return {!MdPanelPosition}
  2283. */
  2284. MdPanelPosition.prototype.absolute = function() {
  2285. this._absolute = true;
  2286. return this;
  2287. };
  2288. /**
  2289. * Sets the value of a position for the panel. Clears any previously set
  2290. * position.
  2291. * @param {string} position Position to set
  2292. * @param {string=} value Value of the position. Defaults to '0'.
  2293. * @returns {!MdPanelPosition}
  2294. * @private
  2295. */
  2296. MdPanelPosition.prototype._setPosition = function(position, value) {
  2297. if (position === MdPanelPosition.absPosition.RIGHT ||
  2298. position === MdPanelPosition.absPosition.LEFT) {
  2299. this._left = this._right = '';
  2300. } else if (
  2301. position === MdPanelPosition.absPosition.BOTTOM ||
  2302. position === MdPanelPosition.absPosition.TOP) {
  2303. this._top = this._bottom = '';
  2304. } else {
  2305. var positions = Object.keys(MdPanelPosition.absPosition).join()
  2306. .toLowerCase();
  2307. throw new Error('mdPanel: Position must be one of ' + positions + '.');
  2308. }
  2309. this['_' + position] = angular.isString(value) ? value : '0';
  2310. return this;
  2311. };
  2312. /**
  2313. * Sets the value of `top` for the panel. Clears any previously set vertical
  2314. * position.
  2315. * @param {string=} top Value of `top`. Defaults to '0'.
  2316. * @returns {!MdPanelPosition}
  2317. */
  2318. MdPanelPosition.prototype.top = function(top) {
  2319. return this._setPosition(MdPanelPosition.absPosition.TOP, top);
  2320. };
  2321. /**
  2322. * Sets the value of `bottom` for the panel. Clears any previously set vertical
  2323. * position.
  2324. * @param {string=} bottom Value of `bottom`. Defaults to '0'.
  2325. * @returns {!MdPanelPosition}
  2326. */
  2327. MdPanelPosition.prototype.bottom = function(bottom) {
  2328. return this._setPosition(MdPanelPosition.absPosition.BOTTOM, bottom);
  2329. };
  2330. /**
  2331. * Sets the panel to the start of the page - `left` if `ltr` or `right` for
  2332. * `rtl`. Clears any previously set horizontal position.
  2333. * @param {string=} start Value of position. Defaults to '0'.
  2334. * @returns {!MdPanelPosition}
  2335. */
  2336. MdPanelPosition.prototype.start = function(start) {
  2337. var position = this._isRTL ? MdPanelPosition.absPosition.RIGHT : MdPanelPosition.absPosition.LEFT;
  2338. return this._setPosition(position, start);
  2339. };
  2340. /**
  2341. * Sets the panel to the end of the page - `right` if `ltr` or `left` for `rtl`.
  2342. * Clears any previously set horizontal position.
  2343. * @param {string=} end Value of position. Defaults to '0'.
  2344. * @returns {!MdPanelPosition}
  2345. */
  2346. MdPanelPosition.prototype.end = function(end) {
  2347. var position = this._isRTL ? MdPanelPosition.absPosition.LEFT : MdPanelPosition.absPosition.RIGHT;
  2348. return this._setPosition(position, end);
  2349. };
  2350. /**
  2351. * Sets the value of `left` for the panel. Clears any previously set
  2352. * horizontal position.
  2353. * @param {string=} left Value of `left`. Defaults to '0'.
  2354. * @returns {!MdPanelPosition}
  2355. */
  2356. MdPanelPosition.prototype.left = function(left) {
  2357. return this._setPosition(MdPanelPosition.absPosition.LEFT, left);
  2358. };
  2359. /**
  2360. * Sets the value of `right` for the panel. Clears any previously set
  2361. * horizontal position.
  2362. * @param {string=} right Value of `right`. Defaults to '0'.
  2363. * @returns {!MdPanelPosition}
  2364. */
  2365. MdPanelPosition.prototype.right = function(right) {
  2366. return this._setPosition(MdPanelPosition.absPosition.RIGHT, right);
  2367. };
  2368. /**
  2369. * Centers the panel horizontally in the viewport. Clears any previously set
  2370. * horizontal position.
  2371. * @returns {!MdPanelPosition}
  2372. */
  2373. MdPanelPosition.prototype.centerHorizontally = function() {
  2374. this._left = '50%';
  2375. this._right = '';
  2376. this._translateX = ['-50%'];
  2377. return this;
  2378. };
  2379. /**
  2380. * Centers the panel vertically in the viewport. Clears any previously set
  2381. * vertical position.
  2382. * @returns {!MdPanelPosition}
  2383. */
  2384. MdPanelPosition.prototype.centerVertically = function() {
  2385. this._top = '50%';
  2386. this._bottom = '';
  2387. this._translateY = ['-50%'];
  2388. return this;
  2389. };
  2390. /**
  2391. * Centers the panel horizontally and vertically in the viewport. This is
  2392. * equivalent to calling both `centerHorizontally` and `centerVertically`.
  2393. * Clears any previously set horizontal and vertical positions.
  2394. * @returns {!MdPanelPosition}
  2395. */
  2396. MdPanelPosition.prototype.center = function() {
  2397. return this.centerHorizontally().centerVertically();
  2398. };
  2399. /**
  2400. * Sets element for relative positioning.
  2401. * @param {string|!Element|!angular.JQLite} element Query selector, DOM element,
  2402. * or angular element to set the panel relative to.
  2403. * @returns {!MdPanelPosition}
  2404. */
  2405. MdPanelPosition.prototype.relativeTo = function(element) {
  2406. this._absolute = false;
  2407. this._relativeToEl = getElement(element);
  2408. return this;
  2409. };
  2410. /**
  2411. * Sets the x and y positions for the panel relative to another element.
  2412. * @param {string} xPosition must be one of the MdPanelPosition.xPosition
  2413. * values.
  2414. * @param {string} yPosition must be one of the MdPanelPosition.yPosition
  2415. * values.
  2416. * @returns {!MdPanelPosition}
  2417. */
  2418. MdPanelPosition.prototype.addPanelPosition = function(xPosition, yPosition) {
  2419. if (!this._relativeToEl) {
  2420. throw new Error('mdPanel: addPanelPosition can only be used with ' +
  2421. 'relative positioning. Set relativeTo first.');
  2422. }
  2423. this._validateXPosition(xPosition);
  2424. this._validateYPosition(yPosition);
  2425. this._positions.push({
  2426. x: xPosition,
  2427. y: yPosition,
  2428. });
  2429. return this;
  2430. };
  2431. /**
  2432. * Ensures that yPosition is a valid position name. Throw an exception if not.
  2433. * @param {string} yPosition
  2434. */
  2435. MdPanelPosition.prototype._validateYPosition = function(yPosition) {
  2436. // empty is ok
  2437. if (yPosition == null) {
  2438. return;
  2439. }
  2440. var positionKeys = Object.keys(MdPanelPosition.yPosition);
  2441. var positionValues = [];
  2442. for (var key, i = 0; key = positionKeys[i]; i++) {
  2443. var position = MdPanelPosition.yPosition[key];
  2444. positionValues.push(position);
  2445. if (position === yPosition) {
  2446. return;
  2447. }
  2448. }
  2449. throw new Error('mdPanel: Panel y position only accepts the following ' +
  2450. 'values:\n' + positionValues.join(' | '));
  2451. };
  2452. /**
  2453. * Ensures that xPosition is a valid position name. Throw an exception if not.
  2454. * @param {string} xPosition
  2455. */
  2456. MdPanelPosition.prototype._validateXPosition = function(xPosition) {
  2457. // empty is ok
  2458. if (xPosition == null) {
  2459. return;
  2460. }
  2461. var positionKeys = Object.keys(MdPanelPosition.xPosition);
  2462. var positionValues = [];
  2463. for (var key, i = 0; key = positionKeys[i]; i++) {
  2464. var position = MdPanelPosition.xPosition[key];
  2465. positionValues.push(position);
  2466. if (position === xPosition) {
  2467. return;
  2468. }
  2469. }
  2470. throw new Error('mdPanel: Panel x Position only accepts the following ' +
  2471. 'values:\n' + positionValues.join(' | '));
  2472. };
  2473. /**
  2474. * Sets the value of the offset in the x-direction. This will add to any
  2475. * previously set offsets.
  2476. * @param {string|number|function(MdPanelPosition): string} offsetX
  2477. * @returns {!MdPanelPosition}
  2478. */
  2479. MdPanelPosition.prototype.withOffsetX = function(offsetX) {
  2480. this._translateX.push(addUnits(offsetX));
  2481. return this;
  2482. };
  2483. /**
  2484. * Sets the value of the offset in the y-direction. This will add to any
  2485. * previously set offsets.
  2486. * @param {string|number|function(MdPanelPosition): string} offsetY
  2487. * @returns {!MdPanelPosition}
  2488. */
  2489. MdPanelPosition.prototype.withOffsetY = function(offsetY) {
  2490. this._translateY.push(addUnits(offsetY));
  2491. return this;
  2492. };
  2493. /**
  2494. * Gets the value of `top` for the panel.
  2495. * @returns {string}
  2496. */
  2497. MdPanelPosition.prototype.getTop = function() {
  2498. return this._top;
  2499. };
  2500. /**
  2501. * Gets the value of `bottom` for the panel.
  2502. * @returns {string}
  2503. */
  2504. MdPanelPosition.prototype.getBottom = function() {
  2505. return this._bottom;
  2506. };
  2507. /**
  2508. * Gets the value of `left` for the panel.
  2509. * @returns {string}
  2510. */
  2511. MdPanelPosition.prototype.getLeft = function() {
  2512. return this._left;
  2513. };
  2514. /**
  2515. * Gets the value of `right` for the panel.
  2516. * @returns {string}
  2517. */
  2518. MdPanelPosition.prototype.getRight = function() {
  2519. return this._right;
  2520. };
  2521. /**
  2522. * Gets the value of `transform` for the panel.
  2523. * @returns {string}
  2524. */
  2525. MdPanelPosition.prototype.getTransform = function() {
  2526. var translateX = this._reduceTranslateValues('translateX', this._translateX);
  2527. var translateY = this._reduceTranslateValues('translateY', this._translateY);
  2528. // It's important to trim the result, because the browser will ignore the set
  2529. // operation if the string contains only whitespace.
  2530. return (translateX + ' ' + translateY).trim();
  2531. };
  2532. /**
  2533. * Sets the `transform` value for a panel element.
  2534. * @param {!angular.JQLite} panelEl
  2535. * @returns {!angular.JQLite}
  2536. * @private
  2537. */
  2538. MdPanelPosition.prototype._setTransform = function(panelEl) {
  2539. return panelEl.css(this._$mdConstant.CSS.TRANSFORM, this.getTransform());
  2540. };
  2541. /**
  2542. * True if the panel is completely on-screen with this positioning; false
  2543. * otherwise.
  2544. * @param {!angular.JQLite} panelEl
  2545. * @return {boolean}
  2546. * @private
  2547. */
  2548. MdPanelPosition.prototype._isOnscreen = function(panelEl) {
  2549. // this works because we always use fixed positioning for the panel,
  2550. // which is relative to the viewport.
  2551. var left = parseInt(this.getLeft());
  2552. var top = parseInt(this.getTop());
  2553. if (this._translateX.length || this._translateY.length) {
  2554. var prefixedTransform = this._$mdConstant.CSS.TRANSFORM;
  2555. var offsets = getComputedTranslations(panelEl, prefixedTransform);
  2556. left += offsets.x;
  2557. top += offsets.y;
  2558. }
  2559. var right = left + panelEl[0].offsetWidth;
  2560. var bottom = top + panelEl[0].offsetHeight;
  2561. return (left >= 0) &&
  2562. (top >= 0) &&
  2563. (bottom <= this._$window.innerHeight) &&
  2564. (right <= this._$window.innerWidth);
  2565. };
  2566. /**
  2567. * Gets the first x/y position that can fit on-screen.
  2568. * @returns {{x: string, y: string}}
  2569. */
  2570. MdPanelPosition.prototype.getActualPosition = function() {
  2571. return this._actualPosition;
  2572. };
  2573. /**
  2574. * Reduces a list of translate values to a string that can be used within
  2575. * transform.
  2576. * @param {string} translateFn
  2577. * @param {!Array<string>} values
  2578. * @returns {string}
  2579. * @private
  2580. */
  2581. MdPanelPosition.prototype._reduceTranslateValues =
  2582. function(translateFn, values) {
  2583. return values.map(function(translation) {
  2584. var translationValue = angular.isFunction(translation) ?
  2585. addUnits(translation(this)) : translation;
  2586. return translateFn + '(' + translationValue + ')';
  2587. }, this).join(' ');
  2588. };
  2589. /**
  2590. * Sets the panel position based on the created panel element and best x/y
  2591. * positioning.
  2592. * @param {!angular.JQLite} panelEl
  2593. * @private
  2594. */
  2595. MdPanelPosition.prototype._setPanelPosition = function(panelEl) {
  2596. // Remove the "position adjusted" class in case it has been added before.
  2597. panelEl.removeClass('_md-panel-position-adjusted');
  2598. // Only calculate the position if necessary.
  2599. if (this._absolute) {
  2600. this._setTransform(panelEl);
  2601. return;
  2602. }
  2603. if (this._actualPosition) {
  2604. this._calculatePanelPosition(panelEl, this._actualPosition);
  2605. this._setTransform(panelEl);
  2606. this._constrainToViewport(panelEl);
  2607. return;
  2608. }
  2609. for (var i = 0; i < this._positions.length; i++) {
  2610. this._actualPosition = this._positions[i];
  2611. this._calculatePanelPosition(panelEl, this._actualPosition);
  2612. this._setTransform(panelEl);
  2613. if (this._isOnscreen(panelEl)) {
  2614. return;
  2615. }
  2616. }
  2617. this._constrainToViewport(panelEl);
  2618. };
  2619. /**
  2620. * Constrains a panel's position to the viewport.
  2621. * @param {!angular.JQLite} panelEl
  2622. * @private
  2623. */
  2624. MdPanelPosition.prototype._constrainToViewport = function(panelEl) {
  2625. var margin = MdPanelPosition.viewportMargin;
  2626. var initialTop = this._top;
  2627. var initialLeft = this._left;
  2628. if (this.getTop()) {
  2629. var top = parseInt(this.getTop());
  2630. var bottom = panelEl[0].offsetHeight + top;
  2631. var viewportHeight = this._$window.innerHeight;
  2632. if (top < margin) {
  2633. this._top = margin + 'px';
  2634. } else if (bottom > viewportHeight) {
  2635. this._top = top - (bottom - viewportHeight + margin) + 'px';
  2636. }
  2637. }
  2638. if (this.getLeft()) {
  2639. var left = parseInt(this.getLeft());
  2640. var right = panelEl[0].offsetWidth + left;
  2641. var viewportWidth = this._$window.innerWidth;
  2642. if (left < margin) {
  2643. this._left = margin + 'px';
  2644. } else if (right > viewportWidth) {
  2645. this._left = left - (right - viewportWidth + margin) + 'px';
  2646. }
  2647. }
  2648. // Class that can be used to re-style the panel if it was repositioned.
  2649. panelEl.toggleClass(
  2650. '_md-panel-position-adjusted',
  2651. this._top !== initialTop || this._left !== initialLeft
  2652. );
  2653. };
  2654. /**
  2655. * Switches between 'start' and 'end'.
  2656. * @param {string} position Horizontal position of the panel
  2657. * @returns {string} Reversed position
  2658. * @private
  2659. */
  2660. MdPanelPosition.prototype._reverseXPosition = function(position) {
  2661. if (position === MdPanelPosition.xPosition.CENTER) {
  2662. return position;
  2663. }
  2664. var start = 'start';
  2665. var end = 'end';
  2666. return position.indexOf(start) > -1 ? position.replace(start, end) : position.replace(end, start);
  2667. };
  2668. /**
  2669. * Handles horizontal positioning in rtl or ltr environments.
  2670. * @param {string} position Horizontal position of the panel
  2671. * @returns {string} The correct position according the page direction
  2672. * @private
  2673. */
  2674. MdPanelPosition.prototype._bidi = function(position) {
  2675. return this._isRTL ? this._reverseXPosition(position) : position;
  2676. };
  2677. /**
  2678. * Calculates the panel position based on the created panel element and the
  2679. * provided positioning.
  2680. * @param {!angular.JQLite} panelEl
  2681. * @param {!{x:string, y:string}} position
  2682. * @private
  2683. */
  2684. MdPanelPosition.prototype._calculatePanelPosition = function(panelEl, position) {
  2685. var panelBounds = panelEl[0].getBoundingClientRect();
  2686. var panelWidth = Math.max(panelBounds.width, panelEl[0].clientWidth);
  2687. var panelHeight = Math.max(panelBounds.height, panelEl[0].clientHeight);
  2688. var targetBounds = this._relativeToEl[0].getBoundingClientRect();
  2689. var targetLeft = targetBounds.left;
  2690. var targetRight = targetBounds.right;
  2691. var targetWidth = targetBounds.width;
  2692. switch (this._bidi(position.x)) {
  2693. case MdPanelPosition.xPosition.OFFSET_START:
  2694. this._left = targetLeft - panelWidth + 'px';
  2695. break;
  2696. case MdPanelPosition.xPosition.ALIGN_END:
  2697. this._left = targetRight - panelWidth + 'px';
  2698. break;
  2699. case MdPanelPosition.xPosition.CENTER:
  2700. var left = targetLeft + (0.5 * targetWidth) - (0.5 * panelWidth);
  2701. this._left = left + 'px';
  2702. break;
  2703. case MdPanelPosition.xPosition.ALIGN_START:
  2704. this._left = targetLeft + 'px';
  2705. break;
  2706. case MdPanelPosition.xPosition.OFFSET_END:
  2707. this._left = targetRight + 'px';
  2708. break;
  2709. }
  2710. var targetTop = targetBounds.top;
  2711. var targetBottom = targetBounds.bottom;
  2712. var targetHeight = targetBounds.height;
  2713. switch (position.y) {
  2714. case MdPanelPosition.yPosition.ABOVE:
  2715. this._top = targetTop - panelHeight + 'px';
  2716. break;
  2717. case MdPanelPosition.yPosition.ALIGN_BOTTOMS:
  2718. this._top = targetBottom - panelHeight + 'px';
  2719. break;
  2720. case MdPanelPosition.yPosition.CENTER:
  2721. var top = targetTop + (0.5 * targetHeight) - (0.5 * panelHeight);
  2722. this._top = top + 'px';
  2723. break;
  2724. case MdPanelPosition.yPosition.ALIGN_TOPS:
  2725. this._top = targetTop + 'px';
  2726. break;
  2727. case MdPanelPosition.yPosition.BELOW:
  2728. this._top = targetBottom + 'px';
  2729. break;
  2730. }
  2731. };
  2732. /*****************************************************************************
  2733. * MdPanelAnimation *
  2734. *****************************************************************************/
  2735. /**
  2736. * Animation configuration object. To use, create an MdPanelAnimation with the
  2737. * desired properties, then pass the object as part of $mdPanel creation.
  2738. *
  2739. * Example:
  2740. *
  2741. * var panelAnimation = new MdPanelAnimation()
  2742. * .openFrom(myButtonEl)
  2743. * .closeTo('.my-button')
  2744. * .withAnimation($mdPanel.animation.SCALE);
  2745. *
  2746. * $mdPanel.create({
  2747. * animation: panelAnimation
  2748. * });
  2749. *
  2750. * @param {!angular.$injector} $injector
  2751. * @final @constructor
  2752. */
  2753. function MdPanelAnimation($injector) {
  2754. /** @private @const {!angular.$mdUtil} */
  2755. this._$mdUtil = $injector.get('$mdUtil');
  2756. /**
  2757. * @private {{element: !angular.JQLite|undefined, bounds: !DOMRect}|
  2758. * undefined}
  2759. */
  2760. this._openFrom;
  2761. /**
  2762. * @private {{element: !angular.JQLite|undefined, bounds: !DOMRect}|
  2763. * undefined}
  2764. */
  2765. this._closeTo;
  2766. /** @private {string|{open: string, close: string}} */
  2767. this._animationClass = '';
  2768. /** @private {number} */
  2769. this._openDuration;
  2770. /** @private {number} */
  2771. this._closeDuration;
  2772. /** @private {number|{open: number, close: number}} */
  2773. this._rawDuration;
  2774. }
  2775. /**
  2776. * Possible default animations.
  2777. * @enum {string}
  2778. */
  2779. MdPanelAnimation.animation = {
  2780. SLIDE: 'md-panel-animate-slide',
  2781. SCALE: 'md-panel-animate-scale',
  2782. FADE: 'md-panel-animate-fade'
  2783. };
  2784. /**
  2785. * Specifies where to start the open animation. `openFrom` accepts a
  2786. * click event object, query selector, DOM element, or a Rect object that
  2787. * is used to determine the bounds. When passed a click event, the location
  2788. * of the click will be used as the position to start the animation.
  2789. * @param {string|!Element|!Event|{top: number, left: number}} openFrom
  2790. * @returns {!MdPanelAnimation}
  2791. */
  2792. MdPanelAnimation.prototype.openFrom = function(openFrom) {
  2793. // Check if 'openFrom' is an Event.
  2794. openFrom = openFrom.target ? openFrom.target : openFrom;
  2795. this._openFrom = this._getPanelAnimationTarget(openFrom);
  2796. if (!this._closeTo) {
  2797. this._closeTo = this._openFrom;
  2798. }
  2799. return this;
  2800. };
  2801. /**
  2802. * Specifies where to animate the panel close. `closeTo` accepts a
  2803. * query selector, DOM element, or a Rect object that is used to determine
  2804. * the bounds.
  2805. * @param {string|!Element|{top: number, left: number}} closeTo
  2806. * @returns {!MdPanelAnimation}
  2807. */
  2808. MdPanelAnimation.prototype.closeTo = function(closeTo) {
  2809. this._closeTo = this._getPanelAnimationTarget(closeTo);
  2810. return this;
  2811. };
  2812. /**
  2813. * Specifies the duration of the animation in milliseconds.
  2814. * @param {number|{open: number, close: number}} duration
  2815. * @returns {!MdPanelAnimation}
  2816. */
  2817. MdPanelAnimation.prototype.duration = function(duration) {
  2818. if (duration) {
  2819. if (angular.isNumber(duration)) {
  2820. this._openDuration = this._closeDuration = toSeconds(duration);
  2821. } else if (angular.isObject(duration)) {
  2822. this._openDuration = toSeconds(duration.open);
  2823. this._closeDuration = toSeconds(duration.close);
  2824. }
  2825. }
  2826. // Save the original value so it can be passed to the backdrop.
  2827. this._rawDuration = duration;
  2828. return this;
  2829. function toSeconds(value) {
  2830. if (angular.isNumber(value)) return value / 1000;
  2831. }
  2832. };
  2833. /**
  2834. * Returns the element and bounds for the animation target.
  2835. * @param {string|!Element|{top: number, left: number}} location
  2836. * @returns {{element: !angular.JQLite|undefined, bounds: !DOMRect}}
  2837. * @private
  2838. */
  2839. MdPanelAnimation.prototype._getPanelAnimationTarget = function(location) {
  2840. if (angular.isDefined(location.top) || angular.isDefined(location.left)) {
  2841. return {
  2842. element: undefined,
  2843. bounds: {
  2844. top: location.top || 0,
  2845. left: location.left || 0
  2846. }
  2847. };
  2848. } else {
  2849. return this._getBoundingClientRect(getElement(location));
  2850. }
  2851. };
  2852. /**
  2853. * Specifies the animation class.
  2854. *
  2855. * There are several default animations that can be used:
  2856. * (MdPanelAnimation.animation)
  2857. * SLIDE: The panel slides in and out from the specified
  2858. * elements.
  2859. * SCALE: The panel scales in and out.
  2860. * FADE: The panel fades in and out.
  2861. *
  2862. * @param {string|{open: string, close: string}} cssClass
  2863. * @returns {!MdPanelAnimation}
  2864. */
  2865. MdPanelAnimation.prototype.withAnimation = function(cssClass) {
  2866. this._animationClass = cssClass;
  2867. return this;
  2868. };
  2869. /**
  2870. * Animate the panel open.
  2871. * @param {!angular.JQLite} panelEl
  2872. * @returns {!angular.$q.Promise} A promise that is resolved when the open
  2873. * animation is complete.
  2874. */
  2875. MdPanelAnimation.prototype.animateOpen = function(panelEl) {
  2876. var animator = this._$mdUtil.dom.animator;
  2877. this._fixBounds(panelEl);
  2878. var animationOptions = {};
  2879. // Include the panel transformations when calculating the animations.
  2880. var panelTransform = panelEl[0].style.transform || '';
  2881. var openFrom = animator.toTransformCss(panelTransform);
  2882. var openTo = animator.toTransformCss(panelTransform);
  2883. switch (this._animationClass) {
  2884. case MdPanelAnimation.animation.SLIDE:
  2885. // Slide should start with opacity: 1.
  2886. panelEl.css('opacity', '1');
  2887. animationOptions = {
  2888. transitionInClass: '_md-panel-animate-enter'
  2889. };
  2890. var openSlide = animator.calculateSlideToOrigin(
  2891. panelEl, this._openFrom) || '';
  2892. openFrom = animator.toTransformCss(openSlide + ' ' + panelTransform);
  2893. break;
  2894. case MdPanelAnimation.animation.SCALE:
  2895. animationOptions = {
  2896. transitionInClass: '_md-panel-animate-enter'
  2897. };
  2898. var openScale = animator.calculateZoomToOrigin(
  2899. panelEl, this._openFrom) || '';
  2900. openFrom = animator.toTransformCss(openScale + ' ' + panelTransform);
  2901. break;
  2902. case MdPanelAnimation.animation.FADE:
  2903. animationOptions = {
  2904. transitionInClass: '_md-panel-animate-enter'
  2905. };
  2906. break;
  2907. default:
  2908. if (angular.isString(this._animationClass)) {
  2909. animationOptions = {
  2910. transitionInClass: this._animationClass
  2911. };
  2912. } else {
  2913. animationOptions = {
  2914. transitionInClass: this._animationClass['open'],
  2915. transitionOutClass: this._animationClass['close'],
  2916. };
  2917. }
  2918. }
  2919. animationOptions.duration = this._openDuration;
  2920. return animator
  2921. .translate3d(panelEl, openFrom, openTo, animationOptions);
  2922. };
  2923. /**
  2924. * Animate the panel close.
  2925. * @param {!angular.JQLite} panelEl
  2926. * @returns {!angular.$q.Promise} A promise that resolves when the close
  2927. * animation is complete.
  2928. */
  2929. MdPanelAnimation.prototype.animateClose = function(panelEl) {
  2930. var animator = this._$mdUtil.dom.animator;
  2931. var reverseAnimationOptions = {};
  2932. // Include the panel transformations when calculating the animations.
  2933. var panelTransform = panelEl[0].style.transform || '';
  2934. var closeFrom = animator.toTransformCss(panelTransform);
  2935. var closeTo = animator.toTransformCss(panelTransform);
  2936. switch (this._animationClass) {
  2937. case MdPanelAnimation.animation.SLIDE:
  2938. // Slide should start with opacity: 1.
  2939. panelEl.css('opacity', '1');
  2940. reverseAnimationOptions = {
  2941. transitionInClass: '_md-panel-animate-leave'
  2942. };
  2943. var closeSlide = animator.calculateSlideToOrigin(
  2944. panelEl, this._closeTo) || '';
  2945. closeTo = animator.toTransformCss(closeSlide + ' ' + panelTransform);
  2946. break;
  2947. case MdPanelAnimation.animation.SCALE:
  2948. reverseAnimationOptions = {
  2949. transitionInClass: '_md-panel-animate-scale-out _md-panel-animate-leave'
  2950. };
  2951. var closeScale = animator.calculateZoomToOrigin(
  2952. panelEl, this._closeTo) || '';
  2953. closeTo = animator.toTransformCss(closeScale + ' ' + panelTransform);
  2954. break;
  2955. case MdPanelAnimation.animation.FADE:
  2956. reverseAnimationOptions = {
  2957. transitionInClass: '_md-panel-animate-fade-out _md-panel-animate-leave'
  2958. };
  2959. break;
  2960. default:
  2961. if (angular.isString(this._animationClass)) {
  2962. reverseAnimationOptions = {
  2963. transitionOutClass: this._animationClass
  2964. };
  2965. } else {
  2966. reverseAnimationOptions = {
  2967. transitionInClass: this._animationClass['close'],
  2968. transitionOutClass: this._animationClass['open']
  2969. };
  2970. }
  2971. }
  2972. reverseAnimationOptions.duration = this._closeDuration;
  2973. return animator
  2974. .translate3d(panelEl, closeFrom, closeTo, reverseAnimationOptions);
  2975. };
  2976. /**
  2977. * Set the height and width to match the panel if not provided.
  2978. * @param {!angular.JQLite} panelEl
  2979. * @private
  2980. */
  2981. MdPanelAnimation.prototype._fixBounds = function(panelEl) {
  2982. var panelWidth = panelEl[0].offsetWidth;
  2983. var panelHeight = panelEl[0].offsetHeight;
  2984. if (this._openFrom && this._openFrom.bounds.height == null) {
  2985. this._openFrom.bounds.height = panelHeight;
  2986. }
  2987. if (this._openFrom && this._openFrom.bounds.width == null) {
  2988. this._openFrom.bounds.width = panelWidth;
  2989. }
  2990. if (this._closeTo && this._closeTo.bounds.height == null) {
  2991. this._closeTo.bounds.height = panelHeight;
  2992. }
  2993. if (this._closeTo && this._closeTo.bounds.width == null) {
  2994. this._closeTo.bounds.width = panelWidth;
  2995. }
  2996. };
  2997. /**
  2998. * Identify the bounding RECT for the target element.
  2999. * @param {!angular.JQLite} element
  3000. * @returns {{element: !angular.JQLite|undefined, bounds: !DOMRect}}
  3001. * @private
  3002. */
  3003. MdPanelAnimation.prototype._getBoundingClientRect = function(element) {
  3004. if (element instanceof angular.element) {
  3005. return {
  3006. element: element,
  3007. bounds: element[0].getBoundingClientRect()
  3008. };
  3009. }
  3010. };
  3011. /*****************************************************************************
  3012. * Util Methods *
  3013. *****************************************************************************/
  3014. /**
  3015. * Returns the angular element associated with a css selector or element.
  3016. * @param el {string|!angular.JQLite|!Element}
  3017. * @returns {!angular.JQLite}
  3018. */
  3019. function getElement(el) {
  3020. var queryResult = angular.isString(el) ?
  3021. document.querySelector(el) : el;
  3022. return angular.element(queryResult);
  3023. }
  3024. /**
  3025. * Gets the computed values for an element's translateX and translateY in px.
  3026. * @param {!angular.JQLite|!Element} el
  3027. * @param {string} property
  3028. * @return {{x: number, y: number}}
  3029. */
  3030. function getComputedTranslations(el, property) {
  3031. // The transform being returned by `getComputedStyle` is in the format:
  3032. // `matrix(a, b, c, d, translateX, translateY)` if defined and `none`
  3033. // if the element doesn't have a transform.
  3034. var transform = getComputedStyle(el[0] || el)[property];
  3035. var openIndex = transform.indexOf('(');
  3036. var closeIndex = transform.lastIndexOf(')');
  3037. var output = { x: 0, y: 0 };
  3038. if (openIndex > -1 && closeIndex > -1) {
  3039. var parsedValues = transform
  3040. .substring(openIndex + 1, closeIndex)
  3041. .split(', ')
  3042. .slice(-2);
  3043. output.x = parseInt(parsedValues[0]);
  3044. output.y = parseInt(parsedValues[1]);
  3045. }
  3046. return output;
  3047. }
  3048. /**
  3049. * Adds units to a number value.
  3050. * @param {string|number} value
  3051. * @return {string}
  3052. */
  3053. function addUnits(value) {
  3054. return angular.isNumber(value) ? value + 'px' : value;
  3055. }
  3056. })(window, window.angular);