canvasframework 0.4.3 → 0.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,246 @@
1
+ /**
2
+ * FirebaseCore - Classe de base pour initialiser Firebase
3
+ *
4
+ * @example
5
+ * const firebase = new FirebaseCore({
6
+ * apiKey: "YOUR_API_KEY",
7
+ * authDomain: "YOUR_PROJECT.firebaseapp.com",
8
+ * projectId: "YOUR_PROJECT_ID",
9
+ * storageBucket: "YOUR_PROJECT.appspot.com",
10
+ * messagingSenderId: "YOUR_SENDER_ID",
11
+ * appId: "YOUR_APP_ID"
12
+ * });
13
+ * await firebase.initialize();
14
+ */
15
+ class FirebaseCore {
16
+ constructor(config) {
17
+ this.config = config;
18
+ this.app = null;
19
+ this.isInitialized = false;
20
+
21
+ // Options
22
+ this.enablePersistence = config.enablePersistence !== false;
23
+ this.enableAnalytics = config.enableAnalytics !== false;
24
+
25
+ // Références aux services
26
+ this.auth = null;
27
+ this.db = null;
28
+ this.storage = null;
29
+ this.functions = null;
30
+ this.messaging = null;
31
+ this.analytics = null;
32
+ }
33
+
34
+ /**
35
+ * Initialiser Firebase
36
+ */
37
+ async initialize() {
38
+ if (this.isInitialized) {
39
+ return this.app;
40
+ }
41
+
42
+ try {
43
+ // Charger les scripts Firebase
44
+ await this.loadFirebaseScripts();
45
+
46
+ // Initialiser l'app Firebase
47
+ this.app = firebase.initializeApp(this.config);
48
+
49
+ // Initialiser Analytics si activé
50
+ if (this.enableAnalytics && firebase.analytics) {
51
+ this.analytics = firebase.analytics();
52
+ }
53
+
54
+ this.isInitialized = true;
55
+
56
+ return this.app;
57
+
58
+ } catch (error) {
59
+ console.error('❌ Erreur initialisation Firebase:', error);
60
+ throw error;
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Charger les scripts Firebase depuis le CDN
66
+ */
67
+ async loadFirebaseScripts() {
68
+ const version = '9.23.0'; // Version Firebase
69
+
70
+ const scripts = [
71
+ `https://www.gstatic.com/firebasejs/${version}/firebase-app-compat.js`,
72
+ `https://www.gstatic.com/firebasejs/${version}/firebase-auth-compat.js`,
73
+ `https://www.gstatic.com/firebasejs/${version}/firebase-database-compat.js`,
74
+ `https://www.gstatic.com/firebasejs/${version}/firebase-firestore-compat.js`,
75
+ `https://www.gstatic.com/firebasejs/${version}/firebase-storage-compat.js`,
76
+ `https://www.gstatic.com/firebasejs/${version}/firebase-functions-compat.js`,
77
+ `https://www.gstatic.com/firebasejs/${version}/firebase-messaging-compat.js`,
78
+ `https://www.gstatic.com/firebasejs/${version}/firebase-analytics-compat.js`
79
+ ];
80
+
81
+ // Charger tous les scripts en parallèle
82
+ await Promise.all(scripts.map(src => this.loadScript(src)));
83
+ }
84
+
85
+ /**
86
+ * Charger un script
87
+ */
88
+ loadScript(src) {
89
+ return new Promise((resolve, reject) => {
90
+ // Vérifier si le script est déjà chargé
91
+ if (document.querySelector(`script[src="${src}"]`)) {
92
+ resolve();
93
+ return;
94
+ }
95
+
96
+ const script = document.createElement('script');
97
+ script.src = src;
98
+ script.async = true;
99
+ script.onload = resolve;
100
+ script.onerror = () => reject(new Error(`Échec du chargement: ${src}`));
101
+ document.head.appendChild(script);
102
+ });
103
+ }
104
+
105
+ /**
106
+ * Obtenir l'instance Firebase
107
+ */
108
+ getApp() {
109
+ if (!this.isInitialized) {
110
+ throw new Error('Firebase non initialisé. Appelez initialize() d\'abord.');
111
+ }
112
+ return this.app;
113
+ }
114
+
115
+ /**
116
+ * Obtenir un service Firebase
117
+ */
118
+ getAuth() {
119
+ if (!this.auth) {
120
+ this.auth = firebase.auth();
121
+ }
122
+ return this.auth;
123
+ }
124
+
125
+ getDatabase() {
126
+ if (!this.db) {
127
+ this.db = firebase.database();
128
+ }
129
+ return this.db;
130
+ }
131
+
132
+ getFirestore() {
133
+ if (!this.firestore) {
134
+ this.firestore = firebase.firestore();
135
+
136
+ // Activer la persistance si demandé
137
+ if (this.enablePersistence) {
138
+ this.firestore.enablePersistence({ synchronizeTabs: true })
139
+ .catch(err => {
140
+ if (err.code === 'failed-precondition') {
141
+ console.warn('⚠️ Persistance: plusieurs onglets ouverts');
142
+ } else if (err.code === 'unimplemented') {
143
+ console.warn('⚠️ Persistance non supportée par ce navigateur');
144
+ }
145
+ });
146
+ }
147
+ }
148
+ return this.firestore;
149
+ }
150
+
151
+ getStorage() {
152
+ if (!this.storage) {
153
+ this.storage = firebase.storage();
154
+ }
155
+ return this.storage;
156
+ }
157
+
158
+ getFunctions(region = 'us-central1') {
159
+ if (!this.functions) {
160
+ this.functions = firebase.functions(region);
161
+ }
162
+ return this.functions;
163
+ }
164
+
165
+ getMessaging() {
166
+ if (!this.messaging) {
167
+ this.messaging = firebase.messaging();
168
+ }
169
+ return this.messaging;
170
+ }
171
+
172
+ getAnalytics() {
173
+ if (!this.analytics) {
174
+ this.analytics = firebase.analytics();
175
+ }
176
+ return this.analytics;
177
+ }
178
+
179
+ /**
180
+ * Obtenir le timestamp serveur
181
+ */
182
+ getServerTimestamp() {
183
+ return firebase.database.ServerValue.TIMESTAMP;
184
+ }
185
+
186
+ /**
187
+ * Obtenir le timestamp Firestore serveur
188
+ */
189
+ getFirestoreServerTimestamp() {
190
+ return firebase.firestore.FieldValue.serverTimestamp();
191
+ }
192
+
193
+ /**
194
+ * Logger un événement Analytics
195
+ */
196
+ logEvent(eventName, eventParams = {}) {
197
+ if (this.analytics) {
198
+ this.analytics.logEvent(eventName, eventParams);
199
+ }
200
+ }
201
+
202
+ /**
203
+ * Définir l'utilisateur pour Analytics
204
+ */
205
+ setUserId(userId) {
206
+ if (this.analytics) {
207
+ this.analytics.setUserId(userId);
208
+ }
209
+ }
210
+
211
+ /**
212
+ * Définir une propriété utilisateur
213
+ */
214
+ setUserProperty(name, value) {
215
+ if (this.analytics) {
216
+ this.analytics.setUserProperties({ [name]: value });
217
+ }
218
+ }
219
+
220
+ /**
221
+ * Vérifier si Firebase est initialisé
222
+ */
223
+ isReady() {
224
+ return this.isInitialized;
225
+ }
226
+
227
+ /**
228
+ * Nettoyer les ressources
229
+ */
230
+ destroy() {
231
+ if (this.app) {
232
+ this.app.delete();
233
+ this.app = null;
234
+ }
235
+
236
+ this.auth = null;
237
+ this.db = null;
238
+ this.storage = null;
239
+ this.functions = null;
240
+ this.messaging = null;
241
+ this.analytics = null;
242
+ this.isInitialized = false;
243
+ }
244
+ }
245
+
246
+ export default FirebaseCore;