lightbox.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. var __metadata = (this && this.__metadata) || function (k, v) {
  9. if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. var core_1 = require("@angular/core");
  13. var common_1 = require("@angular/common");
  14. var domhandler_1 = require("../dom/domhandler");
  15. var Lightbox = /** @class */ (function () {
  16. function Lightbox(el, renderer, cd) {
  17. this.el = el;
  18. this.renderer = renderer;
  19. this.cd = cd;
  20. this.type = 'image';
  21. this.effectDuration = '500ms';
  22. this.autoZIndex = true;
  23. this.baseZIndex = 0;
  24. this.closeOnEscape = true;
  25. }
  26. Lightbox.prototype.onImageClick = function (event, image, i, content) {
  27. this.index = i;
  28. this.loading = true;
  29. content.style.width = 32 + 'px';
  30. content.style.height = 32 + 'px';
  31. this.preventDocumentClickListener = true;
  32. this.show();
  33. this.displayImage(image);
  34. event.preventDefault();
  35. };
  36. Lightbox.prototype.ngAfterViewInit = function () {
  37. this.panel = domhandler_1.DomHandler.findSingle(this.el.nativeElement, '.ui-lightbox ');
  38. if (this.appendTo) {
  39. if (this.appendTo === 'body')
  40. document.body.appendChild(this.panel);
  41. else
  42. domhandler_1.DomHandler.appendChild(this.panel, this.appendTo);
  43. }
  44. };
  45. Lightbox.prototype.onLinkClick = function (event, content) {
  46. this.preventDocumentClickListener = true;
  47. this.show();
  48. event.preventDefault();
  49. };
  50. Lightbox.prototype.displayImage = function (image) {
  51. var _this = this;
  52. setTimeout(function () {
  53. _this.cd.markForCheck();
  54. _this.currentImage = image;
  55. _this.captionText = image.title;
  56. _this.center();
  57. }, 1000);
  58. };
  59. Lightbox.prototype.show = function () {
  60. this.mask = document.createElement('div');
  61. domhandler_1.DomHandler.addMultipleClasses(this.mask, 'ui-widget-overlay ui-dialog-mask');
  62. document.body.appendChild(this.mask);
  63. if (this.autoZIndex) {
  64. this.zindex = this.baseZIndex + (++domhandler_1.DomHandler.zindex);
  65. }
  66. this.mask.style.zIndex = this.zindex - 1;
  67. this.center();
  68. this.visible = true;
  69. this.bindGlobalListeners();
  70. };
  71. Lightbox.prototype.hide = function (event) {
  72. this.captionText = null;
  73. this.index = null;
  74. this.currentImage = null;
  75. this.visible = false;
  76. if (this.mask) {
  77. document.body.removeChild(this.mask);
  78. this.mask = null;
  79. }
  80. this.unbindGlobalListeners();
  81. event.preventDefault();
  82. };
  83. Lightbox.prototype.center = function () {
  84. var elementWidth = domhandler_1.DomHandler.getOuterWidth(this.panel);
  85. var elementHeight = domhandler_1.DomHandler.getOuterHeight(this.panel);
  86. if (elementWidth == 0 && elementHeight == 0) {
  87. this.panel.style.visibility = 'hidden';
  88. this.panel.style.display = 'block';
  89. elementWidth = domhandler_1.DomHandler.getOuterWidth(this.panel);
  90. elementHeight = domhandler_1.DomHandler.getOuterHeight(this.panel);
  91. this.panel.style.display = 'none';
  92. this.panel.style.visibility = 'visible';
  93. }
  94. };
  95. Lightbox.prototype.onImageLoad = function (event, content) {
  96. var _this = this;
  97. var image = event.target;
  98. image.style.visibility = 'hidden';
  99. image.style.display = 'block';
  100. var imageWidth = domhandler_1.DomHandler.getOuterWidth(image);
  101. var imageHeight = domhandler_1.DomHandler.getOuterHeight(image);
  102. image.style.display = 'none';
  103. image.style.visibility = 'visible';
  104. content.style.width = imageWidth + 'px';
  105. content.style.height = imageHeight + 'px';
  106. this.panel.style.left = parseInt(this.panel.style.left) + (domhandler_1.DomHandler.getOuterWidth(this.panel) - imageWidth) / 2 + 'px';
  107. this.panel.style.top = parseInt(this.panel.style.top) + (domhandler_1.DomHandler.getOuterHeight(this.panel) - imageHeight) / 2 + 'px';
  108. setTimeout(function () {
  109. _this.cd.markForCheck();
  110. domhandler_1.DomHandler.fadeIn(image, 500);
  111. image.style.display = 'block';
  112. //this.captionText = this.currentImage.title;
  113. _this.loading = false;
  114. }, parseInt(this.effectDuration));
  115. };
  116. Lightbox.prototype.prev = function (placeholder) {
  117. this.captionText = null;
  118. this.loading = true;
  119. placeholder.style.display = 'none';
  120. if (this.index > 0) {
  121. this.displayImage(this.images[--this.index]);
  122. }
  123. };
  124. Lightbox.prototype.next = function (placeholder) {
  125. this.captionText = null;
  126. this.loading = true;
  127. placeholder.style.display = 'none';
  128. if (this.index <= (this.images.length - 1)) {
  129. this.displayImage(this.images[++this.index]);
  130. }
  131. };
  132. Lightbox.prototype.bindGlobalListeners = function () {
  133. var _this = this;
  134. this.documentClickListener = this.renderer.listen('document', 'click', function (event) {
  135. if (!_this.preventDocumentClickListener && _this.visible) {
  136. _this.hide(event);
  137. }
  138. _this.preventDocumentClickListener = false;
  139. _this.cd.markForCheck();
  140. });
  141. if (this.closeOnEscape && !this.documentEscapeListener) {
  142. this.documentEscapeListener = this.renderer.listen('document', 'keydown', function (event) {
  143. if (event.which == 27) {
  144. if (parseInt(_this.panel.style.zIndex) === (domhandler_1.DomHandler.zindex + _this.baseZIndex)) {
  145. _this.hide(event);
  146. }
  147. }
  148. });
  149. }
  150. };
  151. Lightbox.prototype.unbindGlobalListeners = function () {
  152. if (this.documentEscapeListener) {
  153. this.documentEscapeListener();
  154. this.documentEscapeListener = null;
  155. }
  156. if (this.documentClickListener) {
  157. this.documentClickListener();
  158. this.documentClickListener = null;
  159. }
  160. };
  161. Object.defineProperty(Lightbox.prototype, "leftVisible", {
  162. get: function () {
  163. return this.images && this.images.length && this.index != 0 && !this.loading;
  164. },
  165. enumerable: true,
  166. configurable: true
  167. });
  168. Object.defineProperty(Lightbox.prototype, "rightVisible", {
  169. get: function () {
  170. return this.images && this.images.length && this.index < (this.images.length - 1) && !this.loading;
  171. },
  172. enumerable: true,
  173. configurable: true
  174. });
  175. Lightbox.prototype.ngOnDestroy = function () {
  176. this.unbindGlobalListeners();
  177. if (this.appendTo) {
  178. this.el.nativeElement.appendChild(this.panel);
  179. }
  180. };
  181. __decorate([
  182. core_1.Input(),
  183. __metadata("design:type", Array)
  184. ], Lightbox.prototype, "images", void 0);
  185. __decorate([
  186. core_1.Input(),
  187. __metadata("design:type", String)
  188. ], Lightbox.prototype, "type", void 0);
  189. __decorate([
  190. core_1.Input(),
  191. __metadata("design:type", Object)
  192. ], Lightbox.prototype, "style", void 0);
  193. __decorate([
  194. core_1.Input(),
  195. __metadata("design:type", String)
  196. ], Lightbox.prototype, "styleClass", void 0);
  197. __decorate([
  198. core_1.Input(),
  199. __metadata("design:type", Object)
  200. ], Lightbox.prototype, "appendTo", void 0);
  201. __decorate([
  202. core_1.Input(),
  203. __metadata("design:type", String)
  204. ], Lightbox.prototype, "easing", void 0);
  205. __decorate([
  206. core_1.Input(),
  207. __metadata("design:type", Object)
  208. ], Lightbox.prototype, "effectDuration", void 0);
  209. __decorate([
  210. core_1.Input(),
  211. __metadata("design:type", Boolean)
  212. ], Lightbox.prototype, "autoZIndex", void 0);
  213. __decorate([
  214. core_1.Input(),
  215. __metadata("design:type", Number)
  216. ], Lightbox.prototype, "baseZIndex", void 0);
  217. __decorate([
  218. core_1.Input(),
  219. __metadata("design:type", Boolean)
  220. ], Lightbox.prototype, "closeOnEscape", void 0);
  221. Lightbox = __decorate([
  222. core_1.Component({
  223. selector: 'p-lightbox',
  224. template: "\n <div [ngStyle]=\"style\" [class]=\"styleClass\" *ngIf=\"(type == 'image')\">\n <a *ngFor=\"let image of images; let i = index;\" [href]=\"image.source\" (click)=\"onImageClick($event,image,i,content)\">\n <img [src]=\"image.thumbnail\" [title]=\"image.title\" [alt]=\"image.alt\">\n </a>\n </div>\n <span [ngStyle]=\"style\" [class]=\"styleClass\" *ngIf=\"(type == 'content')\" (click)=\"onLinkClick($event,content)\">\n <ng-content select=\"a\"></ng-content>\n </span>\n <div class=\"ui-lightbox ui-widget ui-corner-all ui-shadow\" [style.display]=\"visible ? 'block' : 'none'\" [style.zIndex]=\"zindex\"\n [ngClass]=\"{'ui-lightbox-loading': loading}\"\n [style.transitionProperty]=\"'all'\" [style.transitionDuration]=\"effectDuration\" [style.transitionTimingFunction]=\"easing\" (click)=\"preventDocumentClickListener=true\">\n <div class=\"ui-lightbox-content-wrapper\">\n <a class=\"ui-state-default ui-lightbox-nav-left ui-corner-right\" [style.zIndex]=\"zindex + 1\" (click)=\"prev(img)\"\n [ngClass]=\"{'ui-helper-hidden':!leftVisible}\"><span class=\"ui-lightbox-nav-icon pi pi-chevron-left\"></span></a>\n <div #content class=\"ui-lightbox-content ui-corner-all\" \n [style.transitionProperty]=\"'width,height'\" [style.transitionDuration]=\"effectDuration\" [style.transitionTimingFunction]=\"easing\">\n <img #img [src]=\"currentImage ? currentImage.source||'' : ''\" (load)=\"onImageLoad($event,content)\" style=\"display:none\">\n <ng-content></ng-content>\n </div>\n <a class=\"ui-state-default ui-lightbox-nav-right ui-corner-left ui-helper-hidden\" [style.zIndex]=\"zindex + 1\" (click)=\"next(img)\"\n [ngClass]=\"{'ui-helper-hidden':!rightVisible}\"><span class=\"ui-lightbox-nav-icon pi pi-chevron-right\"></span></a>\n </div>\n <div class=\"ui-lightbox-caption ui-widget-header\" [style.display]=\"captionText ? 'block' : 'none'\">\n <span class=\"ui-lightbox-caption-text\">{{captionText}}</span><a class=\"ui-lightbox-close ui-corner-all\" tabindex=\"0\" (click)=\"hide($event)\" (keydown.enter)=\"hide($event)\"><span class=\"pi pi-times\"></span></a>\n <div style=\"clear:both\"></div>\n </div>\n </div>\n "
  225. }),
  226. __metadata("design:paramtypes", [core_1.ElementRef, core_1.Renderer2, core_1.ChangeDetectorRef])
  227. ], Lightbox);
  228. return Lightbox;
  229. }());
  230. exports.Lightbox = Lightbox;
  231. var LightboxModule = /** @class */ (function () {
  232. function LightboxModule() {
  233. }
  234. LightboxModule = __decorate([
  235. core_1.NgModule({
  236. imports: [common_1.CommonModule],
  237. exports: [Lightbox],
  238. declarations: [Lightbox]
  239. })
  240. ], LightboxModule);
  241. return LightboxModule;
  242. }());
  243. exports.LightboxModule = LightboxModule;
  244. //# sourceMappingURL=lightbox.js.map