Skip to content

Commit

Permalink
Added pager directive invalidate function to control directive extern…
Browse files Browse the repository at this point in the history
…ally
  • Loading branch information
MKHenson committed Mar 31, 2016
1 parent 09cc796 commit 99db01f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 56 deletions.
6 changes: 3 additions & 3 deletions admin/html/templates/dash-posts.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div class="console" ng-show="!controller.showNewPostForm">
<div class="button blue" ng-click="controller.newPostMode()"><div class="cross"></div>New Post</div>
<div class="button blue" ng-click="controller.showFilters = !controller.showFilters"><div class="cross" ng-show="!controller.showFilters"></div><div class="minus" ng-show="controller.showFilters"></div>Filters</div>
<div class="search"><input type="text" ng-model="controller.searchKeyword" /><div class="search-button" ng-click="controller.updatePageContent()"></div></div>
<div class="search"><input type="text" ng-model="controller.searchKeyword" /><div class="search-button" ng-click="controller.pager.invalidate()"></div></div>
<div class="fix"></div>
</div>

Expand Down Expand Up @@ -63,7 +63,7 @@
<div class="filter-group">
<div class="label">Category</div>
<div class="dropdown">
<select ng-model="controller.searchCategory" ng-change="controller.updatePageContent()">
<select ng-model="controller.searchCategory" ng-change="controller.pager.invalidate()">
<option value=""></option>
<option ng-repeat="category in controller.categories" ng-value="category.slug">{{category.title}}</option>
</select>
Expand Down Expand Up @@ -192,7 +192,7 @@
</div>

<div class="content-view-item-list" ng-show="!controller.showNewPostForm">
<div pager interface="controller" limit="2">
<div pager interface="controller.pager" limit="2">
<div class="content-view-item" ng-repeat="post in controller.posts">
<div class="arrow animate-fast" ng-click="post.more = (post.more === undefined ? true : !post.more )" ng-class="{ active : post.more }"></div>
<div class="view-item-label date">Created: {{post.createdOn | date:"MM/dd/yyyy 'at' h:mma"}}</div>
Expand Down
84 changes: 47 additions & 37 deletions admin/lib/controllers/posts-ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Controller for the dashboard posts section
*/
export class PostsCtrl extends PagedContentCtrl
export class PostsCtrl
{
public postToken: Modepress.IPost;
public posts: Array<Modepress.IPost>;
Expand All @@ -26,13 +26,18 @@
public showMediaBrowser: boolean;
public defaultSlug: string;
public targetImgReciever: string;

private _q: ng.IQService;
private http: ng.IHttpService;
private error: boolean;
private loading: boolean;
private errorMsg: string;
private pager: IPagerRemote;

// $inject annotation.
public static $inject = ["$scope", "$http", "apiURL", "mediaURL", "categories", "$q"];
constructor(scope, http: ng.IHttpService, apiURL: string, mediaURL: string, categories: Array<Modepress.ICategory>, $q: ng.IQService)
{
super(http);
this.newCategoryMode = false;
this.scope = scope;
this.apiURL = apiURL;
Expand All @@ -51,10 +56,15 @@
this.defaultSlug = "";
this.showMediaBrowser = false;
this.targetImgReciever = "";

this.http = http;
this.loading = false;
this.error = false;
this.errorMsg = "";
this._q = $q;
this.pager = this.createPagerRemote();

this.postToken = { title: "", content: "", slug: "", tags: [], categories: [], public: true, brief: "" };
//this.updatePageContent();
var that = this;

tinymce.init({
Expand Down Expand Up @@ -139,13 +149,13 @@
swapOrder()
{
this.sortOrder = (this.sortOrder == 'asc' ? 'desc' : 'asc');
this.updatePageContent();
this.pager.invalidate();
}

swapSortType()
{
this.sortType = (this.sortType == 'created' ? 'updated' : 'created');
this.updatePageContent();
this.pager.invalidate();
}

/**
Expand Down Expand Up @@ -193,42 +203,42 @@
});
}

/**
* Fetches the posts from the database
*/
updatePageContent(index?: number, limit? : number)
createPagerRemote(): IPagerRemote
{
var that = this;
this.error = false;
this.errorMsg = "";
this.loading = true;
//var index = this.index;
//var limit = this.limit;
var keyword = this.searchKeyword;
var searchCategory = this.searchCategory;
var order = this.sortOrder;
var sortType = this.sortType;

return new this._q<number>(function(resolve, reject)
{
that.http.get<Modepress.IGetPosts>(`${that.apiURL}/posts/get-posts?visibility=all&verbose=true&sort=${sortType}&sortOrder=${order}&categories=${searchCategory}&index=${index}&limit=${limit}&keyword=${keyword}`).then(function (token)
var remote: IPagerRemote = {
update: function(index?: number, limit? : number)
{
if (token.data.error) {
that.error = true;
that.errorMsg = token.data.message;
that.posts = [];
that.last = 1;
resolve(1);
}
else {
that.posts = token.data.data;
that.last = token.data.count;
resolve(token.data.count);
}
that.error = false;
that.errorMsg = "";
that.loading = true;
var keyword = that.searchKeyword;
var searchCategory = that.searchCategory;
var order = that.sortOrder;
var sortType = that.sortType;

return new that._q<number>(function(resolve, reject)
{
that.http.get<Modepress.IGetPosts>(`${that.apiURL}/posts/get-posts?visibility=all&verbose=true&sort=${sortType}&sortOrder=${order}&categories=${searchCategory}&index=${index}&limit=${limit}&keyword=${keyword}`).then(function (token)
{
if (token.data.error) {
that.error = true;
that.errorMsg = token.data.message;
that.posts = [];
resolve(1);
}
else {
that.posts = token.data.data;
resolve(token.data.count);
}

that.loading = false;
});
});
that.loading = false;
});
});
}
};

return remote;
}

/**
Expand Down
27 changes: 18 additions & 9 deletions admin/lib/directives/pager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ module clientAdmin
/**
* Interface for the object you pass as the directive's 'interface' attribute
*/
export interface IPager
export interface IPagerRemote
{
updatePageContent : (index?: number, limit? : number) => ng.IPromise<number>;
update : (index?: number, limit? : number) => ng.IPromise<number>;
invalidate?: () => void;
}


Expand All @@ -18,7 +19,7 @@ module clientAdmin
transclude = true;
templateUrl = 'templates/directives/pager.html';
scope = {
interface: '=', // must be IPager
interface: '=', // must be IPagerRemote
index: '=?',
limit: '=?',
last: '=?'
Expand All @@ -33,7 +34,15 @@ module clientAdmin
scope.index = scope.index || 0;
scope.limit = scope.limit || 10;
scope.last = scope.last || 1;
var iPager : IPager = scope.interface;
var iPager : IPagerRemote = scope.interface;

/**
* Creates the invalidate function which can be used externally to control
* when the pager updates its content
*/
iPager.invalidate = function() {
handlePromise(iPager.update( scope.index, scope.limit ));
}

/**
* Handles the promise returned by the update function
Expand Down Expand Up @@ -71,7 +80,7 @@ module clientAdmin
scope.goFirst = function()
{
scope.index = 0;
handlePromise( iPager.updatePageContent( scope.index, scope.limit) );
handlePromise( iPager.update( scope.index, scope.limit) );
}

/**
Expand All @@ -80,7 +89,7 @@ module clientAdmin
scope.goLast = function()
{
scope.index = scope.last - (scope.last % scope.limit);
handlePromise( iPager.updatePageContent( scope.index, scope.limit) );
handlePromise( iPager.update( scope.index, scope.limit) );
}

/**
Expand All @@ -89,7 +98,7 @@ module clientAdmin
scope.goNext = function()
{
scope.index += scope.limit;
handlePromise( iPager.updatePageContent( scope.index, scope.limit) );
handlePromise( iPager.update( scope.index, scope.limit) );
}

/**
Expand All @@ -101,11 +110,11 @@ module clientAdmin
if (scope.index < 0)
scope.index = 0;

handlePromise( iPager.updatePageContent( scope.index, scope.limit) );
handlePromise( iPager.update( scope.index, scope.limit) );
}

// Call the initial update
handlePromise( iPager.updatePageContent( scope.index, scope.limit) );
handlePromise( iPager.update( scope.index, scope.limit) );
}

/**
Expand Down
17 changes: 10 additions & 7 deletions server/dist/definitions/definitions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ declare module clientAdmin {
/**
* Controller for the dashboard posts section
*/
class PostsCtrl extends PagedContentCtrl {
class PostsCtrl {
postToken: Modepress.IPost;
posts: Array<Modepress.IPost>;
showNewPostForm: boolean;
Expand All @@ -251,6 +251,11 @@ declare module clientAdmin {
defaultSlug: string;
targetImgReciever: string;
private _q;
private http;
private error;
private loading;
private errorMsg;
private pager;
static $inject: string[];
constructor(scope: any, http: ng.IHttpService, apiURL: string, mediaURL: string, categories: Array<Modepress.ICategory>, $q: ng.IQService);
/**
Expand Down Expand Up @@ -287,10 +292,7 @@ declare module clientAdmin {
* Sets the page into edit mode
*/
editPostMode(post: Modepress.IPost): void;
/**
* Fetches the posts from the database
*/
updatePageContent(index?: number, limit?: number): ng.IPromise<number>;
createPagerRemote(): IPagerRemote;
/**
* Processes the tags in a post array of keywords
*/
Expand Down Expand Up @@ -327,8 +329,9 @@ declare module clientAdmin {
/**
* Interface for the object you pass as the directive's 'interface' attribute
*/
interface IPager {
updatePageContent: (index?: number, limit?: number) => ng.IPromise<number>;
interface IPagerRemote {
update: (index?: number, limit?: number) => ng.IPromise<number>;
invalidate?: () => void;
}
/**
* Controller for the dashboard media section
Expand Down

0 comments on commit 99db01f

Please sign in to comment.