Subversion Repositories SmartDukaan

Rev

Rev 35594 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 35594 Rev 35626
Line 1284... Line 1284...
1284
    for (let cookie of cookies) {
1284
    for (let cookie of cookies) {
1285
        const [key, value] = cookie.split('=');
1285
        const [key, value] = cookie.split('=');
1286
        if (key === name) return decodeURIComponent(value);
1286
        if (key === name) return decodeURIComponent(value);
1287
    }
1287
    }
1288
    return null;
1288
    return null;
-
 
1289
}
-
 
1290
 
-
 
1291
// ============================================================
-
 
1292
// PROMISE-BASED AJAX UTILITIES (SPA Support)
-
 
1293
// ============================================================
-
 
1294
 
-
 
1295
function doGetAjaxPromise(url, params) {
-
 
1296
    return $.ajax({
-
 
1297
        url: url,
-
 
1298
        method: 'GET',
-
 
1299
        data: params,
-
 
1300
        dataType: 'json',
-
 
1301
        cache: false
-
 
1302
    });
-
 
1303
}
-
 
1304
 
-
 
1305
function doPostAjaxJsonPromise(url, data) {
-
 
1306
    return $.ajax({
-
 
1307
        url: url,
-
 
1308
        method: 'POST',
-
 
1309
        data: JSON.stringify(data),
-
 
1310
        contentType: 'application/json',
-
 
1311
        dataType: 'json',
-
 
1312
        cache: false
-
 
1313
    });
-
 
1314
}
-
 
1315
 
-
 
1316
function doPostAjaxParamsPromise(url, params) {
-
 
1317
    return $.ajax({
-
 
1318
        url: url,
-
 
1319
        method: 'POST',
-
 
1320
        data: params,
-
 
1321
        dataType: 'json',
-
 
1322
        cache: false
-
 
1323
    });
-
 
1324
}
-
 
1325
 
-
 
1326
// ============================================================
-
 
1327
// SPA UTILITIES
-
 
1328
// ============================================================
-
 
1329
 
-
 
1330
function createSpaModule(config) {
-
 
1331
    var defaultConfig = {
-
 
1332
        name: 'SpaModule',
-
 
1333
        endpoints: {},
-
 
1334
        state: {},
-
 
1335
        onInit: function() {},
-
 
1336
        onDestroy: function() {}
-
 
1337
    };
-
 
1338
 
-
 
1339
    var moduleConfig = $.extend(true, {}, defaultConfig, config);
-
 
1340
    var state = $.extend(true, {}, moduleConfig.state);
-
 
1341
    var timers = {};
-
 
1342
 
-
 
1343
    return {
-
 
1344
        getName: function() { return moduleConfig.name; },
-
 
1345
        getState: function(key) { return key ? state[key] : state; },
-
 
1346
        setState: function(key, value) {
-
 
1347
            state[key] = value;
-
 
1348
            return this;
-
 
1349
        },
-
 
1350
        getEndpoint: function(name) {
-
 
1351
            return context + moduleConfig.endpoints[name];
-
 
1352
        },
-
 
1353
        setTimer: function(name, callback, delay) {
-
 
1354
            this.clearTimer(name);
-
 
1355
            timers[name] = setTimeout(callback, delay);
-
 
1356
            return timers[name];
-
 
1357
        },
-
 
1358
        clearTimer: function(name) {
-
 
1359
            if (timers[name]) {
-
 
1360
                clearTimeout(timers[name]);
-
 
1361
                delete timers[name];
-
 
1362
            }
-
 
1363
            return this;
-
 
1364
        },
-
 
1365
        clearAllTimers: function() {
-
 
1366
            var self = this;
-
 
1367
            Object.keys(timers).forEach(function(name) { self.clearTimer(name); });
-
 
1368
            return this;
-
 
1369
        },
-
 
1370
        init: function(options) {
-
 
1371
            moduleConfig.onInit.call(this, options);
-
 
1372
            return this;
-
 
1373
        },
-
 
1374
        destroy: function() {
-
 
1375
            this.clearAllTimers();
-
 
1376
            state = $.extend(true, {}, moduleConfig.state);
-
 
1377
            moduleConfig.onDestroy.call(this);
-
 
1378
            return this;
-
 
1379
        }
-
 
1380
    };
-
 
1381
}
-
 
1382
 
-
 
1383
function handleApiResponse(response, successCallback, errorCallback) {
-
 
1384
    if (response && response.success) {
-
 
1385
        if (typeof successCallback === 'function') {
-
 
1386
            successCallback(response.data);
-
 
1387
        }
-
 
1388
    } else {
-
 
1389
        var message = response && response.message ? response.message : 'An error occurred';
-
 
1390
        if (typeof errorCallback === 'function') {
-
 
1391
            errorCallback(message, response);
-
 
1392
        } else {
-
 
1393
            bootbox.alert(message);
-
 
1394
        }
-
 
1395
    }
-
 
1396
}
-
 
1397
 
-
 
1398
function resolveUrlTemplate(template, params) {
-
 
1399
    var url = template;
-
 
1400
    for (var key in params) {
-
 
1401
        if (params.hasOwnProperty(key)) {
-
 
1402
            url = url.replace('{' + key + '}', encodeURIComponent(params[key]));
-
 
1403
        }
-
 
1404
    }
-
 
1405
    return url;
1289
}
1406
}
1290
1407