Last active
June 30, 2016 12:59
-
-
Save riddla/fa5b0b3d450576874df01118c499873c to your computer and use it in GitHub Desktop.
Templates for RequireJS module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable camelcase */ | |
/* global define */ | |
(function (define) { | |
'use strict'; | |
var _; | |
var moment; | |
define(function (require) { | |
require('common/common.module'); | |
var angular = require('angular'); | |
var faker = require('faker'); | |
_ = require('lodash'); | |
moment = require('momentDE'); | |
faker.locale = 'de'; | |
angular | |
.module('app.common') | |
.factory('dataservice', dataservice); | |
}); | |
function dataservice($http, $q, config, Occupancy, settingsService, $window, CacheFactory, logger, $state) { | |
var occupanciesCache; | |
if (!CacheFactory.get('occupanciesCache')) { | |
occupanciesCache = new CacheFactory('occupanciesCache', { | |
storageMode: 'localStorage' | |
}); | |
} | |
var apiDomain = 'http://ils-test:[email protected]/backend'; | |
var building = { | |
floorplans : null, | |
rooms : null, | |
zones : null, | |
occupancies: null | |
}; | |
return { | |
getOccupancies : getOccupancies, | |
getOccupancy : getOccupancy, | |
getOccupancyForNoticeboard: getOccupancyForNoticeboard, | |
getEvent : getEvent, | |
getEvents : getEvents, | |
addRoomDataToEvent : addRoomDataToEvent, | |
getOccupanciesForTerminal : getOccupanciesForTerminal | |
}; | |
///////////////////// | |
function cacheFilteredOccupancies(filteredOccupancies) { | |
occupanciesCache.put('/filteredOccupancies', filteredOccupancies); | |
} | |
function getTerminalData(occupancies) { | |
var terminalId = settingsService.get('terminalId'); | |
var apiUrl = apiDomain + '/api/terminals/'; | |
if (settingsService.get('mockApi')) { | |
apiUrl = $window.location.origin + '/app/common/api.mock.terminals'; | |
terminalId = '.json'; | |
} | |
if (settingsService.get('forceTerminalApiError')) { | |
apiUrl += '/FORCE_ERROR?!'; | |
//return $q.reject('forcing occupancies api error'); | |
} | |
var deferred = $q.defer(); | |
$http.get(apiUrl + terminalId) | |
.then(function (terminalResponse) { | |
deferred.resolve({ | |
terminal : terminalResponse.data, | |
occupancies: occupancies | |
}); | |
}, function (error) { | |
deferred.reject(config.errors.TERMINAL_CALL_FAILED); | |
}); | |
return deferred.promise; | |
} | |
function filterOccupanciesForTerminal(terminalAndOccupancies) { | |
var deferred = $q.defer(); | |
var occupanciesForTerminal = []; | |
angular.forEach(terminalAndOccupancies.terminal.zones, function (value) { | |
occupanciesForTerminal = _.union( | |
occupanciesForTerminal, | |
_.filter(terminalAndOccupancies.occupancies, {zone: {id: value}}) | |
); | |
}); | |
if (_.isEmpty(occupanciesForTerminal)) { | |
deferred.reject({ | |
'error': {'status': status, 'code': 4041} | |
}); | |
} else { | |
cacheFilteredOccupancies(occupanciesForTerminal); | |
deferred.resolve(occupanciesForTerminal); | |
} | |
return deferred.promise; | |
} | |
function getOccupanciesForTerminal() { | |
return getOccupancies() | |
.then(getTerminalData) | |
.then(filterOccupanciesForTerminal) | |
.catch(useCachedOccupancies); | |
//var allPromise = $q.all([ | |
// getOccupancies(), | |
// getTerminalData() | |
// //filterOccupanciesForTerminal() | |
//]).then(function (data) { | |
// debugger; | |
//}, function (error) { | |
// debugger; | |
//}); | |
// | |
//return allPromise; | |
} | |
function useCachedOccupancies(getOccupanciesForTerminalError) { | |
var cachedOccupancies = occupanciesCache.get('/filteredOccupancies'); | |
if (_.isEmpty(cachedOccupancies)) { | |
logger.error('failed using cached occupancies, no saved data found'); | |
return $state.go('error', {errorId: 666}); | |
} | |
logger.info('using cached occupancies, reason:', getOccupanciesForTerminalError); | |
return $q.when(occupanciesCache.get('/filteredOccupancies')); | |
} | |
function getOccupancies(enableCache) { | |
var deferred = $q.defer(); | |
var apiUrl = apiDomain + '/api/buildings'; | |
if (settingsService.get('mockApi')) { | |
apiUrl = $window.location.origin + '/app/common/api.mock.buildings.json'; | |
} | |
if (settingsService.get('forceOccupanciesApiError')) { | |
apiUrl += '/FORCE_ERROR?!'; | |
//return $q.reject('forcing occupancies api error'); | |
} | |
$http({ | |
method: 'get', | |
url : apiUrl, | |
cache : enableCache | |
}) | |
.then(function (data) { | |
var buildingData = _.find(data.data, {id: config.buildingId}); | |
if (!buildingData) { | |
return deferred.reject(config.errors.BUILDING_DATA_EMPTY); | |
} | |
flattenBuildingData(buildingData); | |
building.occupancies = apiResponseTransformer(building.occupancies, Occupancy); | |
if (settingsService.get('mockNotice')) { | |
building.occupancies[1].is_event = true; | |
} | |
if (settingsService.get('mockUpdatedRoom')) { | |
building.occupancies[1].updated_room = true; | |
} | |
building.events = filterEventsFromOccupancies(building.occupancies); | |
deferred.resolve(building.occupancies); | |
}, function (error) { | |
deferred.reject(config.errors.BUILDING_CALL_FAILED); | |
}); | |
return deferred.promise; | |
} | |
function getOccupancy(occupancyId) { | |
var deferred = $q.defer(); | |
occupancyId = Number(occupancyId); | |
getOccupancies() | |
.then(function (occupancies) { | |
deferred.resolve(_.find(occupancies, {id: occupancyId})); | |
}, | |
function (error) { | |
deferred.reject(error); | |
}); | |
return deferred.promise; | |
} | |
function getOccupancyForNoticeboard() { | |
var deferred = $q.defer(); | |
getOccupancies() | |
.then(function () { | |
deferred.resolve(determineEventToDisplay(building.events)); | |
}, | |
function (error) { | |
// Missing Events should not break the UI, so we just fake that we have no events | |
deferred.resolve({}); | |
}); | |
return deferred.promise; | |
} | |
function getEvent(eventId) { | |
var deferred = $q.defer(); | |
eventId = Number(eventId); | |
//_.find(mockedTerminals.events, { id: eventId }); | |
getEvents() | |
.then(function (events) { | |
deferred.resolve(_.find(events, {id: eventId})); | |
}, | |
function (error) { | |
deferred.reject(error); | |
}); | |
return deferred.promise; | |
} | |
function getEvents() { | |
if (settingsService.get('forceTerminalApiError')) { | |
return $q.reject('forcing terminal api error'); | |
} | |
var deferred = $q.defer(); | |
var apiUrl = apiDomain + '/api/terminals/' + settingsService.get('terminalId'); | |
if (settingsService.get('mockApi')) { | |
apiUrl = $window.location.origin + '/app/common/api.mock.terminals.json'; | |
} | |
$http({ | |
method: 'get', | |
url : apiUrl | |
}) | |
.then(function (data) { | |
var terminalData = data.data.events; | |
if (!terminalData) { | |
return deferred.reject({'error': {'status': status, 'code': 4041}}); | |
} | |
deferred.resolve(terminalData); | |
}); | |
return deferred.promise; | |
} | |
function addRoomDataToEvent(event) { | |
if (_.isUndefined(event.rooms)) { | |
return event; | |
} | |
_.forEach(event.rooms, function (roomId, key) { | |
var roomData = _.find(building.rooms, {id: Number(roomId)}); | |
if (roomData) { | |
event.rooms[key] = roomData; | |
} | |
else { | |
event.rooms.splice(key, 1); | |
} | |
}); | |
if (_.isEmpty(event.rooms)) { | |
delete event.rooms; | |
} | |
return event; | |
} | |
///////////////////// | |
function apiResponseTransformer(responseData, Model) { | |
if (angular.isArray(responseData)) { | |
return responseData | |
.map(Model) | |
.filter(Boolean); | |
} | |
return new Model(responseData); | |
} | |
function flattenBuildingData(buildingData) { | |
building.floorplans = extractFloorplansFromBuilding(buildingData); | |
building.zones = extractZonesFromFloorplans(building.floorplans); | |
building.rooms = extractRoomsFromZones(building.zones); | |
building.occupancies = extractOccupanciesFromRooms(building.rooms); | |
} | |
function extractFloorplansFromBuilding(buildingData) { | |
return buildingData.floorplans; | |
} | |
function extractZonesFromFloorplans(floorplans) { | |
var extractedZones = []; | |
_.forEach(floorplans, function (floorplan, key) { | |
_.forEach(floorplan.zones, function (zone, key2) { | |
zone.floorplanId = floorplan.id; | |
extractedZones.push(zone); | |
}); | |
}); | |
return extractedZones; | |
} | |
function extractRoomsFromZones(zones) { | |
var extractedRooms = []; | |
_.forEach(zones, function (zone, key) { | |
_.forEach(zone.rooms, function (room, key2) { | |
room.zoneId = zone.id; | |
extractedRooms.push(room); | |
}); | |
}); | |
return extractedRooms; | |
} | |
function extractOccupanciesFromRooms(rooms) { | |
var occupancies = []; | |
_.forEach(rooms, function (room, key) { | |
_.forEach(room.occupancies, function (occupancy, key2) { | |
occupancy.room = room; | |
delete occupancy.room.occupancies; | |
occupancy.zone = getZone(room.zoneId); | |
occupancy.floorplan = getFloorplan(occupancy.zone.floorplanId); | |
occupancies.push(occupancy); | |
}); | |
}); | |
if (settingsService.get('mockOccupancies')) { | |
var fakedOccupancies = []; | |
_.times(30, function (i) { | |
var fakedOccupancy = _.clone(occupancies[0]); | |
fakedOccupancy.floorplan = _.clone(occupancies[0].floorplan); | |
fakedOccupancy.room = _.clone(occupancies[0].room); | |
var m = moment(); | |
m = m.utc(); | |
m = m.add((10 * i), 'm'); | |
fakedOccupancy.start = m.unix(); | |
fakedOccupancy.name = fakedOccupancy.name + ' #' + i; | |
//fakedOccupancy.name += ' ' + moment().format(':mm:ss'); | |
fakedOccupancy.id = i + 10000; | |
fakedOccupancies.push(fakedOccupancy); | |
}); | |
fakedOccupancies[0].floorplan.level = '-1'; | |
fakedOccupancies[0].room.number = '-1.061'; | |
fakedOccupancies[1].floorplan.level = '0'; | |
fakedOccupancies[1].room.number = '00.064'; | |
fakedOccupancies[2].floorplan.level = '1'; | |
fakedOccupancies[2].room.number = '01.070'; | |
fakedOccupancies[3].floorplan.level = '2'; | |
fakedOccupancies[3].room.number = '02.065'; | |
occupancies = fakedOccupancies; | |
} | |
return occupancies; | |
} | |
function filterEventsFromOccupancies(occupancies) { | |
return _.filter(occupancies, {is_event: true}); | |
} | |
function determineEventToDisplay(events) { | |
return events[0]; | |
} | |
function getZone(zoneId) { | |
var zone = _.find(building.zones, {id: zoneId}); | |
delete zone.rooms; | |
return zone; | |
} | |
function getFloorplan(floorplanId) { | |
var floorplan = _.find(building.floorplans, {id: floorplanId}); | |
delete floorplan.zones; | |
return floorplan; | |
} | |
} | |
}(define)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (define) { | |
'use strict'; | |
define(function (require) { | |
var dummyDependency = require('dummyDependency'); | |
var module = require('module'); | |
var $ = require('jquery'); | |
var dummyMethod = function (dummyParameter) { | |
/// <summary>Just a dummy method that does nothing.</summary> | |
/// <param name="dummyParameter" type="String" optional="true">Just a dummy parameter.</param> | |
}; | |
var dummyPromisedMethod = function () { | |
/// <summary>Just a dummy method that returns a promise.</summary> | |
/// <returns type="Object">Promise Object for deferred execution/chaining.</returns> | |
var deferred = $.Deferred(); | |
// fake async operation | |
setTimeout(function () { | |
// return deferred.reject(); | |
return deferred.resolve(); | |
}, 2000); | |
return deferred.promise(); | |
}; | |
// config values can set as follows: | |
// requirejs.config({ | |
// config: { | |
// 'dummyModule': { | |
// dummyConfigValue: 'foo' | |
// } | |
// } | |
// }); | |
var dummyMethodUsingTheRequireJsConfig = function () { | |
return module.config().dummyConfigValue; | |
}; | |
return { | |
dummyMethod: dummyMethod, | |
dummyPromisedMethod: dummyPromisedMethod, | |
dummyMethodUsingTheRequireJsConfig: dummyMethodUsingTheRequireJsConfig | |
}; | |
}); | |
}(define)); | |
// usage: | |
// require(['dummyModule']).dummyMethod(); | |
// usage of async method: | |
// var dummyModule = require(['dummyModule']); | |
// | |
// $.when( | |
// dummyModule.dummyPromisedMethod | |
// ).done(function () { | |
// // do something | |
// }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment