├── .gitignore ├── .prettierrc.js ├── .travis.yml ├── LICENSE ├── babel.config.js ├── demo ├── demo-images │ ├── 1.jpg │ ├── 1280x720.png │ ├── 1920x1080.png │ ├── 2.jpg │ ├── 3.jpg │ ├── 320x180.png │ ├── 4.jpg │ ├── 5.jpg │ ├── 6.jpg │ ├── 640x360.png │ ├── 7.jpg │ ├── 75x50.png │ ├── 8.jpg │ └── 960x540.png └── index.html ├── dist ├── css │ └── chocolat.css └── js │ ├── chocolat.cjs.js │ ├── chocolat.esm.js │ └── chocolat.js ├── package-lock.json ├── package.json ├── readme.md ├── rollup.config.js ├── src ├── css │ └── chocolat.css ├── icons │ ├── icon-close.svg │ ├── icon-fullscreen.svg │ ├── icon-left.svg │ ├── icon-loader.svg │ └── icon-right.svg └── js │ ├── chocolat.js │ ├── main.esm.js │ ├── main.iife.js │ └── utils.js └── test ├── index.html └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .idea/ 3 | .DS_Store -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | trailingComma: 'es5', 3 | tabWidth: 4, 4 | bracketSpacing: true, 5 | useTabs: false, 6 | printWidth: 100, 7 | semi: false, 8 | arrowParens: 'always', 9 | singleQuote: true 10 | } -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "10" 4 | install: 5 | - yarn install 6 | before_script: 7 | - sleep 5 8 | script: 9 | - prettier-check src/js/*.js test/*.js 10 | - yarn test 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | presets: [ 3 | [ 4 | "@babel/preset-env", 5 | { 6 | targets: { 7 | browsers: "> 0.5%" 8 | }, 9 | useBuiltIns: "usage", 10 | forceAllTransforms: true, 11 | corejs: { 12 | version: 3, 13 | proposals: false 14 | } 15 | } 16 | ] 17 | ] 18 | }; -------------------------------------------------------------------------------- /demo/demo-images/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicolas-t/Chocolat/82e0dfb97bfa919ba1a44ef1516524044ebbd475/demo/demo-images/1.jpg -------------------------------------------------------------------------------- /demo/demo-images/1280x720.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicolas-t/Chocolat/82e0dfb97bfa919ba1a44ef1516524044ebbd475/demo/demo-images/1280x720.png -------------------------------------------------------------------------------- /demo/demo-images/1920x1080.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicolas-t/Chocolat/82e0dfb97bfa919ba1a44ef1516524044ebbd475/demo/demo-images/1920x1080.png -------------------------------------------------------------------------------- /demo/demo-images/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicolas-t/Chocolat/82e0dfb97bfa919ba1a44ef1516524044ebbd475/demo/demo-images/2.jpg -------------------------------------------------------------------------------- /demo/demo-images/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicolas-t/Chocolat/82e0dfb97bfa919ba1a44ef1516524044ebbd475/demo/demo-images/3.jpg -------------------------------------------------------------------------------- /demo/demo-images/320x180.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicolas-t/Chocolat/82e0dfb97bfa919ba1a44ef1516524044ebbd475/demo/demo-images/320x180.png -------------------------------------------------------------------------------- /demo/demo-images/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicolas-t/Chocolat/82e0dfb97bfa919ba1a44ef1516524044ebbd475/demo/demo-images/4.jpg -------------------------------------------------------------------------------- /demo/demo-images/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicolas-t/Chocolat/82e0dfb97bfa919ba1a44ef1516524044ebbd475/demo/demo-images/5.jpg -------------------------------------------------------------------------------- /demo/demo-images/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicolas-t/Chocolat/82e0dfb97bfa919ba1a44ef1516524044ebbd475/demo/demo-images/6.jpg -------------------------------------------------------------------------------- /demo/demo-images/640x360.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicolas-t/Chocolat/82e0dfb97bfa919ba1a44ef1516524044ebbd475/demo/demo-images/640x360.png -------------------------------------------------------------------------------- /demo/demo-images/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicolas-t/Chocolat/82e0dfb97bfa919ba1a44ef1516524044ebbd475/demo/demo-images/7.jpg -------------------------------------------------------------------------------- /demo/demo-images/75x50.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicolas-t/Chocolat/82e0dfb97bfa919ba1a44ef1516524044ebbd475/demo/demo-images/75x50.png -------------------------------------------------------------------------------- /demo/demo-images/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicolas-t/Chocolat/82e0dfb97bfa919ba1a44ef1516524044ebbd475/demo/demo-images/8.jpg -------------------------------------------------------------------------------- /demo/demo-images/960x540.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicolas-t/Chocolat/82e0dfb97bfa919ba1a44ef1516524044ebbd475/demo/demo-images/960x540.png -------------------------------------------------------------------------------- /demo/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 25 | Chocolat — demo 26 | 27 | 28 | 29 | 32 | 33 |

34 | Full window, contain, click the numbers to open: 35 |

36 | 47 | 48 |

49 | Full window, cover, looping : 50 |

51 | 59 | 60 |

61 | Display in a container, cover, with thumbnails : 62 |

63 | 64 | 75 | 76 |
77 | 78 |

79 | Display in a container, looping : 80 |

81 | 82 | 93 | 94 |
95 | 96 | 97 |

98 | API use, in container, see code source
99 | Open console to have details abouts events 100 |

101 | 102 | Open then try
103 | Next 104 | Prev 105 |
106 | Enter cover mode / 107 | Enter contain mode 108 | 109 |
110 |
111 | Close 112 | 113 |
114 | 115 |

Usage with srcset and sizes attributes. Full window, contained.

116 |

Be careful while using "cover" and data-sizes="100vw", the image could be wider than the viewport width.

117 |
118 | 125 | 126 | 127 |
128 | 129 | 130 | 156 | 256 | 257 | 258 | 259 | -------------------------------------------------------------------------------- /dist/css/chocolat.css: -------------------------------------------------------------------------------- 1 | :root { 2 | --chocolat-icon-left: url("data:image/svg+xml,%3Csvg xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22 viewBox%3D%220 0 32 32%22 stroke-linecap%3D%22round%22 stroke-linejoin%3D%22round%22%3E %3Ccircle cx%3D%2216%22 cy%3D%2216%22 r%3D%2216%22 fill-opacity%3D%22.2%22%2F%3E %3Cpath d%3D%22M15.3 20.7 10.7 16l4.6-4.7m7.4 4.7h-12%22 fill%3D%22none%22 stroke%3D%22%23fff%22 stroke-width%3D%221.3%22%2F%3E%3C%2Fsvg%3E"); 3 | --chocolat-icon-right: url("data:image/svg+xml,%3Csvg xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22 viewBox%3D%220 0 32 32%22 stroke-linecap%3D%22round%22 stroke-linejoin%3D%22round%22%3E %3Ccircle cx%3D%2216%22 cy%3D%2216%22 r%3D%2216%22 fill-opacity%3D%22.2%22%2F%3E %3Cpath d%3D%22m16.7 11.3 4.6 4.7-4.6 4.7M9.3 16h12%22 fill%3D%22none%22 stroke%3D%22%23fff%22 stroke-width%3D%221.3%22%2F%3E%3C%2Fsvg%3E"); 4 | --chocolat-icon-close: url("data:image/svg+xml,%3Csvg xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22 viewBox%3D%220 0 32 32%22 stroke-linecap%3D%22round%22%3E %3Ccircle cx%3D%2216%22 cy%3D%2216%22 r%3D%2216%22 fill-opacity%3D%22.2%22%2F%3E %3Cpath d%3D%22m12 12 8 8m0-8-8 8%22 fill%3D%22none%22 stroke%3D%22%23fff%22 stroke-width%3D%221.3%22%2F%3E%3C%2Fsvg%3E"); 5 | --chocolat-icon-loader: url("data:image/svg+xml,%3Csvg xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22 width%3D%2238%22 height%3D%2238%22%3E %3Cg transform%3D%22translate(1 1)%22 stroke-width%3D%222%22 fill%3D%22none%22 fill-rule%3D%22evenodd%22%3E %3Ccircle stroke-opacity%3D%22.2%22 stroke%3D%22%23000%22 cx%3D%2218%22 cy%3D%2218%22 r%3D%2218%22%2F%3E %3Cpath d%3D%22M36 18C36 8 28 0 18 0%22 stroke%3D%22%23fff%22%3E %3CanimateTransform attributeName%3D%22transform%22 type%3D%22rotate%22 from%3D%220 18 18%22 to%3D%22360 18 18%22 dur%3D%221s%22 repeatCount%3D%22indefinite%22%2F%3E %3C%2Fpath%3E %3C%2Fg%3E%3C%2Fsvg%3E"); 6 | --chocolat-icon-fullscreen: url("data:image/svg+xml,%3Csvg xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22 viewBox%3D%220 0 16 16%22%3E %3Cg stroke%3D%22%23fff%22 fill%3D%22none%22%3E %3Cpath d%3D%22M.5 2.5h15v11H.5z%22%2F%3E %3Cpath d%3D%22M7.5 7.5h8v6h-8z%22%2F%3E %3C%2Fg%3E%3C%2Fsvg%3E"); 7 | --chocolat-transition-time: 0.4s; 8 | } 9 | 10 | .chocolat-zoomable.chocolat-zoomed { 11 | cursor: zoom-out; 12 | } 13 | .chocolat-open { 14 | overflow: hidden; 15 | } 16 | .chocolat-overlay { 17 | transition: opacity var(--chocolat-transition-time) ease, visibility 0s var(--chocolat-transition-time) ease; 18 | height: 100%; 19 | width: 100%; 20 | position: fixed; 21 | left: 0; 22 | top: 0; 23 | z-index: 10; 24 | background-color: #fff; 25 | visibility: hidden; 26 | opacity: 0; 27 | } 28 | .chocolat-overlay.chocolat-visible { 29 | transition: opacity var(--chocolat-transition-time), visibility 0s; 30 | visibility: visible; 31 | opacity: 0.8; 32 | } 33 | 34 | .chocolat-wrapper { 35 | transition: opacity var(--chocolat-transition-time) ease, visibility 0s var(--chocolat-transition-time) ease; 36 | width: 100%; 37 | height: 100%; 38 | position: fixed; 39 | opacity: 0; 40 | left: 0; 41 | top: 0; 42 | z-index: 16; 43 | color: #fff; 44 | visibility: hidden; 45 | } 46 | .chocolat-wrapper.chocolat-visible { 47 | transition: opacity var(--chocolat-transition-time), visibility 0s; 48 | opacity: 1; 49 | visibility: visible; 50 | } 51 | 52 | .chocolat-zoomable .chocolat-img { 53 | cursor: zoom-in; 54 | } 55 | .chocolat-loader { 56 | transition: opacity 0.3s; 57 | height: 38px; 58 | width: 38px; 59 | position: absolute; 60 | left: 50%; 61 | top: 50%; 62 | margin-left: -19px; 63 | margin-top: -19px; 64 | z-index: 11; 65 | background: var(--chocolat-icon-loader); 66 | opacity: 0; 67 | } 68 | .chocolat-loader.chocolat-visible { 69 | opacity: 1; 70 | } 71 | 72 | .chocolat-image-wrapper { 73 | position: fixed; 74 | width: 0px; 75 | height: 0px; 76 | left: 50%; 77 | top: 50%; 78 | z-index: 14; 79 | text-align: left; 80 | transform: translate(-50%, -50%); 81 | } 82 | 83 | .chocolat-image-wrapper .chocolat-img { 84 | position: absolute; 85 | width: 100%; 86 | height: 100%; 87 | } 88 | .chocolat-wrapper .chocolat-left { 89 | width: 50px; 90 | height: 100px; 91 | cursor: pointer; 92 | background: var(--chocolat-icon-left) 50% 50% / 2rem auto no-repeat; 93 | z-index: 17; 94 | visibility: hidden; 95 | } 96 | 97 | .chocolat-layout { 98 | display: flex; 99 | flex-direction: column; 100 | position: absolute; 101 | top: 0; 102 | bottom: 0; 103 | left: 0; 104 | right: 0; 105 | } 106 | .chocolat-image-canvas { 107 | transition: opacity .2s; 108 | opacity: 0; 109 | flex-grow: 1; 110 | align-self: stretch; 111 | } 112 | .chocolat-image-canvas.chocolat-visible { 113 | opacity: 1; 114 | } 115 | .chocolat-center { 116 | flex-grow: 1; 117 | display: flex; 118 | justify-content: center; 119 | align-items: center; 120 | user-select: none; 121 | } 122 | 123 | .chocolat-wrapper .chocolat-right { 124 | width: 50px; 125 | height: 100px; 126 | cursor: pointer; 127 | background: var(--chocolat-icon-right) 50% 50% / 2rem auto no-repeat; 128 | z-index: 17; 129 | visibility: hidden; 130 | } 131 | .chocolat-wrapper .chocolat-right.active { 132 | visibility: visible; 133 | } 134 | .chocolat-wrapper .chocolat-left.active { 135 | visibility: visible; 136 | } 137 | .chocolat-wrapper .chocolat-top { 138 | height: 50px; 139 | overflow: hidden; 140 | z-index: 17; 141 | flex-shrink: 0; 142 | } 143 | .chocolat-wrapper .chocolat-close { 144 | width: 50px; 145 | height: 50px; 146 | cursor: pointer; 147 | position: absolute; 148 | top: 0; 149 | right: 0; 150 | background: var(--chocolat-icon-close) 50% 50% / 2rem auto no-repeat; 151 | } 152 | .chocolat-wrapper .chocolat-bottom { 153 | height: 40px; 154 | font-size: 12px; 155 | z-index: 17; 156 | padding-left: 15px; 157 | padding-right: 15px; 158 | background: rgba(0, 0, 0, 0.2); 159 | flex-shrink: 0; 160 | display: flex; 161 | align-items: center; 162 | 163 | } 164 | .chocolat-wrapper .chocolat-set-title { 165 | display: inline-block; 166 | padding-right: 15px; 167 | line-height: 1; 168 | border-right: 1px solid rgba(255, 255, 255, 0.3); 169 | } 170 | .chocolat-wrapper .chocolat-pagination { 171 | float: right; 172 | display: inline-block; 173 | padding-left: 15px; 174 | padding-right: 15px; 175 | margin-right: 15px; 176 | /*border-right: 1px solid rgba(255, 255, 255, 0.2);*/ 177 | } 178 | .chocolat-wrapper .chocolat-fullscreen { 179 | width: 16px; 180 | height: 40px; 181 | background: var(--chocolat-icon-fullscreen) 50% 50% / 1rem auto no-repeat; 182 | display: block; 183 | cursor: pointer; 184 | float: right; 185 | } 186 | .chocolat-wrapper .chocolat-description { 187 | display: inline-block; 188 | flex-grow: 1; 189 | text-align: left; 190 | } 191 | 192 | /* no container mode*/ 193 | body.chocolat-open > .chocolat-overlay { 194 | z-index: 15; 195 | } 196 | body.chocolat-open > .chocolat-loader { 197 | z-index: 15; 198 | } 199 | body.chocolat-open > .chocolat-image-wrapper { 200 | z-index: 17; 201 | } 202 | 203 | /* container mode*/ 204 | .chocolat-in-container .chocolat-wrapper, 205 | .chocolat-in-container .chocolat-image-wrapper, 206 | .chocolat-in-container .chocolat-overlay { 207 | position: absolute; 208 | } 209 | .chocolat-in-container { 210 | position: relative; 211 | } 212 | 213 | .chocolat-zoomable.chocolat-zooming-in .chocolat-image-wrapper, 214 | .chocolat-zoomable.chocolat-zooming-out .chocolat-image-wrapper { 215 | transition: width .2s ease, height .2s ease; 216 | } 217 | .chocolat-zoomable.chocolat-zooming-in .chocolat-img, 218 | .chocolat-zoomable.chocolat-zooming-out .chocolat-img { 219 | transition: margin .2s ease; 220 | } 221 | 222 | /* uncomment to hide controls when zoomed-in*/ 223 | /* 224 | .chocolat-zoomable .chocolat-top, 225 | .chocolat-zoomable .chocolat-bottom, 226 | .chocolat-zoomable .chocolat-right, 227 | .chocolat-zoomable .chocolat-left { 228 | transition: opacity .3s ease, visibility 0s .3s; 229 | opacity: 1; 230 | } 231 | 232 | .chocolat-zoomable.chocolat-zoomed .chocolat-top, 233 | .chocolat-zoomable.chocolat-zoomed .chocolat-bottom, 234 | .chocolat-zoomable.chocolat-zoomed .chocolat-right, 235 | .chocolat-zoomable.chocolat-zoomed .chocolat-left { 236 | visibility: hidden; 237 | opacity: 0; 238 | } 239 | */ 240 | -------------------------------------------------------------------------------- /dist/js/chocolat.cjs.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('core-js/modules/web.dom-collections.iterator.js'); 4 | 5 | function _classCallCheck(instance, Constructor) { 6 | if (!(instance instanceof Constructor)) { 7 | throw new TypeError("Cannot call a class as a function"); 8 | } 9 | } 10 | 11 | function _defineProperties(target, props) { 12 | for (var i = 0; i < props.length; i++) { 13 | var descriptor = props[i]; 14 | descriptor.enumerable = descriptor.enumerable || false; 15 | descriptor.configurable = true; 16 | if ("value" in descriptor) descriptor.writable = true; 17 | Object.defineProperty(target, descriptor.key, descriptor); 18 | } 19 | } 20 | 21 | function _createClass(Constructor, protoProps, staticProps) { 22 | if (protoProps) _defineProperties(Constructor.prototype, protoProps); 23 | if (staticProps) _defineProperties(Constructor, staticProps); 24 | return Constructor; 25 | } 26 | 27 | function _toConsumableArray(arr) { 28 | return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); 29 | } 30 | 31 | function _arrayWithoutHoles(arr) { 32 | if (Array.isArray(arr)) return _arrayLikeToArray(arr); 33 | } 34 | 35 | function _iterableToArray(iter) { 36 | if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); 37 | } 38 | 39 | function _unsupportedIterableToArray(o, minLen) { 40 | if (!o) return; 41 | if (typeof o === "string") return _arrayLikeToArray(o, minLen); 42 | var n = Object.prototype.toString.call(o).slice(8, -1); 43 | if (n === "Object" && o.constructor) n = o.constructor.name; 44 | if (n === "Map" || n === "Set") return Array.from(o); 45 | if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); 46 | } 47 | 48 | function _arrayLikeToArray(arr, len) { 49 | if (len == null || len > arr.length) len = arr.length; 50 | 51 | for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; 52 | 53 | return arr2; 54 | } 55 | 56 | function _nonIterableSpread() { 57 | throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); 58 | } 59 | 60 | var timerDebounce = undefined; 61 | function debounce(duration, callback) { 62 | clearTimeout(timerDebounce); 63 | timerDebounce = setTimeout(function () { 64 | callback(); 65 | }, duration); 66 | return timerDebounce; 67 | } 68 | function transitionAsPromise(triggeringFunc, el) { 69 | return new Promise(function (resolve) { 70 | var handleTransitionEnd = function handleTransitionEnd() { 71 | el.removeEventListener('transitionend', handleTransitionEnd); 72 | resolve(); 73 | }; 74 | 75 | el.addEventListener('transitionend', handleTransitionEnd); 76 | var classesBefore = el.getAttribute('class'); 77 | var stylesBefore = el.getAttribute('style'); 78 | triggeringFunc(); 79 | 80 | if (classesBefore === el.getAttribute('class') && stylesBefore === el.getAttribute('style')) { 81 | handleTransitionEnd(); 82 | } 83 | 84 | if (parseFloat(getComputedStyle(el)['transitionDuration']) === 0) { 85 | handleTransitionEnd(); 86 | } 87 | }); 88 | } 89 | function loadImage(_ref) { 90 | var src = _ref.src, 91 | srcset = _ref.srcset, 92 | sizes = _ref.sizes; 93 | var image = new Image(); 94 | image.src = src; 95 | 96 | if (srcset) { 97 | image.srcset = srcset; 98 | } 99 | 100 | if (sizes) { 101 | image.sizes = sizes; 102 | } 103 | 104 | if ('decode' in image) { 105 | return new Promise(function (resolve, reject) { 106 | image.decode().then(function () { 107 | resolve(image); 108 | })["catch"](function () { 109 | reject(image); 110 | }); 111 | }); 112 | } else { 113 | return new Promise(function (resolve, reject) { 114 | image.onload = resolve(image); 115 | image.onerror = reject(image); 116 | }); 117 | } 118 | } 119 | function fit(options) { 120 | var height; 121 | var width; 122 | var imgHeight = options.imgHeight, 123 | imgWidth = options.imgWidth, 124 | containerHeight = options.containerHeight, 125 | containerWidth = options.containerWidth, 126 | canvasWidth = options.canvasWidth, 127 | canvasHeight = options.canvasHeight, 128 | imageSize = options.imageSize; 129 | var canvasRatio = canvasHeight / canvasWidth; 130 | var containerRatio = containerHeight / containerWidth; 131 | var imgRatio = imgHeight / imgWidth; 132 | 133 | if (imageSize == 'cover') { 134 | if (imgRatio < containerRatio) { 135 | height = containerHeight; 136 | width = height / imgRatio; 137 | } else { 138 | width = containerWidth; 139 | height = width * imgRatio; 140 | } 141 | } else if (imageSize == 'native') { 142 | height = imgHeight; 143 | width = imgWidth; 144 | } else { 145 | if (imgRatio > canvasRatio) { 146 | height = canvasHeight; 147 | width = height / imgRatio; 148 | } else { 149 | width = canvasWidth; 150 | height = width * imgRatio; 151 | } 152 | 153 | if (imageSize === 'scale-down' && (width >= imgWidth || height >= imgHeight)) { 154 | width = imgWidth; 155 | height = imgHeight; 156 | } 157 | } 158 | 159 | return { 160 | height: height, 161 | width: width 162 | }; 163 | } 164 | function openFullScreen(wrapper) { 165 | if (wrapper.requestFullscreen) { 166 | return wrapper.requestFullscreen(); 167 | } else if (wrapper.webkitRequestFullscreen) { 168 | return wrapper.webkitRequestFullscreen(); 169 | } else if (wrapper.msRequestFullscreen) { 170 | return wrapper.msRequestFullscreen(); 171 | } else { 172 | return Promise.reject(); 173 | } 174 | } 175 | function exitFullScreen() { 176 | if (document.exitFullscreen) { 177 | return document.exitFullscreen(); 178 | } else if (document.webkitExitFullscreen) { 179 | return document.webkitExitFullscreen(); 180 | } else if (document.msExitFullscreen) { 181 | return document.msExitFullscreen(); 182 | } else { 183 | return Promise.reject(); 184 | } 185 | } 186 | 187 | var defaults = { 188 | container: document.body, 189 | // window or element 190 | className: undefined, 191 | imageSize: 'scale-down', 192 | // 'scale-down', 'contain', 'cover' or 'native' 193 | fullScreen: false, 194 | loop: false, 195 | linkImages: true, 196 | setIndex: 0, 197 | firstImageIndex: 0, 198 | lastImageIndex: false, 199 | currentImageIndex: undefined, 200 | allowZoom: true, 201 | closeOnBackgroundClick: true, 202 | imageSourceAttribute: 'href', 203 | setTitle: function setTitle() { 204 | return ''; 205 | }, 206 | description: function description() { 207 | return this.images[this.settings.currentImageIndex].title; 208 | }, 209 | pagination: function pagination() { 210 | var last = this.settings.lastImageIndex + 1; 211 | var position = this.settings.currentImageIndex + 1; 212 | return position + '/' + last; 213 | }, 214 | afterInitialize: function afterInitialize() {}, 215 | afterMarkup: function afterMarkup() {}, 216 | beforeImageLoad: function beforeImageLoad() {}, 217 | afterImageLoad: function afterImageLoad() {}, 218 | afterClose: function afterClose() {}, 219 | zoomedPaddingX: function zoomedPaddingX(canvasWidth, imgWidth) { 220 | return 0; 221 | }, 222 | zoomedPaddingY: function zoomedPaddingY(canvasHeight, imgHeight) { 223 | return 0; 224 | } 225 | }; 226 | var Chocolat = /*#__PURE__*/function () { 227 | function Chocolat(elements, settings) { 228 | var _this = this; 229 | 230 | _classCallCheck(this, Chocolat); 231 | 232 | this.settings = settings; 233 | this.elems = {}; 234 | this.images = []; 235 | this.events = []; 236 | this.state = { 237 | fullScreenOpen: false, 238 | initialZoomState: null, 239 | initialized: false, 240 | timer: false, 241 | visible: false 242 | }; 243 | this._cssClasses = ['chocolat-open', 'chocolat-in-container', 'chocolat-cover', 'chocolat-zoomable', 'chocolat-zoomed', 'chocolat-zooming-in', 'chocolat-zooming-out']; 244 | 245 | if (NodeList.prototype.isPrototypeOf(elements) || HTMLCollection.prototype.isPrototypeOf(elements)) { 246 | elements.forEach(function (el, i) { 247 | _this.images.push({ 248 | title: el.getAttribute('title'), 249 | src: el.getAttribute(settings.imageSourceAttribute), 250 | srcset: el.getAttribute('data-srcset'), 251 | sizes: el.getAttribute('data-sizes') 252 | }); 253 | 254 | _this.off(el, 'click.chocolat'); 255 | 256 | _this.on(el, 'click.chocolat', function (e) { 257 | _this.init(i); 258 | 259 | e.preventDefault(); 260 | }); 261 | }); 262 | } else { 263 | this.images = elements; 264 | } 265 | 266 | if (this.settings.container instanceof Element || this.settings.container instanceof HTMLElement) { 267 | this.elems.container = this.settings.container; 268 | } else { 269 | this.elems.container = document.body; 270 | } 271 | 272 | this.api = { 273 | open: function open(i) { 274 | i = parseInt(i) || 0; 275 | return _this.init(i); 276 | }, 277 | close: function close() { 278 | return _this.close(); 279 | }, 280 | next: function next() { 281 | return _this.change(1); 282 | }, 283 | prev: function prev() { 284 | return _this.change(-1); 285 | }, 286 | "goto": function goto(i) { 287 | return _this.open(i); 288 | }, 289 | current: function current() { 290 | return _this.settings.currentImageIndex; 291 | }, 292 | position: function position() { 293 | return _this.position(_this.elems.img); 294 | }, 295 | destroy: function destroy() { 296 | return _this.destroy(); 297 | }, 298 | set: function set(property, value) { 299 | _this.settings[property] = value; 300 | return value; 301 | }, 302 | get: function get(property) { 303 | return _this.settings[property]; 304 | }, 305 | getElem: function getElem(name) { 306 | return _this.elems[name]; 307 | } 308 | }; 309 | } 310 | 311 | _createClass(Chocolat, [{ 312 | key: "init", 313 | value: function init(i) { 314 | if (!this.state.initialized) { 315 | this.markup(); 316 | this.attachListeners(); 317 | this.settings.lastImageIndex = this.images.length - 1; 318 | this.state.initialized = true; 319 | } 320 | 321 | this.settings.afterInitialize.call(this); 322 | return this.load(i); 323 | } 324 | }, { 325 | key: "load", 326 | value: function load(index) { 327 | var _this2 = this; 328 | 329 | this.settings.beforeImageLoad.call(this); 330 | 331 | if (!this.state.visible) { 332 | this.state.visible = true; 333 | setTimeout(function () { 334 | _this2.elems.overlay.classList.add('chocolat-visible'); 335 | 336 | _this2.elems.wrapper.classList.add('chocolat-visible'); 337 | }, 0); 338 | this.elems.container.classList.add('chocolat-open'); 339 | } 340 | 341 | if (this.settings.fullScreen) { 342 | openFullScreen(this.elems.wrapper); 343 | } 344 | 345 | if (this.settings.currentImageIndex === index) { 346 | return Promise.resolve(); 347 | } 348 | 349 | var loaderTimer = setTimeout(function () { 350 | _this2.elems.loader.classList.add('chocolat-visible'); 351 | }, 1000); 352 | var fadeOutPromise; 353 | var image; 354 | var fadeOutTimer = setTimeout(function () { 355 | fadeOutTimer = undefined; 356 | fadeOutPromise = transitionAsPromise(function () { 357 | _this2.elems.imageCanvas.classList.remove('chocolat-visible'); 358 | }, _this2.elems.imageCanvas); 359 | }, 80); 360 | return loadImage(this.images[index]).then(function (loadedImage) { 361 | image = loadedImage; 362 | 363 | if (fadeOutTimer) { 364 | clearTimeout(fadeOutTimer); 365 | return Promise.resolve(); 366 | } else { 367 | return fadeOutPromise; 368 | } 369 | }).then(function () { 370 | var nextIndex = index + 1; 371 | 372 | if (_this2.images[nextIndex] != undefined) { 373 | loadImage(_this2.images[nextIndex]); 374 | } 375 | 376 | _this2.settings.currentImageIndex = index; 377 | _this2.elems.description.textContent = _this2.settings.description.call(_this2); 378 | _this2.elems.pagination.textContent = _this2.settings.pagination.call(_this2); 379 | 380 | _this2.arrows(); 381 | 382 | return _this2.position(image).then(function () { 383 | _this2.elems.loader.classList.remove('chocolat-visible'); 384 | 385 | clearTimeout(loaderTimer); 386 | return _this2.appear(image); 387 | }); 388 | }).then(function () { 389 | _this2.elems.container.classList.toggle('chocolat-zoomable', _this2.zoomable(image, _this2.elems.wrapper)); 390 | 391 | _this2.settings.afterImageLoad.call(_this2); 392 | }); 393 | } 394 | }, { 395 | key: "position", 396 | value: function position(_ref) { 397 | var _this3 = this; 398 | 399 | var naturalHeight = _ref.naturalHeight, 400 | naturalWidth = _ref.naturalWidth; 401 | var fitOptions = { 402 | imgHeight: naturalHeight, 403 | imgWidth: naturalWidth, 404 | containerHeight: this.elems.container.clientHeight, 405 | containerWidth: this.elems.container.clientWidth, 406 | canvasWidth: this.elems.imageCanvas.clientWidth, 407 | canvasHeight: this.elems.imageCanvas.clientHeight, 408 | imageSize: this.settings.imageSize 409 | }; 410 | 411 | var _fit = fit(fitOptions), 412 | width = _fit.width, 413 | height = _fit.height; 414 | 415 | return transitionAsPromise(function () { 416 | Object.assign(_this3.elems.imageWrapper.style, { 417 | width: width + 'px', 418 | height: height + 'px' 419 | }); 420 | }, this.elems.imageWrapper); 421 | } 422 | }, { 423 | key: "appear", 424 | value: function appear(image) { 425 | var _this4 = this; 426 | 427 | this.elems.imageWrapper.removeChild(this.elems.img); 428 | this.elems.img = image; 429 | this.elems.img.setAttribute('class', 'chocolat-img'); 430 | this.elems.imageWrapper.appendChild(this.elems.img); 431 | var fadeInPromise = transitionAsPromise(function () { 432 | _this4.elems.imageCanvas.classList.add('chocolat-visible'); 433 | }, this.elems.imageCanvas); 434 | return fadeInPromise; 435 | } 436 | }, { 437 | key: "change", 438 | value: function change(step) { 439 | if (!this.state.visible) { 440 | return; 441 | } 442 | 443 | if (!this.settings.linkImages) { 444 | return; 445 | } 446 | 447 | this.zoomOut(); 448 | var requestedImage = this.settings.currentImageIndex + parseInt(step); 449 | 450 | if (requestedImage > this.settings.lastImageIndex) { 451 | if (this.settings.loop) { 452 | return this.load(this.settings.firstImageIndex); 453 | } 454 | } else if (requestedImage < this.settings.firstImageIndex) { 455 | if (this.settings.loop) { 456 | return this.load(this.settings.lastImageIndex); 457 | } 458 | } else { 459 | return this.load(requestedImage); 460 | } 461 | } 462 | }, { 463 | key: "arrows", 464 | value: function arrows() { 465 | if (this.settings.loop) { 466 | this.elems.left.classList.add('active'); 467 | this.elems.right.classList.add('active'); 468 | } else if (this.settings.linkImages) { 469 | this.elems.right.classList.toggle('active', this.settings.currentImageIndex !== this.settings.lastImageIndex); 470 | this.elems.left.classList.toggle('active', this.settings.currentImageIndex !== this.settings.firstImageIndex); 471 | } else { 472 | this.elems.left.classList.remove('active'); 473 | this.elems.right.classList.remove('active'); 474 | } 475 | } 476 | }, { 477 | key: "close", 478 | value: function close() { 479 | var _this5 = this; 480 | 481 | if (this.state.fullScreenOpen) { 482 | exitFullScreen(); 483 | return; 484 | } 485 | 486 | this.state.visible = false; 487 | var promiseOverlay = transitionAsPromise(function () { 488 | _this5.elems.overlay.classList.remove('chocolat-visible'); 489 | }, this.elems.overlay); 490 | var promiseWrapper = transitionAsPromise(function () { 491 | _this5.elems.wrapper.classList.remove('chocolat-visible'); 492 | }, this.elems.wrapper); 493 | return Promise.all([promiseOverlay, promiseWrapper]).then(function () { 494 | _this5.elems.container.classList.remove('chocolat-open'); 495 | 496 | _this5.settings.afterClose.call(_this5); 497 | }); 498 | } 499 | }, { 500 | key: "destroy", 501 | value: function destroy() { 502 | var _this$elems$container; 503 | 504 | for (var i = this.events.length - 1; i >= 0; i--) { 505 | var _this$events$i = this.events[i], 506 | element = _this$events$i.element, 507 | eventName = _this$events$i.eventName; 508 | this.off(element, eventName); 509 | } 510 | 511 | if (!this.state.initialized) { 512 | return; 513 | } 514 | 515 | if (this.state.fullScreenOpen) { 516 | exitFullScreen(); 517 | } 518 | 519 | this.settings.currentImageIndex = undefined; 520 | this.state.visible = false; 521 | this.state.initialized = false; 522 | 523 | (_this$elems$container = this.elems.container.classList).remove.apply(_this$elems$container, _toConsumableArray(this._cssClasses)); 524 | 525 | this.elems.wrapper.parentNode.removeChild(this.elems.wrapper); 526 | } 527 | }, { 528 | key: "markup", 529 | value: function markup() { 530 | this.elems.container.classList.add('chocolat-open', this.settings.className); 531 | 532 | if (this.settings.imageSize == 'cover') { 533 | this.elems.container.classList.add('chocolat-cover'); 534 | } 535 | 536 | if (this.elems.container !== document.body) { 537 | this.elems.container.classList.add('chocolat-in-container'); 538 | } 539 | 540 | this.elems.wrapper = document.createElement('div'); 541 | this.elems.wrapper.setAttribute('id', 'chocolat-content-' + this.settings.setIndex); 542 | this.elems.wrapper.setAttribute('class', 'chocolat-wrapper'); 543 | this.elems.container.appendChild(this.elems.wrapper); 544 | this.elems.overlay = document.createElement('div'); 545 | this.elems.overlay.setAttribute('class', 'chocolat-overlay'); 546 | this.elems.wrapper.appendChild(this.elems.overlay); 547 | this.elems.loader = document.createElement('div'); 548 | this.elems.loader.setAttribute('class', 'chocolat-loader'); 549 | this.elems.wrapper.appendChild(this.elems.loader); 550 | this.elems.layout = document.createElement('div'); 551 | this.elems.layout.setAttribute('class', 'chocolat-layout'); 552 | this.elems.wrapper.appendChild(this.elems.layout); 553 | this.elems.top = document.createElement('div'); 554 | this.elems.top.setAttribute('class', 'chocolat-top'); 555 | this.elems.layout.appendChild(this.elems.top); 556 | this.elems.center = document.createElement('div'); 557 | this.elems.center.setAttribute('class', 'chocolat-center'); 558 | this.elems.layout.appendChild(this.elems.center); 559 | this.elems.left = document.createElement('div'); 560 | this.elems.left.setAttribute('class', 'chocolat-left'); 561 | this.elems.center.appendChild(this.elems.left); 562 | this.elems.imageCanvas = document.createElement('div'); 563 | this.elems.imageCanvas.setAttribute('class', 'chocolat-image-canvas'); 564 | this.elems.center.appendChild(this.elems.imageCanvas); 565 | this.elems.imageWrapper = document.createElement('div'); 566 | this.elems.imageWrapper.setAttribute('class', 'chocolat-image-wrapper'); 567 | this.elems.imageCanvas.appendChild(this.elems.imageWrapper); 568 | this.elems.img = document.createElement('img'); 569 | this.elems.img.setAttribute('class', 'chocolat-img'); 570 | this.elems.imageWrapper.appendChild(this.elems.img); 571 | this.elems.right = document.createElement('div'); 572 | this.elems.right.setAttribute('class', 'chocolat-right'); 573 | this.elems.center.appendChild(this.elems.right); 574 | this.elems.bottom = document.createElement('div'); 575 | this.elems.bottom.setAttribute('class', 'chocolat-bottom'); 576 | this.elems.layout.appendChild(this.elems.bottom); 577 | this.elems.close = document.createElement('span'); 578 | this.elems.close.setAttribute('class', 'chocolat-close'); 579 | this.elems.top.appendChild(this.elems.close); 580 | this.elems.description = document.createElement('span'); 581 | this.elems.description.setAttribute('class', 'chocolat-description'); 582 | this.elems.bottom.appendChild(this.elems.description); 583 | this.elems.pagination = document.createElement('span'); 584 | this.elems.pagination.setAttribute('class', 'chocolat-pagination'); 585 | this.elems.bottom.appendChild(this.elems.pagination); 586 | this.elems.setTitle = document.createElement('span'); 587 | this.elems.setTitle.setAttribute('class', 'chocolat-set-title'); 588 | this.elems.setTitle.textContent = this.settings.setTitle(); 589 | this.elems.bottom.appendChild(this.elems.setTitle); 590 | this.elems.fullscreen = document.createElement('span'); 591 | this.elems.fullscreen.setAttribute('class', 'chocolat-fullscreen'); 592 | this.elems.bottom.appendChild(this.elems.fullscreen); 593 | this.settings.afterMarkup.call(this); 594 | } 595 | }, { 596 | key: "attachListeners", 597 | value: function attachListeners() { 598 | var _this6 = this; 599 | 600 | this.off(document, 'keydown.chocolat'); 601 | this.on(document, 'keydown.chocolat', function (e) { 602 | if (_this6.state.initialized) { 603 | if (e.keyCode == 37) { 604 | _this6.change(-1); 605 | } else if (e.keyCode == 39) { 606 | _this6.change(1); 607 | } else if (e.keyCode == 27) { 608 | _this6.close(); 609 | } 610 | } 611 | }); 612 | var right = this.elems.wrapper.querySelector('.chocolat-right'); 613 | this.off(right, 'click.chocolat'); 614 | this.on(right, 'click.chocolat', function () { 615 | _this6.change(+1); 616 | }); 617 | var left = this.elems.wrapper.querySelector('.chocolat-left'); 618 | this.off(left, 'click.chocolat'); 619 | this.on(left, 'click.chocolat', function () { 620 | _this6.change(-1); 621 | }); 622 | this.off(this.elems.close, 'click.chocolat'); 623 | this.on(this.elems.close, 'click.chocolat', this.close.bind(this)); 624 | this.off(this.elems.fullscreen, 'click.chocolat'); 625 | this.on(this.elems.fullscreen, 'click.chocolat', function () { 626 | if (_this6.state.fullScreenOpen) { 627 | exitFullScreen(); 628 | return; 629 | } 630 | 631 | openFullScreen(_this6.elems.wrapper); 632 | }); 633 | this.off(document, 'fullscreenchange.chocolat'); 634 | this.on(document, 'fullscreenchange.chocolat', function () { 635 | if (document.fullscreenElement || document.webkitCurrentFullScreenElement || document.webkitFullscreenElement) { 636 | _this6.state.fullScreenOpen = true; 637 | } else { 638 | _this6.state.fullScreenOpen = false; 639 | } 640 | }); 641 | this.off(document, 'webkitfullscreenchange.chocolat'); 642 | this.on(document, 'webkitfullscreenchange.chocolat', function () { 643 | if (document.fullscreenElement || document.webkitCurrentFullScreenElement || document.webkitFullscreenElement) { 644 | _this6.state.fullScreenOpen = true; 645 | } else { 646 | _this6.state.fullScreenOpen = false; 647 | } 648 | }); 649 | 650 | if (this.settings.closeOnBackgroundClick) { 651 | this.off(this.elems.overlay, 'click.chocolat'); 652 | this.on(this.elems.overlay, 'click.chocolat', this.close.bind(this)); 653 | } 654 | 655 | this.off(this.elems.wrapper, 'click.chocolat'); 656 | this.on(this.elems.wrapper, 'click.chocolat', function () { 657 | if (_this6.state.initialZoomState === null || !_this6.state.visible) { 658 | return; 659 | } 660 | 661 | _this6.elems.container.classList.add('chocolat-zooming-out'); 662 | 663 | _this6.zoomOut().then(function () { 664 | _this6.elems.container.classList.remove('chocolat-zoomed'); 665 | 666 | _this6.elems.container.classList.remove('chocolat-zooming-out'); 667 | }); 668 | }); 669 | this.off(this.elems.imageWrapper, 'click.chocolat'); 670 | this.on(this.elems.imageWrapper, 'click.chocolat', function (e) { 671 | if (_this6.state.initialZoomState === null && _this6.elems.container.classList.contains('chocolat-zoomable')) { 672 | e.stopPropagation(); 673 | 674 | _this6.elems.container.classList.add('chocolat-zooming-in'); 675 | 676 | _this6.zoomIn(e).then(function () { 677 | _this6.elems.container.classList.add('chocolat-zoomed'); 678 | 679 | _this6.elems.container.classList.remove('chocolat-zooming-in'); 680 | }); 681 | } 682 | }); 683 | this.on(this.elems.wrapper, 'mousemove.chocolat', function (e) { 684 | if (_this6.state.initialZoomState === null || !_this6.state.visible) { 685 | return; 686 | } 687 | 688 | var rect = _this6.elems.wrapper.getBoundingClientRect(); 689 | 690 | var pos = { 691 | top: rect.top + window.scrollY, 692 | left: rect.left + window.scrollX 693 | }; 694 | var height = _this6.elems.wrapper.clientHeight; 695 | var width = _this6.elems.wrapper.clientWidth; 696 | var imgWidth = _this6.elems.img.width; 697 | var imgHeight = _this6.elems.img.height; 698 | var coord = [e.pageX - width / 2 - pos.left, e.pageY - height / 2 - pos.top]; 699 | var mvtX = 0; 700 | 701 | if (imgWidth > width) { 702 | var paddingX = _this6.settings.zoomedPaddingX(imgWidth, width); 703 | 704 | mvtX = coord[0] / (width / 2); 705 | mvtX = ((imgWidth - width) / 2 + paddingX) * mvtX; 706 | } 707 | 708 | var mvtY = 0; 709 | 710 | if (imgHeight > height) { 711 | var paddingY = _this6.settings.zoomedPaddingY(imgHeight, height); 712 | 713 | mvtY = coord[1] / (height / 2); 714 | mvtY = ((imgHeight - height) / 2 + paddingY) * mvtY; 715 | } 716 | 717 | _this6.elems.img.style.marginLeft = -mvtX + 'px'; 718 | _this6.elems.img.style.marginTop = -mvtY + 'px'; 719 | }); 720 | this.on(window, 'resize.chocolat', function (e) { 721 | if (!_this6.state.initialized || !_this6.state.visible) { 722 | return; 723 | } 724 | 725 | debounce(50, function () { 726 | var fitOptions = { 727 | imgHeight: _this6.elems.img.naturalHeight, 728 | imgWidth: _this6.elems.img.naturalWidth, 729 | containerHeight: _this6.elems.wrapper.clientHeight, 730 | containerWidth: _this6.elems.wrapper.clientWidth, 731 | canvasWidth: _this6.elems.imageCanvas.clientWidth, 732 | canvasHeight: _this6.elems.imageCanvas.clientHeight, 733 | imageSize: _this6.settings.imageSize 734 | }; 735 | 736 | var _fit2 = fit(fitOptions); 737 | 738 | _this6.position(_this6.elems.img).then(function () { 739 | _this6.elems.container.classList.toggle('chocolat-zoomable', _this6.zoomable(_this6.elems.img, _this6.elems.wrapper)); 740 | }); 741 | }); 742 | }); 743 | } 744 | }, { 745 | key: "zoomable", 746 | value: function zoomable(image, wrapper) { 747 | var wrapperWidth = wrapper.clientWidth; 748 | var wrapperHeight = wrapper.clientHeight; 749 | var isImageZoomable = this.settings.allowZoom && (image.naturalWidth > wrapperWidth || image.naturalHeight > wrapperHeight) ? true : false; 750 | var isImageStretched = image.clientWidth > image.naturalWidth || image.clientHeight > image.naturalHeight; 751 | return isImageZoomable && !isImageStretched; 752 | } 753 | }, { 754 | key: "zoomIn", 755 | value: function zoomIn(e) { 756 | this.state.initialZoomState = this.settings.imageSize; 757 | this.settings.imageSize = 'native'; 758 | return this.position(this.elems.img); 759 | } 760 | }, { 761 | key: "zoomOut", 762 | value: function zoomOut(e) { 763 | this.settings.imageSize = this.state.initialZoomState || this.settings.imageSize; 764 | this.state.initialZoomState = null; 765 | this.elems.img.style.margin = 0; 766 | return this.position(this.elems.img); 767 | } 768 | }, { 769 | key: "on", 770 | value: function on(element, eventName, cb) { 771 | // const eventName = this.settings.setIndex + '-' + eventName 772 | var length = this.events.push({ 773 | element: element, 774 | eventName: eventName, 775 | cb: cb 776 | }); 777 | element.addEventListener(eventName.split('.')[0], this.events[length - 1].cb); 778 | } 779 | }, { 780 | key: "off", 781 | value: function off(element, eventName) { 782 | // const eventName = this.settings.setIndex + '-' + eventName 783 | var index = this.events.findIndex(function (event) { 784 | return event.element === element && event.eventName === eventName; 785 | }); 786 | 787 | if (this.events[index]) { 788 | element.removeEventListener(eventName.split('.')[0], this.events[index].cb); 789 | this.events.splice(index, 1); 790 | } 791 | } 792 | }]); 793 | 794 | return Chocolat; 795 | }(); 796 | 797 | var instances = []; 798 | function main_esm (elements, options) { 799 | var settings = Object.assign({}, defaults, { 800 | images: [] 801 | }, options, { 802 | setIndex: instances.length 803 | }); 804 | var instance = new Chocolat(elements, settings); 805 | instances.push(instance); 806 | return instance; 807 | } 808 | 809 | module.exports = main_esm; 810 | -------------------------------------------------------------------------------- /dist/js/chocolat.esm.js: -------------------------------------------------------------------------------- 1 | import 'core-js/modules/web.dom-collections.iterator.js'; 2 | 3 | function _classCallCheck(instance, Constructor) { 4 | if (!(instance instanceof Constructor)) { 5 | throw new TypeError("Cannot call a class as a function"); 6 | } 7 | } 8 | 9 | function _defineProperties(target, props) { 10 | for (var i = 0; i < props.length; i++) { 11 | var descriptor = props[i]; 12 | descriptor.enumerable = descriptor.enumerable || false; 13 | descriptor.configurable = true; 14 | if ("value" in descriptor) descriptor.writable = true; 15 | Object.defineProperty(target, descriptor.key, descriptor); 16 | } 17 | } 18 | 19 | function _createClass(Constructor, protoProps, staticProps) { 20 | if (protoProps) _defineProperties(Constructor.prototype, protoProps); 21 | if (staticProps) _defineProperties(Constructor, staticProps); 22 | return Constructor; 23 | } 24 | 25 | function _toConsumableArray(arr) { 26 | return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); 27 | } 28 | 29 | function _arrayWithoutHoles(arr) { 30 | if (Array.isArray(arr)) return _arrayLikeToArray(arr); 31 | } 32 | 33 | function _iterableToArray(iter) { 34 | if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); 35 | } 36 | 37 | function _unsupportedIterableToArray(o, minLen) { 38 | if (!o) return; 39 | if (typeof o === "string") return _arrayLikeToArray(o, minLen); 40 | var n = Object.prototype.toString.call(o).slice(8, -1); 41 | if (n === "Object" && o.constructor) n = o.constructor.name; 42 | if (n === "Map" || n === "Set") return Array.from(o); 43 | if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); 44 | } 45 | 46 | function _arrayLikeToArray(arr, len) { 47 | if (len == null || len > arr.length) len = arr.length; 48 | 49 | for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; 50 | 51 | return arr2; 52 | } 53 | 54 | function _nonIterableSpread() { 55 | throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); 56 | } 57 | 58 | var timerDebounce = undefined; 59 | function debounce(duration, callback) { 60 | clearTimeout(timerDebounce); 61 | timerDebounce = setTimeout(function () { 62 | callback(); 63 | }, duration); 64 | return timerDebounce; 65 | } 66 | function transitionAsPromise(triggeringFunc, el) { 67 | return new Promise(function (resolve) { 68 | var handleTransitionEnd = function handleTransitionEnd() { 69 | el.removeEventListener('transitionend', handleTransitionEnd); 70 | resolve(); 71 | }; 72 | 73 | el.addEventListener('transitionend', handleTransitionEnd); 74 | var classesBefore = el.getAttribute('class'); 75 | var stylesBefore = el.getAttribute('style'); 76 | triggeringFunc(); 77 | 78 | if (classesBefore === el.getAttribute('class') && stylesBefore === el.getAttribute('style')) { 79 | handleTransitionEnd(); 80 | } 81 | 82 | if (parseFloat(getComputedStyle(el)['transitionDuration']) === 0) { 83 | handleTransitionEnd(); 84 | } 85 | }); 86 | } 87 | function loadImage(_ref) { 88 | var src = _ref.src, 89 | srcset = _ref.srcset, 90 | sizes = _ref.sizes; 91 | var image = new Image(); 92 | image.src = src; 93 | 94 | if (srcset) { 95 | image.srcset = srcset; 96 | } 97 | 98 | if (sizes) { 99 | image.sizes = sizes; 100 | } 101 | 102 | if ('decode' in image) { 103 | return new Promise(function (resolve, reject) { 104 | image.decode().then(function () { 105 | resolve(image); 106 | })["catch"](function () { 107 | reject(image); 108 | }); 109 | }); 110 | } else { 111 | return new Promise(function (resolve, reject) { 112 | image.onload = resolve(image); 113 | image.onerror = reject(image); 114 | }); 115 | } 116 | } 117 | function fit(options) { 118 | var height; 119 | var width; 120 | var imgHeight = options.imgHeight, 121 | imgWidth = options.imgWidth, 122 | containerHeight = options.containerHeight, 123 | containerWidth = options.containerWidth, 124 | canvasWidth = options.canvasWidth, 125 | canvasHeight = options.canvasHeight, 126 | imageSize = options.imageSize; 127 | var canvasRatio = canvasHeight / canvasWidth; 128 | var containerRatio = containerHeight / containerWidth; 129 | var imgRatio = imgHeight / imgWidth; 130 | 131 | if (imageSize == 'cover') { 132 | if (imgRatio < containerRatio) { 133 | height = containerHeight; 134 | width = height / imgRatio; 135 | } else { 136 | width = containerWidth; 137 | height = width * imgRatio; 138 | } 139 | } else if (imageSize == 'native') { 140 | height = imgHeight; 141 | width = imgWidth; 142 | } else { 143 | if (imgRatio > canvasRatio) { 144 | height = canvasHeight; 145 | width = height / imgRatio; 146 | } else { 147 | width = canvasWidth; 148 | height = width * imgRatio; 149 | } 150 | 151 | if (imageSize === 'scale-down' && (width >= imgWidth || height >= imgHeight)) { 152 | width = imgWidth; 153 | height = imgHeight; 154 | } 155 | } 156 | 157 | return { 158 | height: height, 159 | width: width 160 | }; 161 | } 162 | function openFullScreen(wrapper) { 163 | if (wrapper.requestFullscreen) { 164 | return wrapper.requestFullscreen(); 165 | } else if (wrapper.webkitRequestFullscreen) { 166 | return wrapper.webkitRequestFullscreen(); 167 | } else if (wrapper.msRequestFullscreen) { 168 | return wrapper.msRequestFullscreen(); 169 | } else { 170 | return Promise.reject(); 171 | } 172 | } 173 | function exitFullScreen() { 174 | if (document.exitFullscreen) { 175 | return document.exitFullscreen(); 176 | } else if (document.webkitExitFullscreen) { 177 | return document.webkitExitFullscreen(); 178 | } else if (document.msExitFullscreen) { 179 | return document.msExitFullscreen(); 180 | } else { 181 | return Promise.reject(); 182 | } 183 | } 184 | 185 | var defaults = { 186 | container: document.body, 187 | // window or element 188 | className: undefined, 189 | imageSize: 'scale-down', 190 | // 'scale-down', 'contain', 'cover' or 'native' 191 | fullScreen: false, 192 | loop: false, 193 | linkImages: true, 194 | setIndex: 0, 195 | firstImageIndex: 0, 196 | lastImageIndex: false, 197 | currentImageIndex: undefined, 198 | allowZoom: true, 199 | closeOnBackgroundClick: true, 200 | imageSourceAttribute: 'href', 201 | setTitle: function setTitle() { 202 | return ''; 203 | }, 204 | description: function description() { 205 | return this.images[this.settings.currentImageIndex].title; 206 | }, 207 | pagination: function pagination() { 208 | var last = this.settings.lastImageIndex + 1; 209 | var position = this.settings.currentImageIndex + 1; 210 | return position + '/' + last; 211 | }, 212 | afterInitialize: function afterInitialize() {}, 213 | afterMarkup: function afterMarkup() {}, 214 | beforeImageLoad: function beforeImageLoad() {}, 215 | afterImageLoad: function afterImageLoad() {}, 216 | afterClose: function afterClose() {}, 217 | zoomedPaddingX: function zoomedPaddingX(canvasWidth, imgWidth) { 218 | return 0; 219 | }, 220 | zoomedPaddingY: function zoomedPaddingY(canvasHeight, imgHeight) { 221 | return 0; 222 | } 223 | }; 224 | var Chocolat = /*#__PURE__*/function () { 225 | function Chocolat(elements, settings) { 226 | var _this = this; 227 | 228 | _classCallCheck(this, Chocolat); 229 | 230 | this.settings = settings; 231 | this.elems = {}; 232 | this.images = []; 233 | this.events = []; 234 | this.state = { 235 | fullScreenOpen: false, 236 | initialZoomState: null, 237 | initialized: false, 238 | timer: false, 239 | visible: false 240 | }; 241 | this._cssClasses = ['chocolat-open', 'chocolat-in-container', 'chocolat-cover', 'chocolat-zoomable', 'chocolat-zoomed', 'chocolat-zooming-in', 'chocolat-zooming-out']; 242 | 243 | if (NodeList.prototype.isPrototypeOf(elements) || HTMLCollection.prototype.isPrototypeOf(elements)) { 244 | elements.forEach(function (el, i) { 245 | _this.images.push({ 246 | title: el.getAttribute('title'), 247 | src: el.getAttribute(settings.imageSourceAttribute), 248 | srcset: el.getAttribute('data-srcset'), 249 | sizes: el.getAttribute('data-sizes') 250 | }); 251 | 252 | _this.off(el, 'click.chocolat'); 253 | 254 | _this.on(el, 'click.chocolat', function (e) { 255 | _this.init(i); 256 | 257 | e.preventDefault(); 258 | }); 259 | }); 260 | } else { 261 | this.images = elements; 262 | } 263 | 264 | if (this.settings.container instanceof Element || this.settings.container instanceof HTMLElement) { 265 | this.elems.container = this.settings.container; 266 | } else { 267 | this.elems.container = document.body; 268 | } 269 | 270 | this.api = { 271 | open: function open(i) { 272 | i = parseInt(i) || 0; 273 | return _this.init(i); 274 | }, 275 | close: function close() { 276 | return _this.close(); 277 | }, 278 | next: function next() { 279 | return _this.change(1); 280 | }, 281 | prev: function prev() { 282 | return _this.change(-1); 283 | }, 284 | "goto": function goto(i) { 285 | return _this.open(i); 286 | }, 287 | current: function current() { 288 | return _this.settings.currentImageIndex; 289 | }, 290 | position: function position() { 291 | return _this.position(_this.elems.img); 292 | }, 293 | destroy: function destroy() { 294 | return _this.destroy(); 295 | }, 296 | set: function set(property, value) { 297 | _this.settings[property] = value; 298 | return value; 299 | }, 300 | get: function get(property) { 301 | return _this.settings[property]; 302 | }, 303 | getElem: function getElem(name) { 304 | return _this.elems[name]; 305 | } 306 | }; 307 | } 308 | 309 | _createClass(Chocolat, [{ 310 | key: "init", 311 | value: function init(i) { 312 | if (!this.state.initialized) { 313 | this.markup(); 314 | this.attachListeners(); 315 | this.settings.lastImageIndex = this.images.length - 1; 316 | this.state.initialized = true; 317 | } 318 | 319 | this.settings.afterInitialize.call(this); 320 | return this.load(i); 321 | } 322 | }, { 323 | key: "load", 324 | value: function load(index) { 325 | var _this2 = this; 326 | 327 | this.settings.beforeImageLoad.call(this); 328 | 329 | if (!this.state.visible) { 330 | this.state.visible = true; 331 | setTimeout(function () { 332 | _this2.elems.overlay.classList.add('chocolat-visible'); 333 | 334 | _this2.elems.wrapper.classList.add('chocolat-visible'); 335 | }, 0); 336 | this.elems.container.classList.add('chocolat-open'); 337 | } 338 | 339 | if (this.settings.fullScreen) { 340 | openFullScreen(this.elems.wrapper); 341 | } 342 | 343 | if (this.settings.currentImageIndex === index) { 344 | return Promise.resolve(); 345 | } 346 | 347 | var loaderTimer = setTimeout(function () { 348 | _this2.elems.loader.classList.add('chocolat-visible'); 349 | }, 1000); 350 | var fadeOutPromise; 351 | var image; 352 | var fadeOutTimer = setTimeout(function () { 353 | fadeOutTimer = undefined; 354 | fadeOutPromise = transitionAsPromise(function () { 355 | _this2.elems.imageCanvas.classList.remove('chocolat-visible'); 356 | }, _this2.elems.imageCanvas); 357 | }, 80); 358 | return loadImage(this.images[index]).then(function (loadedImage) { 359 | image = loadedImage; 360 | 361 | if (fadeOutTimer) { 362 | clearTimeout(fadeOutTimer); 363 | return Promise.resolve(); 364 | } else { 365 | return fadeOutPromise; 366 | } 367 | }).then(function () { 368 | var nextIndex = index + 1; 369 | 370 | if (_this2.images[nextIndex] != undefined) { 371 | loadImage(_this2.images[nextIndex]); 372 | } 373 | 374 | _this2.settings.currentImageIndex = index; 375 | _this2.elems.description.textContent = _this2.settings.description.call(_this2); 376 | _this2.elems.pagination.textContent = _this2.settings.pagination.call(_this2); 377 | 378 | _this2.arrows(); 379 | 380 | return _this2.position(image).then(function () { 381 | _this2.elems.loader.classList.remove('chocolat-visible'); 382 | 383 | clearTimeout(loaderTimer); 384 | return _this2.appear(image); 385 | }); 386 | }).then(function () { 387 | _this2.elems.container.classList.toggle('chocolat-zoomable', _this2.zoomable(image, _this2.elems.wrapper)); 388 | 389 | _this2.settings.afterImageLoad.call(_this2); 390 | }); 391 | } 392 | }, { 393 | key: "position", 394 | value: function position(_ref) { 395 | var _this3 = this; 396 | 397 | var naturalHeight = _ref.naturalHeight, 398 | naturalWidth = _ref.naturalWidth; 399 | var fitOptions = { 400 | imgHeight: naturalHeight, 401 | imgWidth: naturalWidth, 402 | containerHeight: this.elems.container.clientHeight, 403 | containerWidth: this.elems.container.clientWidth, 404 | canvasWidth: this.elems.imageCanvas.clientWidth, 405 | canvasHeight: this.elems.imageCanvas.clientHeight, 406 | imageSize: this.settings.imageSize 407 | }; 408 | 409 | var _fit = fit(fitOptions), 410 | width = _fit.width, 411 | height = _fit.height; 412 | 413 | return transitionAsPromise(function () { 414 | Object.assign(_this3.elems.imageWrapper.style, { 415 | width: width + 'px', 416 | height: height + 'px' 417 | }); 418 | }, this.elems.imageWrapper); 419 | } 420 | }, { 421 | key: "appear", 422 | value: function appear(image) { 423 | var _this4 = this; 424 | 425 | this.elems.imageWrapper.removeChild(this.elems.img); 426 | this.elems.img = image; 427 | this.elems.img.setAttribute('class', 'chocolat-img'); 428 | this.elems.imageWrapper.appendChild(this.elems.img); 429 | var fadeInPromise = transitionAsPromise(function () { 430 | _this4.elems.imageCanvas.classList.add('chocolat-visible'); 431 | }, this.elems.imageCanvas); 432 | return fadeInPromise; 433 | } 434 | }, { 435 | key: "change", 436 | value: function change(step) { 437 | if (!this.state.visible) { 438 | return; 439 | } 440 | 441 | if (!this.settings.linkImages) { 442 | return; 443 | } 444 | 445 | this.zoomOut(); 446 | var requestedImage = this.settings.currentImageIndex + parseInt(step); 447 | 448 | if (requestedImage > this.settings.lastImageIndex) { 449 | if (this.settings.loop) { 450 | return this.load(this.settings.firstImageIndex); 451 | } 452 | } else if (requestedImage < this.settings.firstImageIndex) { 453 | if (this.settings.loop) { 454 | return this.load(this.settings.lastImageIndex); 455 | } 456 | } else { 457 | return this.load(requestedImage); 458 | } 459 | } 460 | }, { 461 | key: "arrows", 462 | value: function arrows() { 463 | if (this.settings.loop) { 464 | this.elems.left.classList.add('active'); 465 | this.elems.right.classList.add('active'); 466 | } else if (this.settings.linkImages) { 467 | this.elems.right.classList.toggle('active', this.settings.currentImageIndex !== this.settings.lastImageIndex); 468 | this.elems.left.classList.toggle('active', this.settings.currentImageIndex !== this.settings.firstImageIndex); 469 | } else { 470 | this.elems.left.classList.remove('active'); 471 | this.elems.right.classList.remove('active'); 472 | } 473 | } 474 | }, { 475 | key: "close", 476 | value: function close() { 477 | var _this5 = this; 478 | 479 | if (this.state.fullScreenOpen) { 480 | exitFullScreen(); 481 | return; 482 | } 483 | 484 | this.state.visible = false; 485 | var promiseOverlay = transitionAsPromise(function () { 486 | _this5.elems.overlay.classList.remove('chocolat-visible'); 487 | }, this.elems.overlay); 488 | var promiseWrapper = transitionAsPromise(function () { 489 | _this5.elems.wrapper.classList.remove('chocolat-visible'); 490 | }, this.elems.wrapper); 491 | return Promise.all([promiseOverlay, promiseWrapper]).then(function () { 492 | _this5.elems.container.classList.remove('chocolat-open'); 493 | 494 | _this5.settings.afterClose.call(_this5); 495 | }); 496 | } 497 | }, { 498 | key: "destroy", 499 | value: function destroy() { 500 | var _this$elems$container; 501 | 502 | for (var i = this.events.length - 1; i >= 0; i--) { 503 | var _this$events$i = this.events[i], 504 | element = _this$events$i.element, 505 | eventName = _this$events$i.eventName; 506 | this.off(element, eventName); 507 | } 508 | 509 | if (!this.state.initialized) { 510 | return; 511 | } 512 | 513 | if (this.state.fullScreenOpen) { 514 | exitFullScreen(); 515 | } 516 | 517 | this.settings.currentImageIndex = undefined; 518 | this.state.visible = false; 519 | this.state.initialized = false; 520 | 521 | (_this$elems$container = this.elems.container.classList).remove.apply(_this$elems$container, _toConsumableArray(this._cssClasses)); 522 | 523 | this.elems.wrapper.parentNode.removeChild(this.elems.wrapper); 524 | } 525 | }, { 526 | key: "markup", 527 | value: function markup() { 528 | this.elems.container.classList.add('chocolat-open', this.settings.className); 529 | 530 | if (this.settings.imageSize == 'cover') { 531 | this.elems.container.classList.add('chocolat-cover'); 532 | } 533 | 534 | if (this.elems.container !== document.body) { 535 | this.elems.container.classList.add('chocolat-in-container'); 536 | } 537 | 538 | this.elems.wrapper = document.createElement('div'); 539 | this.elems.wrapper.setAttribute('id', 'chocolat-content-' + this.settings.setIndex); 540 | this.elems.wrapper.setAttribute('class', 'chocolat-wrapper'); 541 | this.elems.container.appendChild(this.elems.wrapper); 542 | this.elems.overlay = document.createElement('div'); 543 | this.elems.overlay.setAttribute('class', 'chocolat-overlay'); 544 | this.elems.wrapper.appendChild(this.elems.overlay); 545 | this.elems.loader = document.createElement('div'); 546 | this.elems.loader.setAttribute('class', 'chocolat-loader'); 547 | this.elems.wrapper.appendChild(this.elems.loader); 548 | this.elems.layout = document.createElement('div'); 549 | this.elems.layout.setAttribute('class', 'chocolat-layout'); 550 | this.elems.wrapper.appendChild(this.elems.layout); 551 | this.elems.top = document.createElement('div'); 552 | this.elems.top.setAttribute('class', 'chocolat-top'); 553 | this.elems.layout.appendChild(this.elems.top); 554 | this.elems.center = document.createElement('div'); 555 | this.elems.center.setAttribute('class', 'chocolat-center'); 556 | this.elems.layout.appendChild(this.elems.center); 557 | this.elems.left = document.createElement('div'); 558 | this.elems.left.setAttribute('class', 'chocolat-left'); 559 | this.elems.center.appendChild(this.elems.left); 560 | this.elems.imageCanvas = document.createElement('div'); 561 | this.elems.imageCanvas.setAttribute('class', 'chocolat-image-canvas'); 562 | this.elems.center.appendChild(this.elems.imageCanvas); 563 | this.elems.imageWrapper = document.createElement('div'); 564 | this.elems.imageWrapper.setAttribute('class', 'chocolat-image-wrapper'); 565 | this.elems.imageCanvas.appendChild(this.elems.imageWrapper); 566 | this.elems.img = document.createElement('img'); 567 | this.elems.img.setAttribute('class', 'chocolat-img'); 568 | this.elems.imageWrapper.appendChild(this.elems.img); 569 | this.elems.right = document.createElement('div'); 570 | this.elems.right.setAttribute('class', 'chocolat-right'); 571 | this.elems.center.appendChild(this.elems.right); 572 | this.elems.bottom = document.createElement('div'); 573 | this.elems.bottom.setAttribute('class', 'chocolat-bottom'); 574 | this.elems.layout.appendChild(this.elems.bottom); 575 | this.elems.close = document.createElement('span'); 576 | this.elems.close.setAttribute('class', 'chocolat-close'); 577 | this.elems.top.appendChild(this.elems.close); 578 | this.elems.description = document.createElement('span'); 579 | this.elems.description.setAttribute('class', 'chocolat-description'); 580 | this.elems.bottom.appendChild(this.elems.description); 581 | this.elems.pagination = document.createElement('span'); 582 | this.elems.pagination.setAttribute('class', 'chocolat-pagination'); 583 | this.elems.bottom.appendChild(this.elems.pagination); 584 | this.elems.setTitle = document.createElement('span'); 585 | this.elems.setTitle.setAttribute('class', 'chocolat-set-title'); 586 | this.elems.setTitle.textContent = this.settings.setTitle(); 587 | this.elems.bottom.appendChild(this.elems.setTitle); 588 | this.elems.fullscreen = document.createElement('span'); 589 | this.elems.fullscreen.setAttribute('class', 'chocolat-fullscreen'); 590 | this.elems.bottom.appendChild(this.elems.fullscreen); 591 | this.settings.afterMarkup.call(this); 592 | } 593 | }, { 594 | key: "attachListeners", 595 | value: function attachListeners() { 596 | var _this6 = this; 597 | 598 | this.off(document, 'keydown.chocolat'); 599 | this.on(document, 'keydown.chocolat', function (e) { 600 | if (_this6.state.initialized) { 601 | if (e.keyCode == 37) { 602 | _this6.change(-1); 603 | } else if (e.keyCode == 39) { 604 | _this6.change(1); 605 | } else if (e.keyCode == 27) { 606 | _this6.close(); 607 | } 608 | } 609 | }); 610 | var right = this.elems.wrapper.querySelector('.chocolat-right'); 611 | this.off(right, 'click.chocolat'); 612 | this.on(right, 'click.chocolat', function () { 613 | _this6.change(+1); 614 | }); 615 | var left = this.elems.wrapper.querySelector('.chocolat-left'); 616 | this.off(left, 'click.chocolat'); 617 | this.on(left, 'click.chocolat', function () { 618 | _this6.change(-1); 619 | }); 620 | this.off(this.elems.close, 'click.chocolat'); 621 | this.on(this.elems.close, 'click.chocolat', this.close.bind(this)); 622 | this.off(this.elems.fullscreen, 'click.chocolat'); 623 | this.on(this.elems.fullscreen, 'click.chocolat', function () { 624 | if (_this6.state.fullScreenOpen) { 625 | exitFullScreen(); 626 | return; 627 | } 628 | 629 | openFullScreen(_this6.elems.wrapper); 630 | }); 631 | this.off(document, 'fullscreenchange.chocolat'); 632 | this.on(document, 'fullscreenchange.chocolat', function () { 633 | if (document.fullscreenElement || document.webkitCurrentFullScreenElement || document.webkitFullscreenElement) { 634 | _this6.state.fullScreenOpen = true; 635 | } else { 636 | _this6.state.fullScreenOpen = false; 637 | } 638 | }); 639 | this.off(document, 'webkitfullscreenchange.chocolat'); 640 | this.on(document, 'webkitfullscreenchange.chocolat', function () { 641 | if (document.fullscreenElement || document.webkitCurrentFullScreenElement || document.webkitFullscreenElement) { 642 | _this6.state.fullScreenOpen = true; 643 | } else { 644 | _this6.state.fullScreenOpen = false; 645 | } 646 | }); 647 | 648 | if (this.settings.closeOnBackgroundClick) { 649 | this.off(this.elems.overlay, 'click.chocolat'); 650 | this.on(this.elems.overlay, 'click.chocolat', this.close.bind(this)); 651 | } 652 | 653 | this.off(this.elems.wrapper, 'click.chocolat'); 654 | this.on(this.elems.wrapper, 'click.chocolat', function () { 655 | if (_this6.state.initialZoomState === null || !_this6.state.visible) { 656 | return; 657 | } 658 | 659 | _this6.elems.container.classList.add('chocolat-zooming-out'); 660 | 661 | _this6.zoomOut().then(function () { 662 | _this6.elems.container.classList.remove('chocolat-zoomed'); 663 | 664 | _this6.elems.container.classList.remove('chocolat-zooming-out'); 665 | }); 666 | }); 667 | this.off(this.elems.imageWrapper, 'click.chocolat'); 668 | this.on(this.elems.imageWrapper, 'click.chocolat', function (e) { 669 | if (_this6.state.initialZoomState === null && _this6.elems.container.classList.contains('chocolat-zoomable')) { 670 | e.stopPropagation(); 671 | 672 | _this6.elems.container.classList.add('chocolat-zooming-in'); 673 | 674 | _this6.zoomIn(e).then(function () { 675 | _this6.elems.container.classList.add('chocolat-zoomed'); 676 | 677 | _this6.elems.container.classList.remove('chocolat-zooming-in'); 678 | }); 679 | } 680 | }); 681 | this.on(this.elems.wrapper, 'mousemove.chocolat', function (e) { 682 | if (_this6.state.initialZoomState === null || !_this6.state.visible) { 683 | return; 684 | } 685 | 686 | var rect = _this6.elems.wrapper.getBoundingClientRect(); 687 | 688 | var pos = { 689 | top: rect.top + window.scrollY, 690 | left: rect.left + window.scrollX 691 | }; 692 | var height = _this6.elems.wrapper.clientHeight; 693 | var width = _this6.elems.wrapper.clientWidth; 694 | var imgWidth = _this6.elems.img.width; 695 | var imgHeight = _this6.elems.img.height; 696 | var coord = [e.pageX - width / 2 - pos.left, e.pageY - height / 2 - pos.top]; 697 | var mvtX = 0; 698 | 699 | if (imgWidth > width) { 700 | var paddingX = _this6.settings.zoomedPaddingX(imgWidth, width); 701 | 702 | mvtX = coord[0] / (width / 2); 703 | mvtX = ((imgWidth - width) / 2 + paddingX) * mvtX; 704 | } 705 | 706 | var mvtY = 0; 707 | 708 | if (imgHeight > height) { 709 | var paddingY = _this6.settings.zoomedPaddingY(imgHeight, height); 710 | 711 | mvtY = coord[1] / (height / 2); 712 | mvtY = ((imgHeight - height) / 2 + paddingY) * mvtY; 713 | } 714 | 715 | _this6.elems.img.style.marginLeft = -mvtX + 'px'; 716 | _this6.elems.img.style.marginTop = -mvtY + 'px'; 717 | }); 718 | this.on(window, 'resize.chocolat', function (e) { 719 | if (!_this6.state.initialized || !_this6.state.visible) { 720 | return; 721 | } 722 | 723 | debounce(50, function () { 724 | var fitOptions = { 725 | imgHeight: _this6.elems.img.naturalHeight, 726 | imgWidth: _this6.elems.img.naturalWidth, 727 | containerHeight: _this6.elems.wrapper.clientHeight, 728 | containerWidth: _this6.elems.wrapper.clientWidth, 729 | canvasWidth: _this6.elems.imageCanvas.clientWidth, 730 | canvasHeight: _this6.elems.imageCanvas.clientHeight, 731 | imageSize: _this6.settings.imageSize 732 | }; 733 | 734 | var _fit2 = fit(fitOptions); 735 | 736 | _this6.position(_this6.elems.img).then(function () { 737 | _this6.elems.container.classList.toggle('chocolat-zoomable', _this6.zoomable(_this6.elems.img, _this6.elems.wrapper)); 738 | }); 739 | }); 740 | }); 741 | } 742 | }, { 743 | key: "zoomable", 744 | value: function zoomable(image, wrapper) { 745 | var wrapperWidth = wrapper.clientWidth; 746 | var wrapperHeight = wrapper.clientHeight; 747 | var isImageZoomable = this.settings.allowZoom && (image.naturalWidth > wrapperWidth || image.naturalHeight > wrapperHeight) ? true : false; 748 | var isImageStretched = image.clientWidth > image.naturalWidth || image.clientHeight > image.naturalHeight; 749 | return isImageZoomable && !isImageStretched; 750 | } 751 | }, { 752 | key: "zoomIn", 753 | value: function zoomIn(e) { 754 | this.state.initialZoomState = this.settings.imageSize; 755 | this.settings.imageSize = 'native'; 756 | return this.position(this.elems.img); 757 | } 758 | }, { 759 | key: "zoomOut", 760 | value: function zoomOut(e) { 761 | this.settings.imageSize = this.state.initialZoomState || this.settings.imageSize; 762 | this.state.initialZoomState = null; 763 | this.elems.img.style.margin = 0; 764 | return this.position(this.elems.img); 765 | } 766 | }, { 767 | key: "on", 768 | value: function on(element, eventName, cb) { 769 | // const eventName = this.settings.setIndex + '-' + eventName 770 | var length = this.events.push({ 771 | element: element, 772 | eventName: eventName, 773 | cb: cb 774 | }); 775 | element.addEventListener(eventName.split('.')[0], this.events[length - 1].cb); 776 | } 777 | }, { 778 | key: "off", 779 | value: function off(element, eventName) { 780 | // const eventName = this.settings.setIndex + '-' + eventName 781 | var index = this.events.findIndex(function (event) { 782 | return event.element === element && event.eventName === eventName; 783 | }); 784 | 785 | if (this.events[index]) { 786 | element.removeEventListener(eventName.split('.')[0], this.events[index].cb); 787 | this.events.splice(index, 1); 788 | } 789 | } 790 | }]); 791 | 792 | return Chocolat; 793 | }(); 794 | 795 | var instances = []; 796 | function main_esm (elements, options) { 797 | var settings = Object.assign({}, defaults, { 798 | images: [] 799 | }, options, { 800 | setIndex: instances.length 801 | }); 802 | var instance = new Chocolat(elements, settings); 803 | instances.push(instance); 804 | return instance; 805 | } 806 | 807 | export default main_esm; 808 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chocolat", 3 | "version": "1.1.3", 4 | "description": "Responsive Lightbox Plugin", 5 | "main": "dist/js/chocolat.cjs.js", 6 | "module": "dist/js/chocolat.esm.js", 7 | "directories": { 8 | "test": "test" 9 | }, 10 | "scripts": { 11 | "build": "npm run prettier && rollup -c", 12 | "dev": "rollup -c -w", 13 | "test": "mocha-chrome test/index.html", 14 | "release": "yarn version --patch && git push origin HEAD --follow-tags", 15 | "prettier": "./node_modules/.bin/prettier src/js/*.js test/*.js --write" 16 | }, 17 | "dependencies": { 18 | "core-js": "^3.18.3", 19 | "es6-promise": "^4.2.5" 20 | }, 21 | "devDependencies": { 22 | "@babel/core": "^7.2.2", 23 | "@babel/preset-env": "^7.15.8", 24 | "babel-preset-env": "^1.7.0", 25 | "chai": "^4.2.0", 26 | "jquery": "^3.3.1", 27 | "mocha": "^5.2.0", 28 | "mocha-chrome": "^1.1.0", 29 | "postcss-url": "^8.0.0", 30 | "prettier": "1.15.2", 31 | "prettier-check": "^2.0.0", 32 | "rollup": "^0.68.1", 33 | "rollup-plugin-babel": "^4.1.0", 34 | "rollup-plugin-commonjs": "^9.2.0", 35 | "rollup-plugin-node-resolve": "^4.0.0", 36 | "rollup-plugin-postcss": "^1.2.0", 37 | "sinon": "^7.2.2" 38 | }, 39 | "repository": { 40 | "type": "git", 41 | "url": "git+https://github.com/nicolas-t/Chocolat.git" 42 | }, 43 | "keywords": [ 44 | "lightbox", 45 | "plugin", 46 | "responsive" 47 | ], 48 | "author": "Nicolas Turlais", 49 | "licenses": [ 50 | { 51 | "type": "GPLv3", 52 | "url": "https://github.com/nicolas-t/Chocolat/blob/master/LICENSE" 53 | } 54 | ], 55 | "bugs": { 56 | "url": "https://github.com/nicolas-t/Chocolat/issues" 57 | }, 58 | "homepage": "http://chocolat.insipi.de" 59 | } 60 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## Chocolat [![Build Status](https://travis-ci.org/nicolas-t/Chocolat.svg?branch=master)](https://travis-ci.org/nicolas-t/Chocolat) 2 | ----------- 3 | *Chocolat is a responsive lightbox* 4 | 5 | [Documentation is here](https://chocolat.gitbook.io/chocolat/) 6 | [Demo page is here](http://chocolat.insipi.de/#demo) 7 | 8 | [![Image of Chocolat](http://full.ouplo.com/10/9/jzbY.jpg)](http://chocolat.insipi.de/#demo) 9 | 10 | 11 | ## Documentation 12 | ----------- 13 | You can access the full documentation at this page : 14 | [Documentation](https://chocolat.gitbook.io/chocolat/) 15 | 16 | 17 | ## Upgrading 18 | ----------- 19 | A few breaking changes have been made between v0.4 and v1 : 20 | [Upgrading](https://chocolat.gitbook.io/chocolat/upgrading-from-v0.4) 21 | 22 | 23 | ## Contributing 24 | ----------- 25 | 26 | If you have trouble using Chocolat, or want to report a bug, please open an issue here : 27 | https://github.com/nicolas-t/Chocolat/issues/new 28 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import resolve from 'rollup-plugin-node-resolve' 2 | import commonjs from 'rollup-plugin-commonjs' 3 | import babel from 'rollup-plugin-babel' 4 | import pkg from './package.json' 5 | import url from 'postcss-url'; 6 | import postcss from 'rollup-plugin-postcss'; 7 | 8 | export default [ 9 | // browser-friendly UMD build 10 | { 11 | input: 'src/js/main.iife.js', 12 | dest: pkg.browser, 13 | paths: ['src/css'], 14 | output: { 15 | name: 'chocolat', 16 | format: 'iife', 17 | file: 'dist/js/chocolat.js' 18 | }, 19 | plugins: [ 20 | resolve(), 21 | commonjs(), 22 | babel({ 23 | exclude: ['node_modules/**'] 24 | }), 25 | postcss({ 26 | extract: './dist/css/chocolat.css', 27 | plugins: [ 28 | url({ 29 | url: 'inline', 30 | }), 31 | ], 32 | }), 33 | ] 34 | }, 35 | 36 | // CommonJS (for Node) and ES module (for bundlers) build. 37 | // (We could have three entries in the configuration array 38 | // instead of two, but it's quicker to generate multiple 39 | // builds from a single configuration where possible, using 40 | // the `targets` option which can specify `dest` and `format`) 41 | { 42 | input: 'src/js/main.esm.js', 43 | external: ['es6-promise/auto'], 44 | targets: [ 45 | { dest: pkg.main, format: 'cjs' }, 46 | { dest: pkg.module, format: 'es' } 47 | ], 48 | plugins: [ 49 | babel({ 50 | exclude: ['node_modules/**'] 51 | }) 52 | ] 53 | } 54 | ]; -------------------------------------------------------------------------------- /src/css/chocolat.css: -------------------------------------------------------------------------------- 1 | :root { 2 | --chocolat-icon-left: url('./../icons/icon-left.svg'); 3 | --chocolat-icon-right: url('./../icons/icon-right.svg'); 4 | --chocolat-icon-close: url('./../icons/icon-close.svg'); 5 | --chocolat-icon-loader: url('./../icons/icon-loader.svg'); 6 | --chocolat-icon-fullscreen: url('./../icons/icon-fullscreen.svg'); 7 | --chocolat-transition-time: 0.4s; 8 | } 9 | 10 | .chocolat-zoomable.chocolat-zoomed { 11 | cursor: zoom-out; 12 | } 13 | .chocolat-open { 14 | overflow: hidden; 15 | } 16 | .chocolat-overlay { 17 | transition: opacity var(--chocolat-transition-time) ease, visibility 0s var(--chocolat-transition-time) ease; 18 | height: 100%; 19 | width: 100%; 20 | position: fixed; 21 | left: 0; 22 | top: 0; 23 | z-index: 10; 24 | background-color: #fff; 25 | visibility: hidden; 26 | opacity: 0; 27 | } 28 | .chocolat-overlay.chocolat-visible { 29 | transition: opacity var(--chocolat-transition-time), visibility 0s; 30 | visibility: visible; 31 | opacity: 0.8; 32 | } 33 | 34 | .chocolat-wrapper { 35 | transition: opacity var(--chocolat-transition-time) ease, visibility 0s var(--chocolat-transition-time) ease; 36 | width: 100%; 37 | height: 100%; 38 | position: fixed; 39 | opacity: 0; 40 | left: 0; 41 | top: 0; 42 | z-index: 16; 43 | color: #fff; 44 | visibility: hidden; 45 | } 46 | .chocolat-wrapper.chocolat-visible { 47 | transition: opacity var(--chocolat-transition-time), visibility 0s; 48 | opacity: 1; 49 | visibility: visible; 50 | } 51 | 52 | .chocolat-zoomable .chocolat-img { 53 | cursor: zoom-in; 54 | } 55 | .chocolat-loader { 56 | transition: opacity 0.3s; 57 | height: 38px; 58 | width: 38px; 59 | position: absolute; 60 | left: 50%; 61 | top: 50%; 62 | margin-left: -19px; 63 | margin-top: -19px; 64 | z-index: 11; 65 | background: var(--chocolat-icon-loader); 66 | opacity: 0; 67 | } 68 | .chocolat-loader.chocolat-visible { 69 | opacity: 1; 70 | } 71 | 72 | .chocolat-image-wrapper { 73 | position: fixed; 74 | width: 0px; 75 | height: 0px; 76 | left: 50%; 77 | top: 50%; 78 | z-index: 14; 79 | text-align: left; 80 | transform: translate(-50%, -50%); 81 | } 82 | 83 | .chocolat-image-wrapper .chocolat-img { 84 | position: absolute; 85 | width: 100%; 86 | height: 100%; 87 | } 88 | .chocolat-wrapper .chocolat-left { 89 | width: 50px; 90 | height: 100px; 91 | cursor: pointer; 92 | background: var(--chocolat-icon-left) 50% 50% / 2rem auto no-repeat; 93 | z-index: 17; 94 | visibility: hidden; 95 | } 96 | 97 | .chocolat-layout { 98 | display: flex; 99 | flex-direction: column; 100 | position: absolute; 101 | top: 0; 102 | bottom: 0; 103 | left: 0; 104 | right: 0; 105 | } 106 | .chocolat-image-canvas { 107 | transition: opacity .2s; 108 | opacity: 0; 109 | flex-grow: 1; 110 | align-self: stretch; 111 | } 112 | .chocolat-image-canvas.chocolat-visible { 113 | opacity: 1; 114 | } 115 | .chocolat-center { 116 | flex-grow: 1; 117 | display: flex; 118 | justify-content: center; 119 | align-items: center; 120 | user-select: none; 121 | } 122 | 123 | .chocolat-wrapper .chocolat-right { 124 | width: 50px; 125 | height: 100px; 126 | cursor: pointer; 127 | background: var(--chocolat-icon-right) 50% 50% / 2rem auto no-repeat; 128 | z-index: 17; 129 | visibility: hidden; 130 | } 131 | .chocolat-wrapper .chocolat-right.active { 132 | visibility: visible; 133 | } 134 | .chocolat-wrapper .chocolat-left.active { 135 | visibility: visible; 136 | } 137 | .chocolat-wrapper .chocolat-top { 138 | height: 50px; 139 | overflow: hidden; 140 | z-index: 17; 141 | flex-shrink: 0; 142 | } 143 | .chocolat-wrapper .chocolat-close { 144 | width: 50px; 145 | height: 50px; 146 | cursor: pointer; 147 | position: absolute; 148 | top: 0; 149 | right: 0; 150 | background: var(--chocolat-icon-close) 50% 50% / 2rem auto no-repeat; 151 | } 152 | .chocolat-wrapper .chocolat-bottom { 153 | height: 40px; 154 | font-size: 12px; 155 | z-index: 17; 156 | padding-left: 15px; 157 | padding-right: 15px; 158 | background: rgba(0, 0, 0, 0.2); 159 | flex-shrink: 0; 160 | display: flex; 161 | align-items: center; 162 | 163 | } 164 | .chocolat-wrapper .chocolat-set-title { 165 | display: inline-block; 166 | padding-right: 15px; 167 | line-height: 1; 168 | border-right: 1px solid rgba(255, 255, 255, 0.3); 169 | } 170 | .chocolat-wrapper .chocolat-pagination { 171 | float: right; 172 | display: inline-block; 173 | padding-left: 15px; 174 | padding-right: 15px; 175 | margin-right: 15px; 176 | /*border-right: 1px solid rgba(255, 255, 255, 0.2);*/ 177 | } 178 | .chocolat-wrapper .chocolat-fullscreen { 179 | width: 16px; 180 | height: 40px; 181 | background: var(--chocolat-icon-fullscreen) 50% 50% / 1rem auto no-repeat; 182 | display: block; 183 | cursor: pointer; 184 | float: right; 185 | } 186 | .chocolat-wrapper .chocolat-description { 187 | display: inline-block; 188 | flex-grow: 1; 189 | text-align: left; 190 | } 191 | 192 | /* no container mode*/ 193 | body.chocolat-open > .chocolat-overlay { 194 | z-index: 15; 195 | } 196 | body.chocolat-open > .chocolat-loader { 197 | z-index: 15; 198 | } 199 | body.chocolat-open > .chocolat-image-wrapper { 200 | z-index: 17; 201 | } 202 | 203 | /* container mode*/ 204 | .chocolat-in-container .chocolat-wrapper, 205 | .chocolat-in-container .chocolat-image-wrapper, 206 | .chocolat-in-container .chocolat-overlay { 207 | position: absolute; 208 | } 209 | .chocolat-in-container { 210 | position: relative; 211 | } 212 | 213 | .chocolat-zoomable.chocolat-zooming-in .chocolat-image-wrapper, 214 | .chocolat-zoomable.chocolat-zooming-out .chocolat-image-wrapper { 215 | transition: width .2s ease, height .2s ease; 216 | } 217 | .chocolat-zoomable.chocolat-zooming-in .chocolat-img, 218 | .chocolat-zoomable.chocolat-zooming-out .chocolat-img { 219 | transition: margin .2s ease; 220 | } 221 | 222 | /* uncomment to hide controls when zoomed-in*/ 223 | /* 224 | .chocolat-zoomable .chocolat-top, 225 | .chocolat-zoomable .chocolat-bottom, 226 | .chocolat-zoomable .chocolat-right, 227 | .chocolat-zoomable .chocolat-left { 228 | transition: opacity .3s ease, visibility 0s .3s; 229 | opacity: 1; 230 | } 231 | 232 | .chocolat-zoomable.chocolat-zoomed .chocolat-top, 233 | .chocolat-zoomable.chocolat-zoomed .chocolat-bottom, 234 | .chocolat-zoomable.chocolat-zoomed .chocolat-right, 235 | .chocolat-zoomable.chocolat-zoomed .chocolat-left { 236 | visibility: hidden; 237 | opacity: 0; 238 | } 239 | */ 240 | -------------------------------------------------------------------------------- /src/icons/icon-close.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /src/icons/icon-fullscreen.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/icons/icon-left.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /src/icons/icon-loader.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/icons/icon-right.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /src/js/chocolat.js: -------------------------------------------------------------------------------- 1 | import { 2 | debounce, 3 | transitionAsPromise, 4 | loadImage, 5 | fit, 6 | openFullScreen, 7 | exitFullScreen, 8 | } from './utils.js' 9 | 10 | export const defaults = { 11 | container: document.body, // window or element 12 | className: undefined, 13 | imageSize: 'scale-down', // 'scale-down', 'contain', 'cover' or 'native' 14 | fullScreen: false, 15 | loop: false, 16 | linkImages: true, 17 | setIndex: 0, 18 | firstImageIndex: 0, 19 | lastImageIndex: false, 20 | currentImageIndex: undefined, 21 | allowZoom: true, 22 | closeOnBackgroundClick: true, 23 | imageSourceAttribute: 'href', 24 | setTitle: function() { 25 | return '' 26 | }, 27 | description: function() { 28 | return this.images[this.settings.currentImageIndex].title 29 | }, 30 | pagination: function() { 31 | const last = this.settings.lastImageIndex + 1 32 | const position = this.settings.currentImageIndex + 1 33 | 34 | return position + '/' + last 35 | }, 36 | afterInitialize() {}, 37 | afterMarkup() {}, 38 | beforeImageLoad() {}, 39 | afterImageLoad() {}, 40 | afterClose() {}, 41 | zoomedPaddingX: function(canvasWidth, imgWidth) { 42 | return 0 43 | }, 44 | zoomedPaddingY: function(canvasHeight, imgHeight) { 45 | return 0 46 | }, 47 | } 48 | 49 | export class Chocolat { 50 | constructor(elements, settings) { 51 | this.settings = settings 52 | this.elems = {} 53 | this.images = [] 54 | this.events = [] 55 | this.state = { 56 | fullScreenOpen: false, 57 | initialZoomState: null, 58 | initialized: false, 59 | timer: false, 60 | visible: false, 61 | } 62 | 63 | this._cssClasses = [ 64 | 'chocolat-open', 65 | 'chocolat-in-container', 66 | 'chocolat-cover', 67 | 'chocolat-zoomable', 68 | 'chocolat-zoomed', 69 | 'chocolat-zooming-in', 70 | 'chocolat-zooming-out', 71 | ] 72 | 73 | if ( 74 | NodeList.prototype.isPrototypeOf(elements) || 75 | HTMLCollection.prototype.isPrototypeOf(elements) 76 | ) { 77 | elements.forEach((el, i) => { 78 | this.images.push({ 79 | title: el.getAttribute('title'), 80 | src: el.getAttribute(settings.imageSourceAttribute), 81 | srcset: el.getAttribute('data-srcset'), 82 | sizes: el.getAttribute('data-sizes'), 83 | }) 84 | 85 | this.off(el, 'click.chocolat') 86 | this.on(el, 'click.chocolat', (e) => { 87 | this.init(i) 88 | e.preventDefault() 89 | }) 90 | }) 91 | } else { 92 | this.images = elements 93 | } 94 | 95 | if ( 96 | this.settings.container instanceof Element || 97 | this.settings.container instanceof HTMLElement 98 | ) { 99 | this.elems.container = this.settings.container 100 | } else { 101 | this.elems.container = document.body 102 | } 103 | 104 | this.api = { 105 | open: (i) => { 106 | i = parseInt(i) || 0 107 | return this.init(i) 108 | }, 109 | 110 | close: () => { 111 | return this.close() 112 | }, 113 | 114 | next: () => { 115 | return this.change(1) 116 | }, 117 | 118 | prev: () => { 119 | return this.change(-1) 120 | }, 121 | 122 | goto: (i) => { 123 | return this.open(i) 124 | }, 125 | 126 | current: () => { 127 | return this.settings.currentImageIndex 128 | }, 129 | 130 | position: () => { 131 | return this.position(this.elems.img) 132 | }, 133 | 134 | destroy: () => { 135 | return this.destroy() 136 | }, 137 | 138 | set: (property, value) => { 139 | this.settings[property] = value 140 | return value 141 | }, 142 | 143 | get: (property) => { 144 | return this.settings[property] 145 | }, 146 | 147 | getElem: (name) => { 148 | return this.elems[name] 149 | }, 150 | } 151 | } 152 | 153 | init(i) { 154 | if (!this.state.initialized) { 155 | this.markup() 156 | this.attachListeners() 157 | this.settings.lastImageIndex = this.images.length - 1 158 | this.state.initialized = true 159 | } 160 | 161 | this.settings.afterInitialize.call(this) 162 | 163 | return this.load(i) 164 | } 165 | 166 | load(index) { 167 | this.settings.beforeImageLoad.call(this) 168 | 169 | if (!this.state.visible) { 170 | this.state.visible = true 171 | 172 | setTimeout(() => { 173 | this.elems.overlay.classList.add('chocolat-visible') 174 | this.elems.wrapper.classList.add('chocolat-visible') 175 | }, 0) 176 | 177 | this.elems.container.classList.add('chocolat-open') 178 | } 179 | 180 | if (this.settings.fullScreen) { 181 | openFullScreen(this.elems.wrapper) 182 | } 183 | 184 | if (this.settings.currentImageIndex === index) { 185 | return Promise.resolve() 186 | } 187 | 188 | let loaderTimer = setTimeout(() => { 189 | this.elems.loader.classList.add('chocolat-visible') 190 | }, 1000) 191 | 192 | let fadeOutPromise 193 | let image 194 | 195 | let fadeOutTimer = setTimeout(() => { 196 | fadeOutTimer = undefined 197 | fadeOutPromise = transitionAsPromise(() => { 198 | this.elems.imageCanvas.classList.remove('chocolat-visible') 199 | }, this.elems.imageCanvas) 200 | }, 80) 201 | 202 | return loadImage(this.images[index]) 203 | .then((loadedImage) => { 204 | image = loadedImage 205 | if (fadeOutTimer) { 206 | clearTimeout(fadeOutTimer) 207 | return Promise.resolve() 208 | } else { 209 | return fadeOutPromise 210 | } 211 | }) 212 | .then(() => { 213 | const nextIndex = index + 1 214 | if (this.images[nextIndex] != undefined) { 215 | loadImage(this.images[nextIndex]) 216 | } 217 | 218 | this.settings.currentImageIndex = index 219 | this.elems.description.textContent = this.settings.description.call(this) 220 | this.elems.pagination.textContent = this.settings.pagination.call(this) 221 | this.arrows() 222 | 223 | return this.position(image).then(() => { 224 | this.elems.loader.classList.remove('chocolat-visible') 225 | clearTimeout(loaderTimer) 226 | 227 | return this.appear(image) 228 | }) 229 | }) 230 | .then(() => { 231 | this.elems.container.classList.toggle( 232 | 'chocolat-zoomable', 233 | this.zoomable(image, this.elems.wrapper) 234 | ) 235 | this.settings.afterImageLoad.call(this) 236 | }) 237 | } 238 | 239 | position({ naturalHeight, naturalWidth }) { 240 | const fitOptions = { 241 | imgHeight: naturalHeight, 242 | imgWidth: naturalWidth, 243 | containerHeight: this.elems.container.clientHeight, 244 | containerWidth: this.elems.container.clientWidth, 245 | canvasWidth: this.elems.imageCanvas.clientWidth, 246 | canvasHeight: this.elems.imageCanvas.clientHeight, 247 | imageSize: this.settings.imageSize, 248 | } 249 | 250 | const { width, height } = fit(fitOptions) 251 | 252 | return transitionAsPromise(() => { 253 | Object.assign(this.elems.imageWrapper.style, { 254 | width: width + 'px', 255 | height: height + 'px', 256 | }) 257 | }, this.elems.imageWrapper) 258 | } 259 | 260 | appear(image) { 261 | this.elems.imageWrapper.removeChild(this.elems.img) 262 | this.elems.img = image 263 | this.elems.img.setAttribute('class', 'chocolat-img') 264 | this.elems.imageWrapper.appendChild(this.elems.img) 265 | 266 | const fadeInPromise = transitionAsPromise(() => { 267 | this.elems.imageCanvas.classList.add('chocolat-visible') 268 | }, this.elems.imageCanvas) 269 | 270 | return fadeInPromise 271 | } 272 | 273 | change(step) { 274 | if (!this.state.visible) { 275 | return 276 | } 277 | 278 | if (!this.settings.linkImages) { 279 | return 280 | } 281 | 282 | this.zoomOut() 283 | 284 | const requestedImage = this.settings.currentImageIndex + parseInt(step) 285 | if (requestedImage > this.settings.lastImageIndex) { 286 | if (this.settings.loop) { 287 | return this.load(this.settings.firstImageIndex) 288 | } 289 | } else if (requestedImage < this.settings.firstImageIndex) { 290 | if (this.settings.loop) { 291 | return this.load(this.settings.lastImageIndex) 292 | } 293 | } else { 294 | return this.load(requestedImage) 295 | } 296 | } 297 | 298 | arrows() { 299 | if (this.settings.loop) { 300 | this.elems.left.classList.add('active') 301 | this.elems.right.classList.add('active') 302 | } else if (this.settings.linkImages) { 303 | this.elems.right.classList.toggle( 304 | 'active', 305 | this.settings.currentImageIndex !== this.settings.lastImageIndex 306 | ) 307 | this.elems.left.classList.toggle( 308 | 'active', 309 | this.settings.currentImageIndex !== this.settings.firstImageIndex 310 | ) 311 | } else { 312 | this.elems.left.classList.remove('active') 313 | this.elems.right.classList.remove('active') 314 | } 315 | } 316 | 317 | close() { 318 | if (this.state.fullScreenOpen) { 319 | exitFullScreen() 320 | return 321 | } 322 | 323 | this.state.visible = false 324 | 325 | const promiseOverlay = transitionAsPromise(() => { 326 | this.elems.overlay.classList.remove('chocolat-visible') 327 | }, this.elems.overlay) 328 | 329 | const promiseWrapper = transitionAsPromise(() => { 330 | this.elems.wrapper.classList.remove('chocolat-visible') 331 | }, this.elems.wrapper) 332 | 333 | return Promise.all([promiseOverlay, promiseWrapper]).then(() => { 334 | this.elems.container.classList.remove('chocolat-open') 335 | this.settings.afterClose.call(this) 336 | }) 337 | } 338 | 339 | destroy() { 340 | for (let i = this.events.length - 1; i >= 0; i--) { 341 | const { element, eventName } = this.events[i] 342 | this.off(element, eventName) 343 | } 344 | 345 | if (!this.state.initialized) { 346 | return 347 | } 348 | if (this.state.fullScreenOpen) { 349 | exitFullScreen() 350 | } 351 | this.settings.currentImageIndex = undefined 352 | this.state.visible = false 353 | this.state.initialized = false 354 | 355 | this.elems.container.classList.remove(...this._cssClasses) 356 | this.elems.wrapper.parentNode.removeChild(this.elems.wrapper) 357 | } 358 | 359 | markup() { 360 | this.elems.container.classList.add('chocolat-open', this.settings.className) 361 | if (this.settings.imageSize == 'cover') { 362 | this.elems.container.classList.add('chocolat-cover') 363 | } 364 | if (this.elems.container !== document.body) { 365 | this.elems.container.classList.add('chocolat-in-container') 366 | } 367 | 368 | this.elems.wrapper = document.createElement('div') 369 | this.elems.wrapper.setAttribute('id', 'chocolat-content-' + this.settings.setIndex) 370 | this.elems.wrapper.setAttribute('class', 'chocolat-wrapper') 371 | this.elems.container.appendChild(this.elems.wrapper) 372 | 373 | this.elems.overlay = document.createElement('div') 374 | this.elems.overlay.setAttribute('class', 'chocolat-overlay') 375 | this.elems.wrapper.appendChild(this.elems.overlay) 376 | 377 | this.elems.loader = document.createElement('div') 378 | this.elems.loader.setAttribute('class', 'chocolat-loader') 379 | this.elems.wrapper.appendChild(this.elems.loader) 380 | 381 | this.elems.layout = document.createElement('div') 382 | this.elems.layout.setAttribute('class', 'chocolat-layout') 383 | this.elems.wrapper.appendChild(this.elems.layout) 384 | 385 | this.elems.top = document.createElement('div') 386 | this.elems.top.setAttribute('class', 'chocolat-top') 387 | this.elems.layout.appendChild(this.elems.top) 388 | 389 | this.elems.center = document.createElement('div') 390 | this.elems.center.setAttribute('class', 'chocolat-center') 391 | this.elems.layout.appendChild(this.elems.center) 392 | 393 | this.elems.left = document.createElement('div') 394 | this.elems.left.setAttribute('class', 'chocolat-left') 395 | this.elems.center.appendChild(this.elems.left) 396 | 397 | this.elems.imageCanvas = document.createElement('div') 398 | this.elems.imageCanvas.setAttribute('class', 'chocolat-image-canvas') 399 | this.elems.center.appendChild(this.elems.imageCanvas) 400 | 401 | this.elems.imageWrapper = document.createElement('div') 402 | this.elems.imageWrapper.setAttribute('class', 'chocolat-image-wrapper') 403 | this.elems.imageCanvas.appendChild(this.elems.imageWrapper) 404 | 405 | this.elems.img = document.createElement('img') 406 | this.elems.img.setAttribute('class', 'chocolat-img') 407 | this.elems.imageWrapper.appendChild(this.elems.img) 408 | 409 | this.elems.right = document.createElement('div') 410 | this.elems.right.setAttribute('class', 'chocolat-right') 411 | this.elems.center.appendChild(this.elems.right) 412 | 413 | this.elems.bottom = document.createElement('div') 414 | this.elems.bottom.setAttribute('class', 'chocolat-bottom') 415 | this.elems.layout.appendChild(this.elems.bottom) 416 | 417 | this.elems.close = document.createElement('span') 418 | this.elems.close.setAttribute('class', 'chocolat-close') 419 | this.elems.top.appendChild(this.elems.close) 420 | 421 | this.elems.description = document.createElement('span') 422 | this.elems.description.setAttribute('class', 'chocolat-description') 423 | this.elems.bottom.appendChild(this.elems.description) 424 | 425 | this.elems.pagination = document.createElement('span') 426 | this.elems.pagination.setAttribute('class', 'chocolat-pagination') 427 | this.elems.bottom.appendChild(this.elems.pagination) 428 | 429 | this.elems.setTitle = document.createElement('span') 430 | this.elems.setTitle.setAttribute('class', 'chocolat-set-title') 431 | this.elems.setTitle.textContent = this.settings.setTitle() 432 | this.elems.bottom.appendChild(this.elems.setTitle) 433 | 434 | this.elems.fullscreen = document.createElement('span') 435 | this.elems.fullscreen.setAttribute('class', 'chocolat-fullscreen') 436 | this.elems.bottom.appendChild(this.elems.fullscreen) 437 | 438 | this.settings.afterMarkup.call(this) 439 | } 440 | 441 | attachListeners() { 442 | this.off(document, 'keydown.chocolat') 443 | this.on(document, 'keydown.chocolat', (e) => { 444 | if (this.state.initialized) { 445 | if (e.keyCode == 37) { 446 | this.change(-1) 447 | } else if (e.keyCode == 39) { 448 | this.change(1) 449 | } else if (e.keyCode == 27) { 450 | this.close() 451 | } 452 | } 453 | }) 454 | 455 | const right = this.elems.wrapper.querySelector('.chocolat-right') 456 | this.off(right, 'click.chocolat') 457 | this.on(right, 'click.chocolat', () => { 458 | this.change(+1) 459 | }) 460 | 461 | const left = this.elems.wrapper.querySelector('.chocolat-left') 462 | this.off(left, 'click.chocolat') 463 | this.on(left, 'click.chocolat', () => { 464 | this.change(-1) 465 | }) 466 | 467 | this.off(this.elems.close, 'click.chocolat') 468 | this.on(this.elems.close, 'click.chocolat', this.close.bind(this)) 469 | 470 | this.off(this.elems.fullscreen, 'click.chocolat') 471 | this.on(this.elems.fullscreen, 'click.chocolat', () => { 472 | if (this.state.fullScreenOpen) { 473 | exitFullScreen() 474 | return 475 | } 476 | 477 | openFullScreen(this.elems.wrapper) 478 | }) 479 | 480 | this.off(document, 'fullscreenchange.chocolat') 481 | this.on(document, 'fullscreenchange.chocolat', () => { 482 | if ( 483 | document.fullscreenElement || 484 | document.webkitCurrentFullScreenElement || 485 | document.webkitFullscreenElement 486 | ) { 487 | this.state.fullScreenOpen = true 488 | } else { 489 | this.state.fullScreenOpen = false 490 | } 491 | }) 492 | this.off(document, 'webkitfullscreenchange.chocolat') 493 | this.on(document, 'webkitfullscreenchange.chocolat', () => { 494 | if ( 495 | document.fullscreenElement || 496 | document.webkitCurrentFullScreenElement || 497 | document.webkitFullscreenElement 498 | ) { 499 | this.state.fullScreenOpen = true 500 | } else { 501 | this.state.fullScreenOpen = false 502 | } 503 | }) 504 | if (this.settings.closeOnBackgroundClick) { 505 | this.off(this.elems.overlay, 'click.chocolat') 506 | this.on(this.elems.overlay, 'click.chocolat', this.close.bind(this)) 507 | } 508 | 509 | this.off(this.elems.wrapper, 'click.chocolat') 510 | this.on(this.elems.wrapper, 'click.chocolat', () => { 511 | if (this.state.initialZoomState === null || !this.state.visible) { 512 | return 513 | } 514 | 515 | this.elems.container.classList.add('chocolat-zooming-out') 516 | this.zoomOut().then(() => { 517 | this.elems.container.classList.remove('chocolat-zoomed') 518 | this.elems.container.classList.remove('chocolat-zooming-out') 519 | }) 520 | }) 521 | 522 | this.off(this.elems.imageWrapper, 'click.chocolat') 523 | this.on(this.elems.imageWrapper, 'click.chocolat', (e) => { 524 | if ( 525 | this.state.initialZoomState === null && 526 | this.elems.container.classList.contains('chocolat-zoomable') 527 | ) { 528 | e.stopPropagation() 529 | 530 | this.elems.container.classList.add('chocolat-zooming-in') 531 | this.zoomIn(e).then(() => { 532 | this.elems.container.classList.add('chocolat-zoomed') 533 | this.elems.container.classList.remove('chocolat-zooming-in') 534 | }) 535 | } 536 | }) 537 | 538 | this.on(this.elems.wrapper, 'mousemove.chocolat', (e) => { 539 | if (this.state.initialZoomState === null || !this.state.visible) { 540 | return 541 | } 542 | 543 | const rect = this.elems.wrapper.getBoundingClientRect() 544 | const pos = { 545 | top: rect.top + window.scrollY, 546 | left: rect.left + window.scrollX, 547 | } 548 | 549 | const height = this.elems.wrapper.clientHeight 550 | const width = this.elems.wrapper.clientWidth 551 | 552 | const imgWidth = this.elems.img.width 553 | const imgHeight = this.elems.img.height 554 | 555 | const coord = [e.pageX - width / 2 - pos.left, e.pageY - height / 2 - pos.top] 556 | 557 | let mvtX = 0 558 | if (imgWidth > width) { 559 | const paddingX = this.settings.zoomedPaddingX(imgWidth, width) 560 | mvtX = coord[0] / (width / 2) 561 | mvtX = ((imgWidth - width) / 2 + paddingX) * mvtX 562 | } 563 | 564 | let mvtY = 0 565 | if (imgHeight > height) { 566 | const paddingY = this.settings.zoomedPaddingY(imgHeight, height) 567 | mvtY = coord[1] / (height / 2) 568 | mvtY = ((imgHeight - height) / 2 + paddingY) * mvtY 569 | } 570 | 571 | this.elems.img.style.marginLeft = -mvtX + 'px' 572 | this.elems.img.style.marginTop = -mvtY + 'px' 573 | }) 574 | 575 | this.on(window, 'resize.chocolat', (e) => { 576 | if (!this.state.initialized || !this.state.visible) { 577 | return 578 | } 579 | debounce(50, () => { 580 | const fitOptions = { 581 | imgHeight: this.elems.img.naturalHeight, 582 | imgWidth: this.elems.img.naturalWidth, 583 | containerHeight: this.elems.wrapper.clientHeight, 584 | containerWidth: this.elems.wrapper.clientWidth, 585 | canvasWidth: this.elems.imageCanvas.clientWidth, 586 | canvasHeight: this.elems.imageCanvas.clientHeight, 587 | imageSize: this.settings.imageSize, 588 | } 589 | 590 | const { width, height } = fit(fitOptions) 591 | 592 | this.position(this.elems.img).then(() => { 593 | this.elems.container.classList.toggle( 594 | 'chocolat-zoomable', 595 | this.zoomable(this.elems.img, this.elems.wrapper) 596 | ) 597 | }) 598 | }) 599 | }) 600 | } 601 | 602 | zoomable(image, wrapper) { 603 | const wrapperWidth = wrapper.clientWidth 604 | const wrapperHeight = wrapper.clientHeight 605 | 606 | const isImageZoomable = 607 | this.settings.allowZoom && 608 | (image.naturalWidth > wrapperWidth || image.naturalHeight > wrapperHeight) 609 | ? true 610 | : false 611 | 612 | const isImageStretched = 613 | image.clientWidth > image.naturalWidth || image.clientHeight > image.naturalHeight 614 | 615 | return isImageZoomable && !isImageStretched 616 | } 617 | 618 | zoomIn(e) { 619 | this.state.initialZoomState = this.settings.imageSize 620 | this.settings.imageSize = 'native' 621 | 622 | return this.position(this.elems.img) 623 | } 624 | 625 | zoomOut(e) { 626 | this.settings.imageSize = this.state.initialZoomState || this.settings.imageSize 627 | this.state.initialZoomState = null 628 | this.elems.img.style.margin = 0 629 | 630 | return this.position(this.elems.img) 631 | } 632 | 633 | on(element, eventName, cb) { 634 | // const eventName = this.settings.setIndex + '-' + eventName 635 | const length = this.events.push({ 636 | element, 637 | eventName, 638 | cb, 639 | }) 640 | element.addEventListener(eventName.split('.')[0], this.events[length - 1].cb) 641 | } 642 | 643 | off(element, eventName) { 644 | // const eventName = this.settings.setIndex + '-' + eventName 645 | const index = this.events.findIndex((event) => { 646 | return event.element === element && event.eventName === eventName 647 | }) 648 | 649 | if (this.events[index]) { 650 | element.removeEventListener(eventName.split('.')[0], this.events[index].cb) 651 | this.events.splice(index, 1) 652 | } 653 | } 654 | } 655 | -------------------------------------------------------------------------------- /src/js/main.esm.js: -------------------------------------------------------------------------------- 1 | import { Chocolat, defaults } from './chocolat.js' 2 | 3 | const instances = [] 4 | 5 | export default function(elements, options) { 6 | const settings = Object.assign({}, defaults, { images: [] }, options, { 7 | setIndex: instances.length, 8 | }) 9 | const instance = new Chocolat(elements, settings) 10 | 11 | instances.push(instance) 12 | 13 | return instance 14 | } 15 | -------------------------------------------------------------------------------- /src/js/main.iife.js: -------------------------------------------------------------------------------- 1 | import { Chocolat, defaults } from './chocolat.js' 2 | 3 | import '../css/chocolat.css' 4 | 5 | const instances = [] 6 | 7 | window.Chocolat = function(elements, options) { 8 | const settings = Object.assign({}, defaults, { images: [] }, options, { 9 | setIndex: instances.length, 10 | }) 11 | const instance = new Chocolat(elements, settings) 12 | 13 | instances.push(instance) 14 | 15 | return instance 16 | } 17 | -------------------------------------------------------------------------------- /src/js/utils.js: -------------------------------------------------------------------------------- 1 | export let timerDebounce = undefined 2 | 3 | export function debounce(duration, callback) { 4 | clearTimeout(timerDebounce) 5 | timerDebounce = setTimeout(function() { 6 | callback() 7 | }, duration) 8 | 9 | return timerDebounce 10 | } 11 | 12 | export function transitionAsPromise(triggeringFunc, el) { 13 | return new Promise((resolve) => { 14 | const handleTransitionEnd = () => { 15 | el.removeEventListener('transitionend', handleTransitionEnd) 16 | resolve() 17 | } 18 | el.addEventListener('transitionend', handleTransitionEnd) 19 | 20 | const classesBefore = el.getAttribute('class') 21 | const stylesBefore = el.getAttribute('style') 22 | 23 | triggeringFunc() 24 | 25 | if ( 26 | classesBefore === el.getAttribute('class') && 27 | stylesBefore === el.getAttribute('style') 28 | ) { 29 | handleTransitionEnd() 30 | } 31 | if (parseFloat(getComputedStyle(el)['transitionDuration']) === 0) { 32 | handleTransitionEnd() 33 | } 34 | }) 35 | } 36 | 37 | export function loadImage({ src, srcset, sizes }) { 38 | const image = new Image() 39 | 40 | image.src = src 41 | if (srcset) { 42 | image.srcset = srcset 43 | } 44 | if (sizes) { 45 | image.sizes = sizes 46 | } 47 | 48 | if ('decode' in image) { 49 | return new Promise((resolve, reject) => { 50 | image 51 | .decode() 52 | .then(() => { 53 | resolve(image) 54 | }) 55 | .catch(() => { 56 | reject(image) 57 | }) 58 | }) 59 | } else { 60 | return new Promise((resolve, reject) => { 61 | image.onload = resolve(image) 62 | image.onerror = reject(image) 63 | }) 64 | } 65 | } 66 | 67 | export function fit(options) { 68 | let height 69 | let width 70 | 71 | const { 72 | imgHeight, 73 | imgWidth, 74 | containerHeight, 75 | containerWidth, 76 | canvasWidth, 77 | canvasHeight, 78 | imageSize, 79 | } = options 80 | 81 | const canvasRatio = canvasHeight / canvasWidth 82 | const containerRatio = containerHeight / containerWidth 83 | const imgRatio = imgHeight / imgWidth 84 | 85 | if (imageSize == 'cover') { 86 | if (imgRatio < containerRatio) { 87 | height = containerHeight 88 | width = height / imgRatio 89 | } else { 90 | width = containerWidth 91 | height = width * imgRatio 92 | } 93 | } else if (imageSize == 'native') { 94 | height = imgHeight 95 | width = imgWidth 96 | } else { 97 | if (imgRatio > canvasRatio) { 98 | height = canvasHeight 99 | width = height / imgRatio 100 | } else { 101 | width = canvasWidth 102 | height = width * imgRatio 103 | } 104 | if (imageSize === 'scale-down' && (width >= imgWidth || height >= imgHeight)) { 105 | width = imgWidth 106 | height = imgHeight 107 | } 108 | } 109 | 110 | return { 111 | height: height, 112 | width: width, 113 | } 114 | } 115 | 116 | export function openFullScreen(wrapper) { 117 | if (wrapper.requestFullscreen) { 118 | return wrapper.requestFullscreen() 119 | } else if (wrapper.webkitRequestFullscreen) { 120 | return wrapper.webkitRequestFullscreen() 121 | } else if (wrapper.msRequestFullscreen) { 122 | return wrapper.msRequestFullscreen() 123 | } else { 124 | return Promise.reject() 125 | } 126 | } 127 | 128 | export function exitFullScreen() { 129 | if (document.exitFullscreen) { 130 | return document.exitFullscreen() 131 | } else if (document.webkitExitFullscreen) { 132 | return document.webkitExitFullscreen() 133 | } else if (document.msExitFullscreen) { 134 | return document.msExitFullscreen() 135 | } else { 136 | return Promise.reject() 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /test/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Chocolat tests 6 | 7 | 8 | 9 | 10 |
14 | 15 |
16 | 1 17 | 2 18 | 3 19 |
20 | 21 | 22 |
23 | 34 | 35 | 36 |
37 | 38 |
39 |

Index

40 |
41 | 42 | 43 | 44 | 45 | 46 | 47 | 52 | 53 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | describe('Chocolat', function() { 2 | var chocolat 3 | 4 | beforeEach(function() {}) 5 | 6 | afterEach(function() { 7 | chocolat.api.destroy() 8 | }) 9 | 10 | describe('Opening', function() { 11 | it('should call markup function and create markup', function() { 12 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 13 | loop: true, 14 | container: document.querySelector('#container'), 15 | }) 16 | 17 | var spyMarkup = sinon.spy(chocolat, 'markup') 18 | 19 | $('#example0') 20 | .find('.chocolat-image') 21 | .first()[0] 22 | .click() 23 | 24 | expect(spyMarkup.calledOnce).to.be.true 25 | expect($('#container').find('.chocolat-wrapper').length).to.equal(1) 26 | expect($('#container .chocolat-wrapper').find('.chocolat-overlay').length).to.equal(1) 27 | expect($('#container .chocolat-wrapper').find('.chocolat-loader').length).to.equal(1) 28 | expect($('#container .chocolat-wrapper').find('.chocolat-layout').length).to.equal(1) 29 | expect( 30 | $('#container .chocolat-wrapper').find('.chocolat-image-canvas').length 31 | ).to.equal(1) 32 | expect($('#container .chocolat-wrapper').find('.chocolat-center').length).to.equal(1) 33 | expect($('#container .chocolat-wrapper').find('.chocolat-top').length).to.equal(1) 34 | expect($('#container .chocolat-wrapper').find('.chocolat-bottom').length).to.equal(1) 35 | expect($('#container .chocolat-wrapper').find('.chocolat-left').length).to.equal(1) 36 | expect($('#container .chocolat-wrapper').find('.chocolat-right').length).to.equal(1) 37 | expect($('#container .chocolat-top').find('.chocolat-close').length).to.equal(1) 38 | expect($('#container .chocolat-bottom').find('.chocolat-pagination').length).to.equal(1) 39 | expect($('#container .chocolat-bottom').find('.chocolat-description').length).to.equal( 40 | 1 41 | ) 42 | expect($('#container .chocolat-bottom').find('.chocolat-fullscreen').length).to.equal(1) 43 | expect($('#container .chocolat-bottom').find('.chocolat-set-title').length).to.equal(1) 44 | expect($('#container .chocolat-image-wrapper').find('.chocolat-img').length).to.equal(1) 45 | }) 46 | 47 | it('should add css classes to parent when in container', function(done) { 48 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 49 | container: document.querySelector('#container'), 50 | }) 51 | 52 | chocolat.api.open().then(function() { 53 | expect($('#container').hasClass('chocolat-in-container')).to.be.true 54 | expect($('#container').hasClass('chocolat-open')).to.be.true 55 | // expect($('#container').hasClass('chocolat-zoomable')).to.be.true 56 | return done() 57 | }) 58 | }) 59 | 60 | it('should add css classes to body when full window', function(done) { 61 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 62 | imageSize: 'cover', 63 | }) 64 | 65 | chocolat.api.open().then(function() { 66 | expect($('body').hasClass('chocolat-open')).to.be.true 67 | expect($('body').hasClass('chocolat-' + chocolat.settings.imageSize)).to.be.true 68 | // expect($('#container').hasClass('chocolat-zoomable')).to.be.true 69 | return done() 70 | }) 71 | }) 72 | 73 | it('should add custom css classes', function(done) { 74 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 75 | className: 'custom-class-name', 76 | }) 77 | 78 | chocolat.api.open().then(function() { 79 | expect($('body').hasClass('custom-class-name')).to.be.true 80 | return done() 81 | }) 82 | }) 83 | 84 | it('should call init function', function() { 85 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 86 | loop: true, 87 | container: document.querySelector('#container'), 88 | }) 89 | 90 | var spyInit = sinon.spy(chocolat, 'init') 91 | 92 | $('#example0') 93 | .find('.chocolat-image') 94 | .first()[0] 95 | .click() 96 | 97 | expect(spyInit.calledOnce).to.be.true 98 | expect(spyInit.calledWithExactly(0)).to.be.true 99 | }) 100 | 101 | it('should call load function', function() { 102 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 103 | loop: true, 104 | container: document.querySelector('#container'), 105 | }) 106 | 107 | var spyLoad = sinon.spy(chocolat, 'load') 108 | 109 | $('#example0') 110 | .find('.chocolat-image') 111 | .first()[0] 112 | .click() 113 | 114 | expect(spyLoad.calledOnce).to.be.true 115 | expect(spyLoad.calledWithExactly(0)).to.be.true 116 | }) 117 | 118 | it('should call markup function', function() { 119 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 120 | loop: true, 121 | container: document.querySelector('#container'), 122 | }) 123 | 124 | var spyMarkup = sinon.spy(chocolat, 'markup') 125 | 126 | $('#example0') 127 | .find('.chocolat-image') 128 | .first()[0] 129 | .click() 130 | 131 | expect(spyMarkup.calledOnce).to.be.true 132 | }) 133 | 134 | it('should append description element in top element (instead of bottom, the default behaviour)', function() { 135 | var afterMarkup = function() { 136 | return $(this.elems.description).appendTo(this.elems.top) 137 | } 138 | 139 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 140 | loop: true, 141 | container: document.querySelector('#container'), 142 | afterMarkup: afterMarkup, 143 | }) 144 | 145 | $('#example0') 146 | .find('.chocolat-image') 147 | .first()[0] 148 | .click() 149 | 150 | const top = chocolat.api.getElem('top') 151 | const bottom = chocolat.api.getElem('bottom') 152 | expect($(top).find(chocolat.api.getElem('description')).length).to.equal(1) 153 | expect($(bottom).find(chocolat.api.getElem('description')).length).to.equal(0) 154 | }) 155 | 156 | it('can define a title', function() { 157 | var setTitle = function() { 158 | return 'hello' 159 | } 160 | 161 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 162 | container: document.querySelector('#container'), 163 | setTitle: setTitle, 164 | }) 165 | 166 | $('#example0') 167 | .find('.chocolat-image') 168 | .first()[0] 169 | .click() 170 | 171 | const elem = chocolat.api.getElem('setTitle') 172 | expect(elem.textContent).to.equal('hello') 173 | }) 174 | 175 | it('can define a description per image', function(done) { 176 | var description = function() { 177 | return 'prefix ' + this.images[this.settings.currentImageIndex].title + ' suffix' 178 | } 179 | 180 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 181 | container: document.querySelector('#container'), 182 | description: description, 183 | }) 184 | 185 | chocolat.api.open().then(() => { 186 | const elem = chocolat.api.getElem('description') 187 | expect(elem.textContent).to.equal('prefix foo suffix') 188 | 189 | chocolat.api.next().then(() => { 190 | const elem = chocolat.api.getElem('description') 191 | expect(elem.textContent).to.equal('prefix baz suffix') 192 | done() 193 | }) 194 | }) 195 | }) 196 | 197 | it('can define a pagination per image', function(done) { 198 | var pagination = function() { 199 | var last = this.settings.lastImageIndex + 1 200 | var position = this.settings.currentImageIndex + 1 201 | 202 | return 'prefix ' + position + '/' + last + ' suffix' 203 | } 204 | 205 | chocolat = Chocolat(document.querySelectorAll('#example0 .chocolat-image'), { 206 | container: document.querySelector('#container'), 207 | pagination: pagination, 208 | }) 209 | 210 | chocolat.api.open().then(() => { 211 | const elem = chocolat.api.getElem('pagination') 212 | expect(elem.textContent).to.equal('prefix 1/3 suffix') 213 | 214 | chocolat.api.next().then(() => { 215 | const elem = chocolat.api.getElem('pagination') 216 | expect(elem.textContent).to.equal('prefix 2/3 suffix') 217 | done() 218 | }) 219 | }) 220 | }) 221 | }) 222 | 223 | describe('Destroy method', function() { 224 | it('should remove wrapper element', function(done) { 225 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image')) 226 | 227 | chocolat.api.open().then(function() { 228 | var lengthBefore = $('.chocolat-wrapper').length 229 | expect(lengthBefore).to.equal(1) 230 | 231 | chocolat.api.destroy() 232 | 233 | var lengthAfter = $('.chocolat-wrapper').length 234 | expect(lengthAfter).to.equal(0) 235 | return done() 236 | }) 237 | }) 238 | 239 | it('should unbind event handler of the links (triggering chocolat.open)', function() { 240 | const links = document.querySelectorAll('.chocolat-image') 241 | chocolat = Chocolat(links) 242 | 243 | var eventBefore = Object.values(chocolat.events) 244 | expect(eventBefore.length).to.be.above(0) 245 | 246 | chocolat.api.destroy() 247 | 248 | var eventAfter = Object.values(chocolat.events) 249 | expect(eventAfter.length).to.equal(0) 250 | 251 | var spyInit = sinon.spy(chocolat, 'init') 252 | 253 | const link = links[0] 254 | const linkBefore = link.getAttribute('href') 255 | 256 | link.setAttribute('href', '#') 257 | link.click() 258 | 259 | expect(spyInit.notCalled).to.be.true 260 | link.setAttribute('href', linkBefore) 261 | }) 262 | }) 263 | 264 | describe('Change image', function() { 265 | it('should go to next image', function(done) { 266 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image')) 267 | 268 | var spyLoad = sinon.spy(chocolat, 'load') 269 | var spyChange = sinon.spy(chocolat, 'change') 270 | 271 | chocolat.api.open().then(function() { 272 | expect(spyLoad.calledWithExactly(0)).to.be.true 273 | 274 | chocolat.elems.right.click() 275 | 276 | expect(spyLoad.calledWithExactly(1)).to.be.true 277 | 278 | expect(spyChange.calledOnce).to.be.true 279 | expect(spyChange.calledWithExactly(1)).to.be.true 280 | 281 | return done() 282 | }) 283 | }) 284 | 285 | it('should go to previous image', function(done) { 286 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image')) 287 | 288 | chocolat.api.open().then(function() { 289 | var spyLoad = sinon.spy(chocolat, 'load') 290 | 291 | chocolat.api.next().then(function() { 292 | expect(spyLoad.calledWithExactly(1)).to.be.true 293 | var spyChange = sinon.spy(chocolat, 'change') 294 | 295 | chocolat.api.prev().then(function() { 296 | expect(spyLoad.calledWithExactly(0)).to.be.true 297 | 298 | expect(spyChange.calledOnce).to.be.true 299 | expect(spyChange.calledWithExactly(-1)).to.be.true 300 | 301 | expect(chocolat.api.current()).to.equal(0) 302 | 303 | return done() 304 | }) 305 | }) 306 | }) 307 | }) 308 | 309 | it('should loop and go to last image', function(done) { 310 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 311 | loop: true, 312 | }) 313 | 314 | chocolat.api.open().then(function() { 315 | var spyLoad = sinon.spy(chocolat, 'load') 316 | 317 | chocolat.api.prev().then(function() { 318 | expect(spyLoad.calledWithExactly(chocolat.api.get('lastImageIndex'))).to.be.true 319 | expect(chocolat.api.current()).to.equal(chocolat.api.get('lastImageIndex')) 320 | 321 | return done() 322 | }) 323 | }) 324 | }) 325 | 326 | return it('should loop and go to first image', function(done) { 327 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 328 | loop: true, 329 | }) 330 | 331 | var lastImageIndex = chocolat.images.length - 1 332 | 333 | chocolat.api.open(lastImageIndex).then(function() { 334 | var spyLoad = sinon.spy(chocolat, 'load') 335 | 336 | chocolat.api.next().then(function() { 337 | expect(spyLoad.calledWithExactly(0)).to.be.true 338 | expect(chocolat.api.current()).to.equal(0) 339 | 340 | return done() 341 | }) 342 | }) 343 | }) 344 | }) 345 | 346 | describe('ImageSize cover', function() { 347 | it('should add class chocolat-cover to parent', function(done) { 348 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 349 | imageSize: 'cover', 350 | }) 351 | 352 | chocolat.api.open().then(function() { 353 | expect(chocolat.api.get('imageSize')).to.equal('cover') 354 | expect( 355 | chocolat.api.getElem('container').classList.contains('chocolat-cover') 356 | ).to.be.true 357 | return done() 358 | }) 359 | }) 360 | 361 | it("should have 'shortest' side of the container equal to 'shortest' side to the image in container", function(done) { 362 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 363 | imageSize: 'cover', 364 | container: document.querySelector('#container'), 365 | }) 366 | 367 | chocolat.api.open().then(function() { 368 | var dim = getExpectedDimensions(chocolat) 369 | 370 | if (dim.imgRatio < dim.containerRatio) { 371 | var targetWidth = dim.imgHeight / dim.imgRatio 372 | // 1px delta, because of rounded values 373 | expect(dim.imgWidth).to.be.closeTo(targetWidth, 1) 374 | expect(dim.imgHeight).to.be.closeTo(dim.containerHeight, 1) 375 | } else { 376 | var targetHeight = dim.imgWidth * dim.imgRatio 377 | // 1px delta, because of rounded values 378 | expect(dim.imgHeight).to.be.closeTo(targetHeight, 1) 379 | expect(dim.imgWidth).to.be.closeTo(dim.containerWidth, 1) 380 | } 381 | 382 | return done() 383 | }) 384 | }) 385 | 386 | it("should have 'shortest' side of the container equal to 'shortest' side to the image in window", function(done) { 387 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 388 | imageSize: 'cover', 389 | }) 390 | 391 | chocolat.api.open().then(function() { 392 | var dim = getExpectedDimensions(chocolat) 393 | 394 | if (dim.imgRatio < dim.containerRatio) { 395 | var targetWidth = dim.imgHeight / dim.imgRatio 396 | // 1px delta, because of rounded values 397 | expect(dim.imgWidth).to.be.closeTo(targetWidth, 1) 398 | expect(dim.imgHeight).to.be.closeTo(dim.containerHeight, 1) 399 | } else { 400 | var targetHeight = dim.imgWidth * dim.imgRatio 401 | // 1px delta, because of rounded values 402 | expect(dim.imgHeight).to.be.closeTo(targetHeight, 1) 403 | expect(dim.imgWidth).to.be.closeTo(dim.containerWidth, 1) 404 | } 405 | 406 | return done() 407 | }) 408 | }) 409 | }) 410 | 411 | describe('ImageSize contain', function() { 412 | it("should have 'longest' side of the container equal to 'longest' side to the image in container", function(done) { 413 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 414 | imageSize: 'contain', 415 | container: document.querySelector('#container'), 416 | }) 417 | 418 | chocolat.api.open().then(function() { 419 | var dim = getExpectedDimensions(chocolat) 420 | 421 | if (dim.imgRatio > dim.canvasRatio) { 422 | var targetWidth = dim.canvasHeight / dim.imgRatio 423 | // 1px delta, because of rounded values 424 | expect(dim.imgWidth).to.be.closeTo(targetWidth, 1) 425 | expect(dim.imgHeight).to.be.closeTo(dim.canvasHeight, 1) 426 | } else { 427 | var targetHeight = dim.canvasWidth * dim.imgRatio 428 | // 1px delta, because of rounded values 429 | expect(dim.imgHeight).to.be.closeTo(targetHeight, 1) 430 | expect(dim.imgWidth).to.be.closeTo(dim.canvasWidth, 1) 431 | } 432 | 433 | return done() 434 | }) 435 | }) 436 | 437 | it("should have 'longest' side of the container equal to 'longest' side to the image in window", function(done) { 438 | chocolat = Chocolat(document.querySelectorAll('.chocolat-image'), { 439 | imageSize: 'contain', 440 | }) 441 | 442 | chocolat.api.open().then(function() { 443 | var dim = getExpectedDimensions(chocolat) 444 | 445 | if (dim.imgRatio > dim.canvasRatio) { 446 | var targetWidth = dim.canvasHeight / dim.imgRatio 447 | // 1px delta, because of rounded values 448 | expect(dim.imgWidth).to.be.closeTo(targetWidth, 1) 449 | expect(dim.imgHeight).to.be.closeTo(dim.canvasHeight, 1) 450 | } else { 451 | var targetHeight = dim.canvasWidth * dim.imgRatio 452 | // 1px delta, because of rounded values 453 | expect(dim.imgHeight).to.be.closeTo(targetHeight, 1) 454 | expect(dim.imgWidth).to.be.closeTo(dim.canvasWidth, 1) 455 | } 456 | 457 | return done() 458 | }) 459 | }) 460 | }) 461 | 462 | describe('API', function() { 463 | before(function() { 464 | chocolat = Chocolat([ 465 | { src: '../demo/demo-images/1.jpg', title: 'You can zoom in the image' }, 466 | { src: '../demo/demo-images/2.jpg', title: 'You can zoom in the image' }, 467 | { src: '../demo/demo-images/3.jpg', title: 'You can zoom in the image' }, 468 | ]) 469 | }) 470 | 471 | it('should have a open method', function() { 472 | expect(typeof chocolat.api.open).to.equal('function') 473 | }) 474 | 475 | it('should have a close method', function() { 476 | expect(typeof chocolat.api.close).to.equal('function') 477 | }) 478 | 479 | it('should have a next method', function() { 480 | expect(typeof chocolat.api.next).to.equal('function') 481 | }) 482 | 483 | it('should have a prev method', function() { 484 | expect(typeof chocolat.api.prev).to.equal('function') 485 | }) 486 | 487 | it('should have a goto method', function() { 488 | expect(typeof chocolat.api.goto).to.equal('function') 489 | }) 490 | 491 | it('should have a current method', function() { 492 | expect(typeof chocolat.api.current).to.equal('function') 493 | }) 494 | 495 | it('should have a position method', function() { 496 | expect(typeof chocolat.api.position).to.equal('function') 497 | }) 498 | 499 | it('should have a destroy method', function() { 500 | expect(typeof chocolat.api.destroy).to.equal('function') 501 | }) 502 | 503 | it('should have a set method', function() { 504 | expect(typeof chocolat.api.set).to.equal('function') 505 | }) 506 | 507 | it('should have a get method', function() { 508 | expect(typeof chocolat.api.get).to.equal('function') 509 | }) 510 | 511 | it('should have a getElem method', function() { 512 | expect(typeof chocolat.api.getElem).to.equal('function') 513 | }) 514 | }) 515 | 516 | describe('srcset and sizes attributes', function() { 517 | before(function() { 518 | chocolat = Chocolat(document.querySelectorAll('#example1 .chocolat-image'), {}) 519 | }) 520 | it('Should include the same srcset and sizes attributes from the trigger link (data-srcset and data-sizes)', function() { 521 | const trigger = document.querySelector('#example1 .chocolat-image') 522 | 523 | chocolat.api.open().then(function() { 524 | const openedImg = document.querySelector('.chocolat-image-wrapper img.chocolat-img') 525 | 526 | expect(openedImg.getAttribute('srcset')).to.be.equal( 527 | trigger.getAttribute('data-srcset') 528 | ) 529 | expect(openedImg.getAttribute('sizes')).to.be.equal( 530 | trigger.getAttribute('data-sizes') 531 | ) 532 | }) 533 | }) 534 | it('Should not add empty srcset or sizes attributes', function() { 535 | const trigger = document.querySelector('#example0 .chocolat-image') 536 | 537 | chocolat.api.open().then(function() { 538 | const openedImg = document.querySelector('.chocolat-image-wrapper img.chocolat-img') 539 | 540 | expect(openedImg.getAttribute('srcset')).to.be.null() 541 | expect(openedImg.getAttribute('sizes')).to.be.null() 542 | }) 543 | }) 544 | }) 545 | }) 546 | 547 | function getJqueryElement(name) {} 548 | 549 | // function used to calculate image dimensions 550 | // in order to be compared with what chocolat is really displaying 551 | function getExpectedDimensions(chocolat) { 552 | var imgWidth = chocolat.api.getElem('img').width 553 | var imgHeight = chocolat.api.getElem('img').height 554 | 555 | var containerWidth = chocolat.api.getElem('container').clientWidth 556 | var containerHeight = chocolat.api.getElem('container').clientHeight 557 | 558 | var canvasWidth = chocolat.api.getElem('imageCanvas').clientWidth 559 | var canvasHeight = chocolat.api.getElem('imageCanvas').clientHeight 560 | 561 | var imgRatio = imgHeight / imgWidth 562 | var containerRatio = containerHeight / containerWidth 563 | var canvasRatio = canvasHeight / canvasWidth 564 | 565 | return { 566 | imgWidth: imgWidth, 567 | imgHeight: imgHeight, 568 | containerWidth: containerWidth, 569 | containerHeight: containerHeight, 570 | canvasWidth: canvasWidth, 571 | canvasHeight: canvasHeight, 572 | imgRatio: imgRatio, 573 | containerRatio: containerRatio, 574 | canvasRatio: canvasRatio, 575 | } 576 | } 577 | --------------------------------------------------------------------------------