forked from chrishutchinson/cardkit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dom.js
3745 lines (3011 loc) · 153 KB
/
dom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("react"), require("react-dom"), require("rvg.js"), require("deep-extend"), require("react-color"));
else if(typeof define === 'function' && define.amd)
define("CardKitDOM", ["react", "react-dom", "rvg.js", "deep-extend", "react-color"], factory);
else if(typeof exports === 'object')
exports["CardKitDOM"] = factory(require("react"), require("react-dom"), require("rvg.js"), require("deep-extend"), require("react-color"));
else
root["CardKitDOM"] = factory(root["react"], root["react-dom"], root["rvg.js"], root["deep-extend"], root["react-color"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_1__, __WEBPACK_EXTERNAL_MODULE_2__, __WEBPACK_EXTERNAL_MODULE_4__, __WEBPACK_EXTERNAL_MODULE_7__, __WEBPACK_EXTERNAL_MODULE_30__) {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
// Dependencies
var React = __webpack_require__(1);
var ReactDOM = __webpack_require__(2);
var Card = __webpack_require__(3);
var CardKitRenderer = __webpack_require__(5);
var UI = __webpack_require__(8);
var SVGToImage = __webpack_require__(49);
var _require = __webpack_require__(28);
var slugify = _require.slugify;
/**
* @name CardKitDOM
* @class Additional CardKit class used for rendering in the DOM
*/
var CardKitDOM = function (_CardKitRenderer) {
_inherits(CardKitDOM, _CardKitRenderer);
/**
* Constructor takes in an instance of CardKit and stores it for later use
*
* @param {CardKit} cardkit - An instance of CardKit
*/
function CardKitDOM(cardkit) {
_classCallCheck(this, CardKitDOM);
// Ensure we're operating in a browser environment
if (typeof document === 'undefined') {
throw new Error('CardKitDOM can only be used in a browser environment');
}
// Store render IDs
var _this = _possibleConstructorReturn(this, (CardKitDOM.__proto__ || Object.getPrototypeOf(CardKitDOM)).call(this, cardkit));
_this.renderedCardID = null;
_this.renderedUIID = null;
return _this;
}
/**
* Renders the built-in UI to the supplied element
*
* @param {string} id - The ID of the element to render the UI into
*/
_createClass(CardKitDOM, [{
key: 'renderUI',
value: function renderUI(id) {
if (!this._isValidElement(id)) {
throw new Error('Invalid element ID provided');
}
var element = document.getElementById(id);
var template = this.cardkit.templates ? Object.keys(this.cardkit.templates)[0] : false;
var theme = this.cardkit.themes ? Object.keys(this.cardkit.themes)[0] : false;
var layout = this.cardkit.layouts ? Object.keys(this.cardkit.layouts)[0] : false;
this.renderedUIID = id;
ReactDOM.render(React.createElement(UI, {
configuration: this.computeConfiguration({
template: template,
theme: theme,
layout: layout
}),
templates: this.cardkit.templates,
themes: this.cardkit.themes,
layouts: this.cardkit.layouts,
cardKit: this
}), element);
}
/**
* Renders just the Card as a React component to the supplied element
*
* @param {string} id - The ID of the element to render the card into
* @param {object} options - Any override data (e.g. theme, layout) to use when rendering the card
*/
}, {
key: 'renderCard',
value: function renderCard(id, options) {
if (!this._isValidElement(id)) {
throw new Error('Invalid element ID provided');
}
var element = document.getElementById(id);
this.renderedCardID = id;
ReactDOM.render(React.createElement(Card, { configuration: this.computeConfiguration(options) }), element);
}
/**
* Checks if the ID provided is valid
*
* @param {string} id - The ID to validate
* @return {boolean} If the ID was valid
*/
}, {
key: '_isValidElement',
value: function _isValidElement(id) {
if (!id) {
return false;
}
var element = document.getElementById(id);
if (!element) {
return false;
}
return true;
}
/**
* Re-renders the Card or UI
*/
}, {
key: 'rerender',
value: function rerender() {
if (this.renderedUIID) {
this.renderUI(this.renderedUIID);
}
if (this.renderedCardID) {
this.renderCard(this.renderedCardID);
}
}
/**
* Downloads the card as an image in the browser
*
* @param {number} scale - The scale to output at
* @param {object} element - The element to use to generate the image
*/
}, {
key: 'download',
value: function download() {
var scale = arguments.length <= 0 || arguments[0] === undefined ? 2 : arguments[0];
var element = arguments[1];
element = element.childNodes[0] || document.getElementById(this.renderedCardID).childNodes[0];
var svgToImage = new SVGToImage(element);
// Setup default filename
var filename = 'cardkit-default.jpg';
// Get the configuration
var configuration = this.computeConfiguration();
// If there's a layer that has the useAsFilename property, find it
var filenameLayerKey = Object.keys(configuration.layers).find(function (key) {
var layer = configuration.layers[key];
return layer.useAsFilename === true && // Has the useAsFilename property
layer.hidden !== true && // Is not hidden
layer.type === 'text'; // Is of type text
});
// Get the layer that has the filename on it
var filenameLayer = configuration.layers[filenameLayerKey];
// Update the filename
if (filenameLayer) {
filename = slugify(filenameLayer.text) + '.jpg';
}
// Trigger the download
svgToImage.download(filename, {
format: 'image/jpeg',
scale: scale
});
}
}]);
return CardKitDOM;
}(CardKitRenderer);
module.exports = CardKitDOM;
/***/ },
/* 1 */
/***/ function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_1__;
/***/ },
/* 2 */
/***/ function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_2__;
/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
// Libraries
var React = __webpack_require__(1);
// RVG Elements
var _require = __webpack_require__(4);
var SVG = _require.SVG;
var Text = _require.Text;
var Rectangle = _require.Rectangle;
var Circle = _require.Circle;
var Ellipse = _require.Ellipse;
var Line = _require.Line;
var Image = _require.Image;
var Path = _require.Path;
var LinearGradient = _require.LinearGradient;
/**
* @name Card
* @class The Card React element
*/
var Card = function (_React$Component) {
_inherits(Card, _React$Component);
function Card() {
_classCallCheck(this, Card);
return _possibleConstructorReturn(this, (Card.__proto__ || Object.getPrototypeOf(Card)).apply(this, arguments));
}
_createClass(Card, [{
key: 'calculateYPosition',
/**
* Calculates the Y position of an element based on any attachments etc.
*
* @param {object} layers - The object of all layers
* @param {object} layer - The layer to calculate the Y position for
*
* @return {integer} The Y position
*/
value: function calculateYPosition(layers, layer) {
// Get the layer's currently configured Y position
var attachYLayerPosition = this.getLayerValue(layers, layer, 'y');
// If this is an object and has the attach property
if ((typeof attachYLayerPosition === 'undefined' ? 'undefined' : _typeof(attachYLayerPosition)) === 'object' && attachYLayerPosition.attach !== 'undefined') {
// Get the layer to attach to
var attachYLayer = layers[layer.y.attach];
// Calculate the Y offset
var attachYLayerHeight = 0;
switch (attachYLayer.type) {
case 'text':
var attachYLayerText = attachYLayer.text.split('\n');
if (attachYLayer.text !== '') {
attachYLayerHeight = attachYLayerText.length * (this.getLayerValue(layers, attachYLayer, 'lineHeight') || this.getLayerValue(layers, attachYLayer, 'fontSize'));
}
break;
default:
if (typeof this.getLayerValue(layers, attachYLayer, 'height') !== 'undefined') {
attachYLayerHeight = this.getLayerValue(layers, attachYLayer, 'height');
}
break;
}
// Add any additionally configured offset value
var attachYLayerOffset = layer.y.offset || 0;
// Add them together and recursively call this function if the next layer has an attachment
attachYLayerPosition = attachYLayerHeight + this.calculateYPosition(layers, attachYLayer) + attachYLayerOffset;
}
// Return the value
return attachYLayerPosition;
}
/**
* Returns the value for a given layer property
*
* @param {object} layers - The object of all layers
* @param {object} layer - The layer to get the value for
* @param {object} key - The key of the value to get from the layer
*
* @return {mixed} The value of the property on the layer
*/
}, {
key: 'getLayerValue',
value: function getLayerValue(layers, layer, key) {
if (typeof layer[key] === 'function') {
return layer[key](layers, this.refs.svg);
}
return layer[key];
}
/**
* Compute the gradient elements to render to the <defs> element
*
* @param {object} layers - The configuration object representing the layers that may require gradients
*
* @return {array} An array of React elements to render to the <defs> element
*/
}, {
key: 'computeGradients',
value: function computeGradients(layers) {
var _this2 = this;
var array = [];
var layer = void 0,
gradient = void 0;
Object.keys(layers).forEach(function (key) {
layer = layers[key];
if (_this2.getLayerValue(layers, layer, 'gradient')) {
gradient = _this2.getLayerValue(layers, layer, 'gradient');
array.push(React.createElement(LinearGradient, { key: key,
name: key,
x1: 0, x2: 0,
y1: 0, y2: 1,
stops: gradient }));
}
});
return array;
}
/**
* Compute the layers to render on the Card
*
* @param {object} layers - The configuration object representing the layers to render
*
* @return {array} An array of React elements to render on the card
*/
}, {
key: 'computeLayers',
value: function computeLayers(layers) {
var _this3 = this;
var array = [];
var layer = void 0;
// Iterate over the layers
Object.keys(layers).forEach(function (key) {
layer = layers[key];
// If the layer is hidden, ignore it
if (_this3.getLayerValue(layers, layer, 'hidden') === true) {
return;
};
// Setup an object to contain our layer data
var layerData = {};
// Iterate over the properties of the layer, and compute the value (handles getters, functions, and object implementations such as `y`)
Object.keys(layer).forEach(function (k) {
layerData[k] = _this3.getLayerValue(layers, layer, k);
});
// Make the fill value map to a gradient name, if a gradient has been configured
// See computeGradients() for the creation of gradient definitions
if (_this3.getLayerValue(layers, layer, 'gradient')) {
layerData.fill = 'url(#' + key + ')';
}
// Switch over the layer type to ensure we create the card correctly
switch (layer.type) {
case 'text':
// Split by newline
var text = layerData.text.split('\n');
array.push(React.createElement(
Text,
{ x: layerData.x,
y: _this3.calculateYPosition(layers, layerData),
fontFamily: layerData.fontFamily,
fontSize: layerData.fontSize,
fontWeight: layerData.fontWeight,
lineHeight: layerData.lineHeight,
textAnchor: layerData.textAnchor,
fill: layerData.fill,
draggable: layerData.draggable,
transform: layerData.transform,
opacity: layerData.opacity,
smartQuotes: layerData.smartQuotes,
key: key },
text
));
break;
case 'image':
array.push(React.createElement(Image, { x: layerData.x,
y: _this3.calculateYPosition(layers, layerData),
href: layerData.src,
height: layerData.height,
width: layerData.width,
draggable: layerData.draggable,
transform: layerData.transform,
opacity: layerData.opacity,
key: key }));
break;
case 'rectangle':
array.push(React.createElement(Rectangle, { x: layerData.x,
y: _this3.calculateYPosition(layers, layerData),
fill: layerData.fill,
height: layerData.height,
width: layerData.width,
draggable: layerData.draggable,
transform: layerData.transform,
key: key }));
break;
case 'circle':
array.push(React.createElement(Circle, { x: layerData.x,
y: _this3.calculateYPosition(layers, layerData),
fill: layerData.fill,
radius: layerData.radius,
draggable: layerData.draggable,
transform: layerData.transform,
key: key }));
break;
case 'ellipse':
array.push(React.createElement(Ellipse, { x: layerData.x,
y: _this3.calculateYPosition(layers, layerData),
fill: layerData.fill,
radiusX: layerData.radiusX,
radiusY: layerData.radiusY,
draggable: layerData.draggable,
transform: layerData.transform,
key: key }));
break;
case 'line':
array.push(React.createElement(Line, { x: [layerData.x1, layerData.x2],
y: [layerData.y1, layerData.y2],
stroke: layerData.stroke || layerData.fill,
draggable: layerData.draggable,
transform: layerData.transform,
key: key }));
break;
case 'path':
array.push(React.createElement(Path, { d: layerData.path || layerData.d,
fill: layerData.fill,
draggable: layerData.draggable,
transform: layerData.transform,
key: key }));
break;
}
});
return array;
}
/**
* Compute the fonts needed for the card
*
* @param {object} fonts - The fonts to use when rendering this card
*
* @return {array} An array of React elements to render in the <defs /> element of the SVG
*/
}, {
key: 'computeFonts',
value: function computeFonts() {
var fonts = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
return Object.keys(fonts).map(function (key, index) {
var src = fonts[key];
var format = 'svg';
if (_typeof(fonts[key]) === 'object') {
src = fonts[key].src;
format = fonts[key].format || 'svg';
}
return React.createElement(
'style',
{ key: index },
'@font-face {\n font-family: \'' + key + '\';\n src: url("' + src + '") format("' + format + '");\n font-weight: normal;\n font-style: normal;\n }'
);
});
}
/**
* Renders the card
*
* @return {object} JSX for the React Component
*/
}, {
key: 'render',
value: function render() {
// Grab our configuration
var _props$configuration = this.props.configuration;
var card = _props$configuration.card;
var fonts = _props$configuration.fonts;
var layers = _props$configuration.layers;
// Compute layers, gradients and fonts
var layerArray = this.computeLayers(layers);
var gradientsArray = this.computeGradients(layers);
var fontsArray = this.computeFonts(fonts);
// Return
return React.createElement(
'div',
{ className: 'card', ref: 'svg', style: { maxWidth: card.width, maxHeight: card.height } },
React.createElement(
SVG,
{ height: card.height, width: card.width, fill: card.fill },
React.createElement(
'defs',
null,
fontsArray,
gradientsArray
),
layerArray
)
);
}
}]);
return Card;
}(React.Component);
Card.propTypes = {
configuration: React.PropTypes.shape({
card: React.PropTypes.object,
fonts: React.PropTypes.object,
layers: React.PropTypes.object
})
};
// Export
module.exports = Card;
/***/ },
/* 4 */
/***/ function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_4__;
/***/ },
/* 5 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
// Dependencies
var CardKit = __webpack_require__(6);
/**
* @name CardKitRenderer
* @class
*/
var CardKitRenderer = function () {
/**
* Constructor takes in an instance of CardKit and stores it for later user
*
* @param {CardKit} cardkit - An instance of CardKit
*/
function CardKitRenderer(cardkit) {
_classCallCheck(this, CardKitRenderer);
// Ensure we recieve a CardKit object
if (!cardkit) {
throw new Error('An instance of CardKit was not provided');
}
// Validate the instance of CardKit supplied is good
if (!(cardkit instanceof CardKit) && cardkit.constructor.name !== 'CardKit') {
throw new Error('Invalid CardKit object provided');
}
this.cardkit = cardkit;
this.cardkit.addRenderer(this);
}
/**
* Re-render
*/
_createClass(CardKitRenderer, [{
key: 'rerender',
value: function rerender() {
return;
}
/**
* Compute the configuration of the supplied CardKit object
*
* @param {object} options - The options to compute the configuration with
*
* @return {object} The configuration object
*/
}, {
key: 'computeConfiguration',
value: function computeConfiguration() {
var options = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
return this.cardkit.computeConfiguration(options);
}
}]);
return CardKitRenderer;
}();
module.exports = CardKitRenderer;
/***/ },
/* 6 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var deepExtend = __webpack_require__(7);
/**
* @name CardKit
* @class Core CardKit class used for managing a single card instance
*/
var CardKit = function () {
/**
* Constructor takes in the configuration and stores it for later user
*
* @param {object} configuration - The configuration object to initialise the CardKit image with.
* @param {object} options - The additional options for use
*/
function CardKit(configuration) {
var options = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
_classCallCheck(this, CardKit);
if (!configuration) {
throw new Error('A configuration object was not provided');
}
if (!this._isValidConfiguration(configuration)) {
throw new Error('Invalid configuration object provided');
}
// Store the configuration
this.configuration = configuration;
// Configure the options
this._configureOptions(options);
// Setup an empty array of renderers
this.renderers = [];
}
/**
* Configures the supplied options on this instance of CardKit
*
* @param {object} options - The options to configure
*/
_createClass(CardKit, [{
key: '_configureOptions',
value: function _configureOptions(options) {
if (options) {
if (options.templates) {
if (!this._isValidTemplatesConfiguration(options.templates)) {
throw new Error('Invalid templates configuration object provided');
}
this.templates = options.templates;
} else {
this.templates = null;
}
if (options.themes) {
if (!this._isValidThemesConfiguration(options.themes)) {
throw new Error('Invalid themes configuration object provided');
}
this.themes = options.themes;
} else {
this.themes = null;
}
if (options.layouts) {
if (!this._isValidLayoutsConfiguration(options.layouts)) {
throw new Error('Invalid layouts configuration object provided');
}
this.layouts = options.layouts;
} else {
this.layouts = null;
}
}
}
/**
* Validates the provided configuration object
*
* @param {object} configuration - The configuration object to validate
*
* @return {boolean} Is the configuration object valid
*/
}, {
key: '_isValidConfiguration',
value: function _isValidConfiguration(configuration) {
return (typeof configuration === 'undefined' ? 'undefined' : _typeof(configuration)) === 'object' && // Should be an object
typeof configuration.card !== 'undefined' && // Should have a card property
_typeof(configuration.card) === 'object' && // Card property should be an object
typeof configuration.card.height !== 'undefined' && // Should have a height
typeof configuration.card.width !== 'undefined'; // Should have a width
}
/**
* Validates the provided templates configuration object
*
* @param {object} configuration - The templates configuration object to validate
*
* @return {boolean} Is the templates configuration object valid
*/
}, {
key: '_isValidTemplatesConfiguration',
value: function _isValidTemplatesConfiguration(templates) {
return (typeof templates === 'undefined' ? 'undefined' : _typeof(templates)) === 'object'; // Should be an object
}
/**
* Validates the provided themes configuration object
*
* @param {object} configuration - The themes configuration object to validate
*
* @return {boolean} Is the themes configuration object valid
*/
}, {
key: '_isValidThemesConfiguration',
value: function _isValidThemesConfiguration(themes) {
return (typeof themes === 'undefined' ? 'undefined' : _typeof(themes)) === 'object'; // Should be an object
}
/**
* Validates the provided layouts configuration object
*
* @param {object} configuration - The layouts configuration object to validate
*
* @return {boolean} Is the layouts configuration object valid
*/
}, {
key: '_isValidLayoutsConfiguration',
value: function _isValidLayoutsConfiguration(layouts) {
return (typeof layouts === 'undefined' ? 'undefined' : _typeof(layouts)) === 'object'; // Should be an object
}
/**
* Validates the supplied renderer
*
* @param {object} renderer - The renderer to validate
*
* @return {boolean} If the renderer is valid
*/
}, {
key: '_isValidRenderer',
value: function _isValidRenderer(renderer) {
return renderer.cardkit === this;
}
/**
* Compute the configuration
*
* @param {object} options - Any options (e.g. a specific theme and / or layout) to use when computing the configuration
*
* @return {object} The computed configuration
*/
}, {
key: 'computeConfiguration',
value: function computeConfiguration() {
var options = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0];
// Get the base configuration
var configuration = Object.assign({}, this.configuration);
// If we got options supplied
if (options) {
if (options.template && typeof this.templates[options.template] !== 'undefined') {
// Get the template based on the name and merge it onto the base configuration
configuration = deepExtend(configuration, this.templates[options.template]);
}
if (options.theme && typeof this.themes[options.theme] !== 'undefined') {
// Get the theme based on the name and merge it onto the base configuration
configuration = deepExtend(configuration, this.themes[options.theme]);
}
if (options.layout && typeof this.layouts[options.layout] !== 'undefined') {
// Get the layout based on the name and merge it onto the base configuration
configuration = deepExtend(configuration, this.layouts[options.layout]);
}
}
// Return the computed configuration
return configuration;
}
/**
* Updates the configuration, and optionally rerenders the image (if previously rendered)
*
* @param {object} configuration - The configuration object to update to
* @param {object} options - Any options to supply (templates, themes, layouts)
* @param {boolean} rerender - Whether or not to attempt to rerender the image
*/
}, {
key: 'updateConfiguration',
value: function updateConfiguration(configuration) {
var options = arguments.length <= 1 || arguments[1] === undefined ? { layouts: null, templates: null, themes: null } : arguments[1];
var rerender = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];
this.configuration = configuration;
this._configureOptions(options);
if (rerender) {
var renderers = this.getRenderers();
renderers.forEach(function (renderer) {
switch (renderer.constructor.name) {
case 'CardKitDOM':
renderer.rerender();
break;
}
});
}
}
/**
* Get the renderers
*
* @return {array} The configured renderers
*/
}, {
key: 'getRenderers',
value: function getRenderers() {
return this.renderers;
}
/**
* Add a renderer
*
* @param {object} renderer - A renderer to add
*/
}, {
key: 'addRenderer',
value: function addRenderer(renderer) {
if (!this._isValidRenderer(renderer)) {
throw new Error('Invalid renderer object provided');
}
this.renderers.push(renderer);
}
}]);
return CardKit;
}();
// Export it
module.exports = CardKit;
// Add it to the window object if we have one
if (typeof window !== 'undefined') {
window.CardKit = CardKit;
}
/***/ },
/* 7 */
/***/ function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_7__;
/***/ },
/* 8 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }