-
Notifications
You must be signed in to change notification settings - Fork 122
/
across-tabs.js
1539 lines (1245 loc) · 43.1 KB
/
across-tabs.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
/*!
*
* across-tabs "1.0.0"
* https://github.com/wingify/across-tabs.js
* MIT licensed
*
* Copyright (C) 2017-2018 Wingify - A project by Varun Malhotra(https://github.com/softvar)
*
*/
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define("AcrossTabs", [], factory);
else if(typeof exports === 'object')
exports["AcrossTabs"] = factory();
else
root["AcrossTabs"] = factory();
})(this, function() {
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';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _parent = __webpack_require__(1);
var _parent2 = _interopRequireDefault(_parent);
var _child = __webpack_require__(11);
var _child2 = _interopRequireDefault(_child);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Expose Parent and Child modules on AcrossTabs Object
* @type {Object}
*/
var AcrossTabs = {
Parent: _parent2.default,
Child: _child2.default
};
exports.default = AcrossTabs;
module.exports = exports['default'];
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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; }; }();
var _tab = __webpack_require__(2);
var _tab2 = _interopRequireDefault(_tab);
var _tab3 = __webpack_require__(3);
var _tab4 = _interopRequireDefault(_tab3);
var _dom = __webpack_require__(9);
var _dom2 = _interopRequireDefault(_dom);
var _TabStatusEnum = __webpack_require__(6);
var _TabStatusEnum2 = _interopRequireDefault(_TabStatusEnum);
var _WarningTextEnum = __webpack_require__(7);
var _WarningTextEnum2 = _interopRequireDefault(_WarningTextEnum);
var _PostMessageEventNamesEnum = __webpack_require__(4);
var _PostMessageEventNamesEnum2 = _interopRequireDefault(_PostMessageEventNamesEnum);
var _postmessage = __webpack_require__(10);
var _postmessage2 = _interopRequireDefault(_postmessage);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var heartBeat = void 0,
tab = void 0;
// Named Class expression
var Parent = function () {
/**
* Involed when object is instantiated
* Set flags/variables and calls init method to attach event listeners
* @param {Object} config - Refer API/docs for config keys
*/
function Parent(config) {
_classCallCheck(this, Parent);
config = config || {};
if (typeof config.heartBeatInterval === 'undefined') {
config.heartBeatInterval = 500;
}
if (typeof config.shouldInitImmediately === 'undefined') {
config.shouldInitImmediately = true;
}
// reset tabs with every new Object
_tab4.default.tabs = [];
this.Tab = _tab2.default;
_extends(this, config);
_tab4.default.config = config;
if (this.shouldInitImmediately) {
this.init();
}
}
_createClass(Parent, [{
key: 'addInterval',
value: function addInterval() {
var i = void 0,
tabs = _tab4.default.getAll(),
openedTabs = _tab4.default.getOpened();
// don't poll if all tabs are in CLOSED states
if (!openedTabs || !openedTabs.length) {
window.clearInterval(heartBeat); // stop the interval
heartBeat = null;
return false;
}
for (i = 0; i < tabs.length; i++) {
if (this.removeClosedTabs) {
this.watchStatus(tabs[i]);
}
/**
* The check is required since tab would be removed when closed(in case of `removeClosedTabs` flag),
* irrespective of heatbeat controller
*/
if (tabs[i]) {
tabs[i].status = tabs[i].ref.closed ? _TabStatusEnum2.default.CLOSE : _TabStatusEnum2.default.OPEN;
}
}
// Call the user-defined callback after every polling operation is operted in a single run
if (this.onPollingCallback) {
this.onPollingCallback();
}
}
}, {
key: 'startPollingTabs',
/**
* Poll all tabs for their status - OPENED / CLOSED
* An interval is created which checks for last and current status.
* There's a property on window i.e. `closed` which returns true for the closed window.
* And one can see `true` only in another tab when the tab was opened by the same `another` tab.
*/
value: function startPollingTabs() {
var _this = this;
heartBeat = window.setInterval(function () {
return _this.addInterval();
}, this.heartBeatInterval);
}
}, {
key: 'watchStatus',
/**
* Compare tab status - OPEN vs CLOSE
* @param {Object} tab
*/
value: function watchStatus(tab) {
if (!tab) {
return false;
}
var newStatus = tab.ref.closed ? _TabStatusEnum2.default.CLOSE : _TabStatusEnum2.default.OPEN,
oldStatus = tab.status;
// If last and current status(inside a polling interval) are same, don't do anything
if (!newStatus || newStatus === oldStatus) {
return false;
}
// OPEN to CLOSE state
if (oldStatus === _TabStatusEnum2.default.OPEN && newStatus === _TabStatusEnum2.default.CLOSE) {
// remove tab from tabUtils
_tab4.default._remove(tab);
}
// Change from CLOSE to OPEN state is never gonna happen ;)
}
}, {
key: 'onChildUnload',
/**
* Called when a child is refreshed/closed
* @param {Object} ev - Event
*/
value: function onChildUnload(ev) {
if (this.onChildDisconnect) {
this.onChildDisconnect(ev.detail);
}
}
/**
* Enable link/btn, which got disabled on clicking.
* Note: works only when `data-tab-opener="heatmap"` is used on the respective element
* @param {Object} ev - Event
*/
}, {
key: 'customEventUnListener',
value: function customEventUnListener(ev) {
this.enableElements();
if (this.onHandshakeCallback) {
this.onHandshakeCallback(ev.detail);
}
}
}, {
key: 'addEventListeners',
/**
* Attach postmessage, native and custom listeners to the window
*/
value: function addEventListeners() {
var _this2 = this;
window.removeEventListener('message', _postmessage2.default.onNewTab);
window.addEventListener('message', _postmessage2.default.onNewTab);
window.removeEventListener('toggleElementDisabledAttribute', this.customEventUnListener);
window.addEventListener('toggleElementDisabledAttribute', function (ev) {
return _this2.customEventUnListener(ev);
});
window.removeEventListener('onChildUnload', this.onChildUnload);
window.addEventListener('onChildUnload', function (ev) {
return _this2.onChildUnload(ev);
});
// Let children tabs know when Parent is closed / refereshed.
window.onbeforeunload = function () {
_tab4.default.broadCastAll(_PostMessageEventNamesEnum2.default.PARENT_DISCONNECTED);
};
}
}, {
key: 'enableElements',
/**
* API methods exposed for Public
*
* Re-enable the link/btn which got disabled on clicking
*/
value: function enableElements() {
_dom2.default.enable('data-tab-opener');
}
}, {
key: 'getAllTabs',
/**
* Return list of all tabs
* @return {Array}
*/
value: function getAllTabs() {
return _tab4.default.getAll();
}
}, {
key: 'getOpenedTabs',
/**
* Return list of all OPENED tabs
* @return {Array}
*/
value: function getOpenedTabs() {
return _tab4.default.getOpened();
}
}, {
key: 'getClosedTabs',
/**
* Return list of all CLOSED tabs
* @return {Array}
*/
value: function getClosedTabs() {
return _tab4.default.getClosed();
}
/**
* Close all tabs at once
* @return {Object}
*/
}, {
key: 'closeAllTabs',
value: function closeAllTabs() {
return _tab4.default.closeAll();
}
}, {
key: 'closeTab',
/**
* Close a specific tab
* @return {Object}
*/
value: function closeTab(id) {
return _tab4.default.closeTab(id);
}
}, {
key: 'broadCastAll',
/**
* Send a postmessage to all OPENED tabs
* @return {Object}
*/
value: function broadCastAll(msg) {
return _tab4.default.broadCastAll(msg);
}
/**
* Send a postmessage to a specific tab
* @return {Object}
*/
}, {
key: 'broadCastTo',
value: function broadCastTo(id, msg) {
return _tab4.default.broadCastTo(id, msg);
}
/**
* Open a new tab. Config has to be passed with some required keys
* @return {Object} tab
*/
}, {
key: 'openNewTab',
value: function openNewTab(config) {
if (!config) {
throw new Error(_WarningTextEnum2.default.CONFIG_REQUIRED);
}
var url = config.url;
if (!url) {
throw new Error(_WarningTextEnum2.default.URL_REQUIRED);
}
tab = new this.Tab();
tab.create(config);
// If polling is already there, don't set it again
if (!heartBeat) {
this.startPollingTabs();
}
return tab;
}
}, {
key: 'init',
/**
* API methods exposed for Public ends here
**/
/**
* Invoked on object instantiation unless user pass a key to call it explicitly
*/
value: function init() {
this.addEventListeners();
}
}]);
return Parent;
}();
;
exports.default = Parent;
module.exports = exports['default'];
/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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; }; }();
var _tab = __webpack_require__(3);
var _tab2 = _interopRequireDefault(_tab);
var _uuid = __webpack_require__(8);
var _uuid2 = _interopRequireDefault(_uuid);
var _dom = __webpack_require__(9);
var _dom2 = _interopRequireDefault(_dom);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
// Named Class expression
var Tab = function () {
/**
* Invoked when the object is instantiated
*/
function Tab() {
_classCallCheck(this, Tab);
// Set name of Parent tab if not already defined
window.name = window.name || 'PARENT_TAB';
}
_createClass(Tab, [{
key: 'create',
/**
* Open a new tab
* @param {Object} config - Refer API for config keys
* @return {Object} this
*/
value: function create(config) {
config = config || {};
_extends(this, config);
this.id = _uuid2.default.generate() || _tab2.default.tabs.length + 1;
this.status = 'open';
// Refere https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Window_features for WindowFeatures
this.ref = window.open(this.url, '_blank', config.windowFeatures);
_dom2.default.disable('data-tab-opener');
window.newlyTabOpened = {
id: this.id,
name: this.name,
ref: this.ref
};
// Push it to the list of tabs
_tab2.default.addNew(this);
// Return reference for chaining purpose
return this;
}
}]);
return Tab;
}();
;
exports.default = Tab;
module.exports = exports['default'];
/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _PostMessageEventNamesEnum = __webpack_require__(4);
var _PostMessageEventNamesEnum2 = _interopRequireDefault(_PostMessageEventNamesEnum);
var _array = __webpack_require__(5);
var _array2 = _interopRequireDefault(_array);
var _TabStatusEnum = __webpack_require__(6);
var _TabStatusEnum2 = _interopRequireDefault(_TabStatusEnum);
var _WarningTextEnum = __webpack_require__(7);
var _WarningTextEnum2 = _interopRequireDefault(_WarningTextEnum);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* A Tab utility file to deal with tab operations
*/
var tabUtils = {
tabs: [],
config: {}
};
/**
* Remove a tab from a list of all tabs.
* This is required when users opts for removing the closed tabs from the list of tabs.
* This can be done explictly by passing `removeClosedTabs` key while instantiating Parent.
* @param {Object} tab
*/
tabUtils._remove = function (tab) {
var index = void 0;
index = _array2.default.searchByKeyName(tabUtils.tabs, 'id', tab.id, 'index');
tabUtils.tabs.splice(index, 1);
};
/**
* As explained in `event-listeners/postmessage.js` file,
* the data received from postmessage API is further processed based on our convention
* @param {String} msg
* @return {String} modified msg
*/
tabUtils._preProcessMessage = function (msg) {
// make msg always an object to support JSON support
try {
msg = JSON.stringify(msg);
} catch (e) {
throw new Error(_WarningTextEnum2.default.INVALID_JSON);
}
if (msg.indexOf(_PostMessageEventNamesEnum2.default.PARENT_COMMUNICATED) === -1) {
msg = _PostMessageEventNamesEnum2.default.PARENT_COMMUNICATED + msg;
}
return msg;
};
/**
* Add a new tab to the Array of tabs
* @param {Object} tab
* @return {Object} - this
*/
tabUtils.addNew = function (tab) {
tabUtils.tabs.push(tab);
return undefined;
};
/**
* Filter out all the opened tabs
* @return {Array} - only the opened tabs
*/
tabUtils.getOpened = function () {
return tabUtils.tabs.filter(function (tab) {
return tab.status === _TabStatusEnum2.default.OPEN;
});
};
/**
* Filter out all the closed tabs
* @return {Array} - only the closed tabs
*/
tabUtils.getClosed = function () {
return tabUtils.tabs.filter(function (tab) {
return tab.status === _TabStatusEnum2.default.CLOSE;
});
};
/**
* To get list of all tabs(closed/opened).
* Note: Closed tabs will not be returned if `removeClosedTabs` key is paased while instantiaiting Parent.
* @return {Array} - list of all tabs
*/
tabUtils.getAll = function () {
return tabUtils.tabs;
};
/**
* Close a specific tab
* @param {String} id
* @return {Object} this
*/
tabUtils.closeTab = function (id) {
var tab = _array2.default.searchByKeyName(tabUtils.tabs, 'id', id);
if (tab && tab.ref) {
tab.ref.close();
tab.status = _TabStatusEnum2.default.CLOSE;
}
return tabUtils;
// --tabUtils.tabs.length;
};
/**
* Close all opened tabs using a native method `close` available on window.open reference.
* @return {tabUtils} this
*/
tabUtils.closeAll = function () {
var i = void 0;
for (i = 0; i < tabUtils.tabs.length; i++) {
if (tabUtils.tabs[i] && tabUtils.tabs[i].ref) {
tabUtils.tabs[i].ref.close();
tabUtils.tabs[i].status = _TabStatusEnum2.default.CLOSE;
}
}
return tabUtils;
};
/**
* Send a postmessage to every opened Child tab(excluding itself i.e Parent Tab)
* @param {String} msg
* @param {Boolean} isSiteInsideFrame
*/
tabUtils.broadCastAll = function (msg, isSiteInsideFrame) {
var i = void 0,
tabs = tabUtils.getOpened();
msg = tabUtils._preProcessMessage(msg);
for (i = 0; i < tabs.length; i++) {
tabUtils.sendMessage(tabs[i], msg, isSiteInsideFrame);
}
return tabUtils;
};
/**
* Send a postmessage to a specific Child tab
* @param {String} id
* @param {String} msg
* @param {Boolean} isSiteInsideFrame
*/
tabUtils.broadCastTo = function (id, msg, isSiteInsideFrame) {
var targetedTab = void 0,
tabs = tabUtils.getAll();
msg = tabUtils._preProcessMessage(msg);
targetedTab = _array2.default.searchByKeyName(tabs, 'id', id); // TODO: tab.id
tabUtils.sendMessage(targetedTab, msg, isSiteInsideFrame);
return tabUtils;
};
/**
* Send a postMessage to the desired window/frame
* @param {Object} target
* @param {String} msg
* @param {Boolean} isSiteInsideFrame
*/
tabUtils.sendMessage = function (target, msg, isSiteInsideFrame) {
var origin = tabUtils.config.origin || '*';
if (isSiteInsideFrame) {
target.ref[0].postMessage(msg, origin);
} else {
target.ref.top.postMessage(msg, origin);
}
};
exports.default = tabUtils;
module.exports = exports['default'];
/***/ },
/* 4 */
/***/ function(module, exports) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
/**
* Enum for different event names used for tab-communication
* @type {Object}
*/
var PostMessageEventNamesEnum = {
LOADED: '__TAB__LOADED_EVENT__',
CUSTOM: '__TAB__CUSTOM_EVENT__',
ON_BEFORE_UNLOAD: '__TAB__ON_BEFORE_UNLOAD__',
PARENT_DISCONNECTED: '__PARENT_DISCONNECTED__',
HANDSHAKE_WITH_PARENT: '__HANDSHAKE_WITH_PARENT__',
PARENT_COMMUNICATED: '__PARENT_COMMUNICATED__'
};
exports.default = PostMessageEventNamesEnum;
module.exports = exports['default'];
/***/ },
/* 5 */
/***/ function(module, exports) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var arrayUtils = {};
/**
* Different type of data needed after searching an item(Object) within data(Array of Objects).
* 1. `INDEX` returns just the index at which the item was present
* 2. `OBJECT` returns the matched object
* 3. `BOTH` returns an object { obj: matched_object, index: index_found }
*/
var returnPreferenceEnum = {
INDEX: 'index',
OBJECT: 'object',
BOTH: 'both'
};
/**
* Search for an item(Object) within a data-set(Array Of Objects)
* @param {Array of Objects} data
* @param {String} key - Unique key to search on the basis of
* @param {String} value - The matching criteria
* @param {String} returnPreference - what kind of output is needed
* @return {Object}
*/
arrayUtils.searchByKeyName = function (data, key, value, returnPreference) {
if (!data || !key) {
return false;
}
returnPreference = returnPreference || returnPreferenceEnum[1]; // default to Object
var i = void 0,
obj = void 0,
returnData = void 0,
index = -1;
for (i = 0; i < data.length; i++) {
obj = data[i];
// Number matching support
if (!isNaN(value) && parseInt(obj[key], 10) === parseInt(value, 10)) {
index = i;
break;
} else if (isNaN(value) && obj[key] === value) {
// String exact matching support
index = i;
break;
}
}
if (index === -1) {
// item not found
data[index] = {}; // for consistency
}
switch (returnPreference) {
case returnPreferenceEnum.INDEX:
returnData = index;
break;
case returnPreferenceEnum.BOTH:
returnData = {
obj: data[index],
index: index
};
break;
case returnPreferenceEnum.OBJECT:
default:
returnData = data[index];
break;
}
return returnData;
};
exports.default = arrayUtils;
module.exports = exports['default'];
/***/ },
/* 6 */
/***/ function(module, exports) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
/**
* Enum for Tab status(still opened / closed) used for tab-communication
* @type {Object}
*/
var TabStatusEnum = {
OPEN: 'open',
CLOSE: 'close'
};
exports.default = TabStatusEnum;
module.exports = exports['default'];
/***/ },
/* 7 */
/***/ function(module, exports) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
/**
* Enum for showing various warnings to suser when things done wrong
* @type {Object}
*/
var WarningTextEnum = {
INVALID_JSON: 'Invalid JSON Object!',
INVALID_DATA: 'Some wrong message is being sent by Parent.',
CONFIG_REQUIRED: 'Configuration options required. Please read docs.',
URL_REQUIRED: 'Url is needed for creating and opening a new window/tab. Please read docs.'
};
exports.default = WarningTextEnum;
module.exports = exports['default'];
/***/ },
/* 8 */
/***/ function(module, exports) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
/**
* UUID.js: The RFC-compliant UUID generator for JavaScript.
* ES6 port of only `generate` method of UUID by Varun Malhotra under MIT License
*
* @author LiosK
* @version v3.3.0
* @license The MIT License: Copyright (c) 2010-2016 LiosK.
*/
/** @constructor */
var UUID = void 0;
UUID = function () {
'use strict';
/** @lends UUID */
function UUID() {}
/**
* The simplest function to get an UUID string.
* @returns {string} A version 4 UUID string.
*/
UUID.generate = function () {
var rand = UUID._getRandomInt,
hex = UUID._hexAligner;
// ["timeLow", "timeMid", "timeHiAndVersion", "clockSeqHiAndReserved", "clockSeqLow", "node"]
return hex(rand(32), 8) + // time_low
'-' + hex(rand(16), 4) + // time_mid
'-' + hex(0x4000 | rand(12), 4) + // time_hi_and_version
'-' + hex(0x8000 | rand(14), 4) + // clock_seq_hi_and_reserved clock_seq_low
'-' + hex(rand(48), 12); // node
};
/**
* Returns an unsigned x-bit random integer.
* @param {int} x A positive integer ranging from 0 to 53, inclusive.
* @returns {int} An unsigned x-bit random integer (0 <= f(x) < 2^x).
*/
UUID._getRandomInt = function (x) {
if (x < 0) {
return NaN;
}
if (x <= 30) {
return 0 | Math.random() * (1 << x);
}
if (x <= 53) {
return (0 | Math.random() * (1 << 30)) + (0 | Math.random() * (1 << x - 30)) * (1 << 30);
}
return NaN;
};
/**
* Returns a function that converts an integer to a zero-filled string.
* @param {int} radix
* @returns {function(num, length)}
*/
UUID._getIntAligner = function (radix) {
return function (num, length) {
var str = num.toString(radix),
i = length - str.length,
z = '0';
for (; i > 0; i >>>= 1, z += z) {
if (i & 1) {
str = z + str;
}
}
return str;
};
};
UUID._hexAligner = UUID._getIntAligner(16);
/**
* Returns UUID string representation.
* @returns {string} {@link UUID#hexString}.
*/
UUID.prototype.toString = function () {
return this.hexString;
};
return UUID;
}(UUID);
exports.default = UUID;
module.exports = exports['default'];
/***/ },
/* 9 */
/***/ function(module, exports) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
/**
* This utility helps enabling/disabling the Link/Button on the Parent Tab.
* As soon as, user clicks on link/btn to open a new tab, the link/btn gets disabled.
* Once child communicates for the first time with the Parent, the link/btn is re-enabled to open up new tab.
* This feature is toggleable and can be used explicitly putting a data attribute on the link/btn.
*
* <a href="/demo.html" data-tab-opener="parent" target="_blank" on-click="parent.openNewTab(config)">Open Tab</a>
*/
var domUtils = {
disable: function disable(selector) {
if (!selector) {
return false;
}
var i = void 0,
ATOpenerElems = document.querySelectorAll('[' + selector + ']');
for (i = 0; i < ATOpenerElems.length; i++) {
ATOpenerElems[i].setAttribute('disabled', 'disabled');