Skip to content

Commit

Permalink
Addressed how modules were defined
Browse files Browse the repository at this point in the history
Fixes #5497
  • Loading branch information
BigFunger authored and Jim Unger committed Dec 18, 2015
1 parent efa6210 commit a4b831c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 83 deletions.
6 changes: 3 additions & 3 deletions src/server/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function (kbnServer, server, config) {

server = kbnServer.server = new Hapi.Server();

const urlLookup = require('./urlLookup')(server);
const shortUrlLookup = require('./short_url_lookup')(server);

// Create a new connection
var connectionOptions = {
Expand Down Expand Up @@ -160,7 +160,7 @@ module.exports = function (kbnServer, server, config) {
method: 'GET',
path: '/goto/{urlId}',
handler: async function (request, reply) {
const url = await urlLookup.getUrl(request.params.urlId);
const url = await shortUrlLookup.getUrl(request.params.urlId);
reply().redirect(url);
}
});
Expand All @@ -169,7 +169,7 @@ module.exports = function (kbnServer, server, config) {
method: 'POST',
path: '/shorten',
handler: async function (request, reply) {
const urlId = await urlLookup.generateUrlId(request.payload.url);
const urlId = await shortUrlLookup.generateUrlId(request.payload.url);
reply(urlId);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export default function (server) {
id: urlId,
body: {
doc: {
'access-date': new Date(),
'access-count': urlDoc._source['access-count'] + 1
'accessDate': new Date(),
'accessCount': urlDoc._source.accessCount + 1
}
}
});
Expand All @@ -35,7 +35,7 @@ export default function (server) {
resolve(response);
})
.catch(err => {
resolve();
reject();
});
});

Expand All @@ -52,9 +52,9 @@ export default function (server) {
id: urlId,
body: {
url,
'access-count': 0,
'create-date': new Date(),
'access-date': new Date()
'accessCount': 0,
'createDate': new Date(),
'accessDate': new Date()
}
})
.then(response => {
Expand Down
106 changes: 52 additions & 54 deletions src/ui/public/share/index.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,55 @@
define(function (require) {
const app = require('ui/modules').get('kibana');

app.directive('share', function (Private) {
const urlShortener = Private(require('./url_shortener'));

return {
restrict: 'E',
scope: { objectType: '@' },
template: require('ui/share/index.html'),
controller: function ($scope, $rootScope, $location, $http) {
$scope.shortUrlsLoading = false;

$scope.$watch('getUrl()', function (url) {
$scope.url = url;
$scope.embedUrl = url.replace('?', '?embed&');

//when the url changes we want to hide any generated short urls
$scope.shortenUrls = false;
$scope.shortUrl = undefined;
});

$scope.$watch('shortenUrls', enabled => {
if (!!enabled) {
if ($scope.shortUrl) {
const app = require('ui/modules').get('kibana');

app.directive('share', function (Private) {
const urlShortener = Private(require('./url_shortener'));

return {
restrict: 'E',
scope: { objectType: '@' },
template: require('ui/share/index.html'),
controller: function ($scope, $rootScope, $location, $http) {
$scope.shortUrlsLoading = false;

$scope.$watch('getUrl()', function (url) {
$scope.url = url;
$scope.embedUrl = url.replace('?', '?embed&');

//when the url changes we want to hide any generated short urls
$scope.shortenUrls = false;
$scope.shortUrl = undefined;
});

$scope.$watch('shortenUrls', enabled => {
if (!!enabled) {
if ($scope.shortUrl) {
$scope.shortUrlsLoading = false;
} else {
$scope.shortUrlsLoading = true;

const linkPromise = urlShortener.shortenUrl($scope.url)
.then(shortUrl => {
$scope.shortUrl = shortUrl;
});

const embedPromise = urlShortener.shortenUrl($scope.embedUrl)
.then(shortUrl => {
$scope.shortEmbedUrl = shortUrl;
});

Promise.all([linkPromise, embedPromise])
.then(() => {
$scope.shortUrlsLoading = false;
} else {
$scope.shortUrlsLoading = true;

const linkPromise = urlShortener.shortenUrl($scope.url)
.then(shortUrl => {
$scope.shortUrl = shortUrl;
});

const embedPromise = urlShortener.shortenUrl($scope.embedUrl)
.then(shortUrl => {
$scope.shortEmbedUrl = shortUrl;
});

Promise.all([linkPromise, embedPromise])
.then(() => {
$scope.shortUrlsLoading = false;
})
.catch(err => {
$scope.shortenUrls = false;
});
}
})
.catch(err => {
$scope.shortenUrls = false;
});
}
});

$scope.getUrl = function () {
return $location.absUrl();
};
}
};
});
}
});

$scope.getUrl = function () {
return $location.absUrl();
};
}
};
});
38 changes: 18 additions & 20 deletions src/ui/public/share/url_shortener.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
define(function (require) {
return function createUrlShortener(Notifier, $http, $location) {
const notify = new Notifier({
location: 'Url Shortener'
});
const baseUrl = `${$location.protocol()}://${$location.host()}:${$location.port()}`;
export default function createUrlShortener(Notifier, $http, $location) {
const notify = new Notifier({
location: 'Url Shortener'
});
const baseUrl = `${$location.protocol()}://${$location.host()}:${$location.port()}`;

async function shortenUrl(url) {
const relativeUrl = url.replace(baseUrl, '');
const formData = { url: relativeUrl };
async function shortenUrl(url) {
const relativeUrl = url.replace(baseUrl, '');
const formData = { url: relativeUrl };

try {
const result = await $http.post('/shorten', formData);
try {
const result = await $http.post('/shorten', formData);

return `${baseUrl}/goto/${result.data}`;
} catch (err) {
notify.error(err);
throw err;
}
return `${baseUrl}/goto/${result.data}`;
} catch (err) {
notify.error(err);
throw err;
}
}

return {
shortenUrl
};
return {
shortenUrl
};
});
};

0 comments on commit a4b831c

Please sign in to comment.