Skip to content

Instantly share code, notes, and snippets.

@nikoma
Created August 21, 2014 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikoma/f90d36699e09de1ae31f to your computer and use it in GitHub Desktop.
Save nikoma/f90d36699e09de1ae31f to your computer and use it in GitHub Desktop.
Tracks for dashboard example
'use strict';
// Configuring the Articles module
angular.module('tracks').run(['Menus',
function(Menus) {
// Set top bar menu items
// Menus.addMenuItem('topbar', 'Tracks', 'tracks', 'dropdown');
// Menus.addSubMenuItem('topbar', 'tracks', 'Facebook', 'tracks');
}
]);
'use strict';
//Setting up route
angular.module('tracks').config(['$stateProvider',
function ($stateProvider) {
// Tracks state routing
$stateProvider.
// state('listTracks', {
// url: '/tracks',
// templateUrl: 'modules/tracks/views/list-tracks.client.view.html'
// }).
state('createTrack', {
url: '/tracks/create',
templateUrl: 'modules/tracks/views/create-track.client.view.html'
}).
state('listTracksSearch', {
url: '/tracks/search/:trackId',
templateUrl: 'modules/tracks/views/list-searches.client.view.html'
}).
//vishal
state('listTracksYoutube', {
url: '/tracks/youtube/:trackId',
templateUrl: 'modules/tracks/views/list-youtube.client.view.html'
}).
state('listTracksFacebook', {
url: '/tracks/:trackId',
templateUrl: 'modules/tracks/views/list-tracks.client.view.html'
}).
state('listTracksTwitter', {
url: '/tracks/twitter/:trackId',
templateUrl: 'modules/tracks/views/list-twitter.client.view.html'
}).
state('listTracksInstagram', {
url: '/tracks/instagram/:trackId',
templateUrl: 'modules/tracks/views/list-instagram.client.view.html'
}).
state('editTrack', {
url: '/tracks/:trackId/edit',
templateUrl: 'modules/tracks/views/edit-track.client.view.html'
});
}
]);
'use strict';
// Tracks controller
angular.module('tracks').controller('InstagramController', ['$scope', '$stateParams', '$location', 'Authentication', 'apphera',
function ($scope, $stateParams, $location, Authentication, apphera) {
$scope.authentication = Authentication;
$scope.tracks = [];
$scope.loader = true;
$scope.noData = false;
$scope.url = apphera.dashboardApi+'tracks/'+$stateParams.trackId+'/instagram';
// Find a list of Tracks
// $scope.find = function () {
// $http.get('/api/tracks/' + $stateParams.trackId + '/instagram').success(function (data) {
// $scope.tracks = data;
// $scope.loader = false;
// $scope.totalItems = data.length;
// $scope.currentPage = 1;
// $scope.pageChanged();
// if (data.length <= 0) {
// $scope.noData = true;
// }
// });
// };
// Find existing Track
$scope.findOne = function () {
$scope.track = apphera.getTrackResults('instagram', $stateParams.trackId)
.then(function(result){
$scope.tracks = result;
});
};
}
]);
'use strict';
// Tracks controller
angular.module('tracks').controller('SearchController', ['$scope', '$http', '$stateParams', '$location', 'Authentication', 'Search', 'apphera',
function ($scope, $http, $stateParams, $location, Authentication, Search, apphera) {
$scope.authentication = Authentication;
$scope.tracks = [];
$scope.loader = true;
$scope.noData = false;
$scope.market = 'en-IN';
$scope.filteredTracksSearch = [];
$scope.totalItems = 0;
$scope.currentPage = 0;
$scope.setPage = function (pageNo) {
$scope.currentPage = pageNo;
};
$scope.pageChanged = function () {
var begin = ($scope.currentPage - 1) * 10;
var end = begin + 10;
console.log(begin + ' ' + end);
$scope.filteredTracksSearch = $scope.tracks.slice(begin, end);
};
// Find a list of Tracks
$scope.find = function () {
$scope.tracks = [];
apphera.getTrackResults('search', $stateParams.trackId, $scope.market)
.then(function(result){
$scope.tracks = result.ranks.results;
console.log(result);
$scope.totalItems = result.length;
console.log('length is :' + result.length);
});
$scope.loader = false;
$scope.currentPage = 1;
$scope.pageChanged();
// $http.get('/api/tracks/' + $stateParams.trackId + '/search/' + $scope.market).success(function (data) {
// if(data){
// $scope.noData = true;
// }else{
// $scope.tracks = data.results;
// //console.log(data);
// $scope.totalItems = data.results.length;
// }
// $scope.loader = false;
// $scope.currentPage = 1;
// $scope.pageChanged();
// console.log($scope.loader);
// });
};
// // Find existing Track
// $scope.findOne = function () {
// $scope.track = Search.get({
// trackId: $stateParams.trackId
// });
// };
}
]);
'use strict';
// Tracks controller
angular.module('tracks').controller('TracksController', ['$scope', '$http', '$stateParams', '$location', 'Authentication', 'Tracks',
function($scope, $http, $stateParams, $location, Authentication, Tracks ) {
$scope.authentication = Authentication;
$scope.tracks = [];
$scope.totalItems = 0;
$scope.currentPage = 0;
$scope.url = apphera.dashboardApi + 'tracks/'+$stateParams.trackId+'/facebook';
// Create new Track
$scope.create = function() {
// Create new Track object
var track = new Tracks ({
name: this.name
});
// Redirect after save
track.$save(function(response) {
$location.path('tracks/' + response._id);
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
// Clear form fields
this.name = '';
};
// Remove existing Track
$scope.remove = function( track ) {
if ( track ) { track.$remove();
for (var i in $scope.tracks ) {
if ($scope.tracks [i] === track ) {
$scope.tracks.splice(i, 1);
}
}
} else {
$scope.track.$remove(function() {
$location.path('tracks');
});
}
};
// Update existing Track
$scope.update = function() {
var track = $scope.track ;
track.$update(function() {
$location.path('tracks/' + track._id);
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
// Find a list of Tracks
$scope.find = function() {
//$scope.tracks = Tracks.query();
//$scope.tracks = Tracks.query(url;
//http:localhost:3000/api/ http to get data
// $http.get('api/tracks/'+$stateParams.trackId+'/facebook').success(function(data){
// $scope.tracks = data;
// $scope.totalItems = data.length;
// $scope.currentPage = 1;
// $scope.pageChanged();
// });
};
// Find existing Track
$scope.findOne = function() {
$scope.track = Tracks.get({
trackId: $stateParams.trackId
});
};
}
]);
'use strict';
// Tracks controller
angular.module('tracks').controller('twitterController', ['$scope', '$http', '$stateParams', '$location', 'Authentication', 'Search', 'Twitter',
function ($scope, $http, $stateParams, $location, Authentication, Search) {
$scope.authentication = Authentication;
$scope.tracks = [];
$scope.loader = true;
$scope.noData = false;
$scope.url = apphera.dashboardApi+'tracks/'+$stateParams.trackId+'/twitter';
$scope.filteredTracksTwitter = [];
$scope.test = [];
// Find a list of Track
$scope.$on('pagination:loadPage', function (event, status, config) {
// config contains parameters of the page request
console.log(config.url);
// status is the HTTP status of the result
console.log(status);
});
// Find existing Track
$scope.findOne = function () {
$scope.track = Search.get({
trackId: $stateParams.trackId
});
};
}
]);
'use strict';
// Tracks controller
angular.module('tracks').controller('youtubeController', ['$scope', '$http', '$stateParams', '$location', 'Authentication', 'Search',
function ($scope, $http, $stateParams, $location, Authentication, Search) {
$scope.authentication = Authentication;
$scope.tracks = [];
$scope.loader = true;
$scope.noData = false;
$scope.youtubes = [];
$scope.url = apphera.dashboardApi+'tracks/'+$stateParams.trackId+'/youtube';
// $scope.find = function () {
// $http.get('/api/tracks/' + $stateParams.trackId + '/youtube').success(function (data) {
// $scope.tracks = data;
// $scope.loader = false;
// $scope.totalItems = data.length;
// $scope.currentPage = 1;
// $scope.pageChanged();
// if (data.length <= 0) {
// $scope.noData = true;
// }
// });
// };
// Find existing Track
$scope.findOne = function () {
$scope.track = Search.get({
trackId: $stateParams.trackId
});
};
}
]);
'use strict';
//Tracks service used to communicate Tracks REST endpoints TODO: Add keyword ID here
angular.module('tracks').factory('Search', ['$resource',
function($resource) {
return $resource('/api/tracks/633/search', { trackId: '@_id'
}, {
update: {
method: 'PUT'
}
});
}
]);
'use strict';
//Tracks service used to communicate Tracks REST endpoints TODO: Add keyword ID here
angular.module('tracks').factory('Tracks', ['$resource',
function($resource) {
return $resource('/api/tracks/633/facebook', { trackId: '@_id'
}, {
update: {
method: 'PUT'
}
});
}
]);
'use strict';
//Tracks service used to communicate Tracks REST endpoints TODO: Add keyword ID here
angular.module('tracks').factory('Youtube', ['$resource',
function($resource) {
return $resource('/api/tracks/648/youtube', { trackId: '@_id'
}, {
update: {
method: 'PUT'
}
});
}
]);
'use strict';
(function() {
// Tracks Controller Spec
describe('Tracks Controller Tests', function() {
// Initialize global variables
var TracksController,
scope,
$httpBackend,
$stateParams,
$location;
// The $resource service augments the response object with methods for updating and deleting the resource.
// If we were to use the standard toEqual matcher, our tests would fail because the test values would not match
// the responses exactly. To solve the problem, we define a new toEqualData Jasmine matcher.
// When the toEqualData matcher compares two objects, it takes only object properties into
// account and ignores methods.
beforeEach(function() {
jasmine.addMatchers({
toEqualData: function(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
return {
pass: angular.equals(actual, expected)
};
}
};
}
});
});
// Then we can start by loading the main application module
beforeEach(module(ApplicationConfiguration.applicationModuleName));
// The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
// This allows us to inject a service but then attach it to a variable
// with the same name as the service.
beforeEach(inject(function($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_) {
// Set a new global scope
scope = $rootScope.$new();
// Point global variables to injected services
$stateParams = _$stateParams_;
$httpBackend = _$httpBackend_;
$location = _$location_;
// Initialize the Tracks controller.
TracksController = $controller('TracksController', {
$scope: scope
});
}));
it('$scope.find() should create an array with at least one Track object fetched from XHR', inject(function(Tracks) {
// Create sample Track using the Tracks service
var sampleTrack = new Tracks({
name: 'New Track'
});
// Create a sample Tracks array that includes the new Track
var sampleTracks = [sampleTrack];
// Set GET response
$httpBackend.expectGET('tracks').respond(sampleTracks);
// Run controller functionality
scope.find();
$httpBackend.flush();
// Test scope value
expect(scope.tracks).toEqualData(sampleTracks);
}));
it('$scope.findOne() should create an array with one Track object fetched from XHR using a trackId URL parameter', inject(function(Tracks) {
// Define a sample Track object
var sampleTrack = new Tracks({
name: 'New Track'
});
// Set the URL parameter
$stateParams.trackId = '525a8422f6d0f87f0e407a33';
// Set GET response
$httpBackend.expectGET(/tracks\/([0-9a-fA-F]{24})$/).respond(sampleTrack);
// Run controller functionality
scope.findOne();
$httpBackend.flush();
// Test scope value
expect(scope.track).toEqualData(sampleTrack);
}));
it('$scope.create() with valid form data should send a POST request with the form input values and then locate to new object URL', inject(function(Tracks) {
// Create a sample Track object
var sampleTrackPostData = new Tracks({
name: 'New Track'
});
// Create a sample Track response
var sampleTrackResponse = new Tracks({
_id: '525cf20451979dea2c000001',
name: 'New Track'
});
// Fixture mock form input values
scope.name = 'New Track';
// Set POST response
$httpBackend.expectPOST('tracks', sampleTrackPostData).respond(sampleTrackResponse);
// Run controller functionality
scope.create();
$httpBackend.flush();
// Test form inputs are reset
expect(scope.name).toEqual('');
// Test URL redirection after the Track was created
expect($location.path()).toBe('/tracks/' + sampleTrackResponse._id);
}));
it('$scope.update() should update a valid Track', inject(function(Tracks) {
// Define a sample Track put data
var sampleTrackPutData = new Tracks({
_id: '525cf20451979dea2c000001',
name: 'New Track'
});
// Mock Track in scope
scope.track = sampleTrackPutData;
// Set PUT response
$httpBackend.expectPUT(/tracks\/([0-9a-fA-F]{24})$/).respond();
// Run controller functionality
scope.update();
$httpBackend.flush();
// Test URL location to new object
expect($location.path()).toBe('/tracks/' + sampleTrackPutData._id);
}));
it('$scope.remove() should send a DELETE request with a valid trackId and remove the Track from the scope', inject(function(Tracks) {
// Create new Track object
var sampleTrack = new Tracks({
_id: '525a8422f6d0f87f0e407a33'
});
// Create new Tracks array and include the Track
scope.tracks = [sampleTrack];
// Set expected DELETE response
$httpBackend.expectDELETE(/tracks\/([0-9a-fA-F]{24})$/).respond(204);
// Run controller functionality
scope.remove(sampleTrack);
$httpBackend.flush();
// Test array after successful delete
expect(scope.tracks.length).toBe(0);
}));
});
}());
'use strict';
// Use applicaion configuration module to register a new module
ApplicationConfiguration.registerModule('tracks');
<section data-ng-controller="TracksController">
<div class="page-header">
<h1>New Track</h1>
</div>
<div class="col-md-12">
<form class="form-horizontal" data-ng-submit="create()" novalidate>
<fieldset>
<div class="form-group">
<label class="control-label" for="name">Name</label>
<div class="controls">
<input type="text" data-ng-model="name" id="name" class="form-control" placeholder="Name" required>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-default">
</div>
<div data-ng-show="error" class="text-danger">
<strong data-ng-bind="error"></strong>
</div>
</fieldset>
</form>
</div>
</section>
<section data-ng-controller="TracksController" data-ng-init="findOne()">
<div class="page-header">
<h1>Edit Track</h1>
</div>
<div class="col-md-12">
<form class="form-horizontal" data-ng-submit="update()" novalidate>
<fieldset>
<div class="form-group">
<label class="control-label" for="name">Name</label>
<div class="controls">
<input type="text" data-ng-model="track.name" id="name" class="form-control" placeholder="Name" required>
</div>
</div>
<div class="form-group">
<input type="submit" value="Update" class="btn btn-default">
</div>
<div data-ng-show="error" class="text-danger">
<strong data-ng-bind="error"></strong>
</div>
</fieldset>
</form>
</div>
</section>
<section data-ng-controller="InstagramController" data-ng-init="find()">
<div class="page-header">
<h1>Instagram</h1>
</div>
<div class="loader">
<div ng-show="loader">
<img src="modules/core/img/big-ajax-loader.gif"/>
</div>
</div>
<div class="alert alert-warning text-center" ng-show="noData">
No data found
</div>
<div class="list-group">
<a target="_blank" data-ng-repeat="instagram in tracks" data-ng-href="{{instagram.body.link}}"
class="list-group-item">
<small class="list-group-item-text1">
<table cellspacing="0" cellpadding="0" border="0" width="100%;">
<tr>
<td width="10%"><img ng-src="{{instagram.body.images.thumbnail.url}}"/>&nbsp;</td>
<td width="3%">
<spacer type="block" width="1%">
</td>
<td width="30%"> by <span data-ng-bind="instagram.body.user.full_name"></span></td>
<td width="3%">
<spacer type="block" width="1%">
</td>
<td width="30%">like count <span data-ng-bind="instagram.body.likes.count"></span></td>
<td width="3%">
<spacer type="block" width="1%">
</td>
<td width="21%"><span data-ng-bind="instagram.body.created_time | date:'medium'"></span></td>
</tr>
</table>
</small>
<h4 class="list-group-item-heading" data-ng-bind="instagram.text"></h4>
</a>
<begriffs.pagination collection="tracks" per-page="10" url="url"></begriffs.pagination>
</div>
</section>
<section data-ng-controller="SearchController" data-ng-init="find()">
<div class="page-header">
<h1>Search
<select ng-model="market" ng-change="find()">
<option value="en-IN">India</option>
<option value="en-US">USA</option>
</select>
</h1>
</div>
<div class="loader">
<div ng-show="loader">
<img src="modules/core/img/big-ajax-loader.gif" />
</div></div>
<div class="alert alert-warning text-center" ng-show="noData">
No data found
</div>
<div class="list-group">
<a target="_blank" data-ng-repeat="track in tracks" data-ng-href="{{track.link}}" class="list-group-item">
<!--<small class="list-group-item-text">
Posted on
<span data-ng-bind="track.body.created_time | date:'medium'"></span>
by
<span data-ng-bind="track.body.from.name"></span>
<img ng-src="{{track.body.picture}}"/>
</small>-->
<small class="list-group-item-text1">
<table cellspacing="0" cellpadding="0" border="0" width="100%;">
<tr>
<td width="20%">{{track.ranks}}</td>
<!-- <td width="3%"><spacer type="block" width="1%"></td>
<td width="62%"> by <span data-ng-bind="track.body.from.name"></span> </td>
<td width="1%"><spacer type="block" width="1%"></td>
<td width="12%">Posted on <span data-ng-bind="twitter.created | date:'medium'"></span></td>
<td width="2%"><spacer type="block" width="1%"></td>
<td width="14%"><span data-ng-bind="track.body.created_time | date:'medium'"></ span>-->
<td width="20%">
{{track.title}}
</td>
<td width="20%">
{{track.link_text}}
</td>
</tr>
</table>
</small>
<h4 class="list-group-item-heading" data-ng-bind="track.name"></h4>
</a>
<pagination total-items="totalItems" ng-model="currentPage" ng-change="pageChanged()"></pagination>
</div>
</section>
<section data-ng-controller="TracksController" data-ng-init="find()">
<div class="page-header">
<h1>Tracks</h1>
</div>
<div class="list-group">
<a data-ng-repeat="track in tracks" data-ng-href="#!/tracks/{{track._id}}" class="list-group-item">
<!--<small class="list-group-item-text">
Posted on
<span data-ng-bind="track.body.created_time | date:'medium'"></span>
by
<span data-ng-bind="track.body.from.name"></span>
<img ng-src="{{track.body.picture}}"/>
</small>-->
<small class="list-group-item-text1">
<table cellspacing="0" cellpadding="0" border="0" width="100%;">
<tr>
<td width="6%"><img ng-src="{{track.body.picture}}"/>&nbsp;</td>
<td width="3%"><spacer type="block" width="1%"></td>
<td width="62%"> by <span data-ng-bind="track.body.from.name"></span> </td>
<td width="1%"><spacer type="block" width="1%"></td>
<td width="12%">Posted on <span data-ng-bind="twitter.created | date:'medium'"></span></td>
<td width="2%"><spacer type="block" width="1%"></td>
<td width="14%"><span data-ng-bind="track.body.created_time | date:'medium'"></span></td>
</tr>
</table>
</small>
<h4 class="list-group-item-heading" data-ng-bind="track.name"></h4>
</a>
<begriffs.pagination per-page="10" collection="tracks" url="url"></begriffs.pagination>
</div>
</section>
<section data-ng-controller="twitterController" data-ng-init="find()">
<div class="page-header">
<h1>Twitter</h1>
</div>
<div class="loader">
<div ng-show="loader">
<img src="modules/core/img/big-ajax-loader.gif" />
</div></div>
<div class="alert alert-warning text-center" ng-show="noData">
No data found
</div>
<div class="list-group">
<a target="_blank" data-ng-repeat="twitter in test" data-ng-href="{{twitter.player_url}}" class="list-group-item">
<small class="list-group-item-text1">
<table cellspacing="0" cellpadding="0" border="0" width="100%;">
<tr>
<td width="10%"><img ng-src="{{twitter.body.user.profile_image_url}}"/>&nbsp;</td>
<td width="3%"><spacer type="block" width="1%"></td>
<td width="30%"> by <span data-ng-bind="twitter.twitter_user_name"></span> </td>
<td width="3%"><spacer type="block" width="1%"></td>
<td width="30%">Posted on <span data-ng-bind="twitter.body.text"></span></td>
<td width="3%"><spacer type="block" width="1%"></td>
<td width="21%"><span data-ng-bind="twitter.created_at | date:'medium'"></span></td>
</tr>
</table>
</small>
<h4 class="list-group-item-heading" data-ng-bind="twitter.name"></h4>
</a>
<begriffs.pagination collection="test" url="url"></begriffs.pagination>
</div>
</section>
<section data-ng-controller="youtubeController" data-ng-init="find()">
<div class="page-header">
<h1>Youtube</h1>
</div>
<div class="loader">
<div ng-show="loader">
<img src="modules/core/img/big-ajax-loader.gif" />
</div></div>
<div class="alert alert-warning text-center" ng-show="noData">
No data found
</div>
<div class="list-group">
<a target="_blank" data-ng-repeat="youtube in youtubes" data-ng-href="{{youtube.player_url}}" class="list-group-item">
<small class="list-group-item-text1">
<table cellspacing="0" cellpadding="0" border="0" width="100%;">
<tr>
<td width="10%"><img ng-src="{{youtube.thumbnails[0].url}}"/>&nbsp;</td>
<td width="3%"><spacer type="block" width="1%"></td>
<td width="30%"> by <span data-ng-bind="youtube.title"></span> </td>
<td width="3%"><spacer type="block" width="1%"></td>
<td width="30%">Posted on <span data-ng-bind="youtube.description"></span></td>
<td width="3%"><spacer type="block" width="1%"></td>
<td width="21%"><span data-ng-bind="youtube.published_at | date:'medium'"></span></td>
</tr>
</table>
</small>
<h4 class="list-group-item-heading" data-ng-bind="youtube.name"></h4>
</a>
<begriffs.pagination per-page="10" collection="youtubes" url="url"></begriffs.pagination>
</div>
</section>
<section data-ng-controller="TracksController" data-ng-init="search()">
<!--<div class="page-header">
</div>
<div class="pull-right">
<p>{{track.ranking}}</p>
<a class="btn btn-primary" target="_blank" href="{{link}}">
<i class="glyphicon glyphicon-edit">{{track.link_text}}</i>
</a>
</div>-->
</section>
<section data-ng-controller="TracksController" data-ng-init="findOne()">
<div class="page-header">
<h1 data-ng-bind="track.name"></h1>
</div>
<div class="pull-right" data-ng-show="authentication.user._id == track.user._id">
<a class="btn btn-primary" href="/#!/tracks/{{track._id}}/edit">
<i class="glyphicon glyphicon-edit"></i>
</a>
<a class="btn btn-primary" data-ng-click="remove();">
<i class="glyphicon glyphicon-trash"></i>
</a>
</div>
<small>
<em class="text-muted">
Posted on
<span data-ng-bind="track.created | date:'mediumDate'"></span>
by
<span data-ng-bind="track.user.displayName"></span>
</em>
</small>
</section>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment