Skip to content

Instantly share code, notes, and snippets.

@moduscreate
Created June 29, 2012 18:53
Show Gist options
  • Select an option

  • Save moduscreate/3019950 to your computer and use it in GitHub Desktop.

Select an option

Save moduscreate/3019950 to your computer and use it in GitHub Desktop.
Ext.define('AW.controller.Main', {
extend : 'Ext.app.Controller',
config : {
layout : 'card',
tags : undefined,
stories : undefined,
otherStories : undefined,
rawStories : undefined,
currentChannel : undefined,
carouselCards : [
'Layout1',
'Layout2',
'Layout3'
],
views : [
'Main',
'StoriesCarousel',
'Article',
// 'Tagger',
'TagPopup',
'MoreStories',
'TagManager',
'AW.view.carousel.Layout1',
'AW.view.carousel.Layout2',
'AW.view.carousel.Layout3'
],
models : [
'Story',
'SavedArticle',
'Tag'
],
stores : [
'Stories',
'Tags',
'MoreStories'
],
refs : {
main : 'main'
},
control : {
'[type=Layout1]' : {
articletap : 'onArticleTap'
},
skybox : {
articletap : 'onArticleTap'
},
morestories : {
articletap : 'onArticleTap'
},
// Todo : push to article controller
article : {
tagtap : 'onArticleTagTap'
},
tagmanager : {
tagtap : 'onTagManagerTagTap'
},
// Todo : push to article controller
'[action=moreStories]' : {
tap : 'onMoreStoriesTap'
},
'[text=Saved]' : {
tap : 'onSavedStoriesTap'
},
'[iconCls=awire-search]' : {
tap : 'onSearchTap'
},
'[xtype=button][text=Tags]' : {
tap : 'onTagsBtnTap'
}
}
},
launch : function() {
var me = this;
me.fetchArticles();
me.setCurrentChannel('All Stories');
// Initialize the main view
Ext.Viewport.add(Ext.create('AW.view.Main'));
me.getApplication().on({
scope : me,
channelUpdate : 'onAppChannelUpdate',
mask : 'showMask',
unmask : 'hidemask',
articletap : 'onArticleTap'
});
},
applyTags : function(cfg) {
if (cfg === undefined) {
var s = Ext.create('AW.store.Tags');
s.load();
return s;
}
},
applyStories : function(cfg) {
if (cfg === undefined) {
return Ext.create('AW.store.Stories');
}
},
fetchArticles : function(channelSlug, includeTags, excludeTags) {
var params = {
format : 'jsonp',
limit : 50
};
if (channelSlug) {
params.channel__slug = channelSlug;
}
if (includeTags && includeTags.length > 0) {
params.tags__slug__in = includeTags.toString();
delete params.channel__slug;
}
if (excludeTags && excludeTags.length > 0) {
params['-tags__slug__in'] = excludeTags.toString();
delete params.channel__slug;
}
Ext.data.JsonP.request({
url : 'http://www.theatlanticwire.com/api/v0.1/post/',
scope : this,
success : this.onAfterFetchArticles,
params : params
});
},
onAfterFetchArticles : function(result, request) {
var me = this,
data = result.objects;
this.setRawStories(result.objects);
var pages = [],
stories = this.getStories();
stories.setData(data);
var storeData = [].concat(stories.getRange()),
pageObj,
page;
while (storeData.length > 0) {
page = storeData.splice(0, 7);
pageObj = {};
Ext.each(page, function(record, index) {
pageObj['item' + index ] = record.data;
});
pages.push(pageObj);
}
pages.pop(); // incomplete card is last
me.renderCards(pages);
me.hideMask();
},
getRandomCard : function() {
var cardMap = this.getCarouselCards(),
numItems = cardMap.length,
lastCard = this.lastCardIndex,
number = 99;
while (number >= numItems) {
number = Math.floor(Math.random() * 10);
if (number == lastCard) {
number = 99;
}
}
this.lastCardIndex = number;
return cardMap[number];
},
renderCards : function(pages) {
var me = this,
cards = [],
viewNs = AW.view.carousel,
card;
Ext.each(pages, function(page, i) {
// card = me.getRandomCard();
card = 'Layout2';
cards[i] = new viewNs[card]({
data : page
});
});
this.getMain().addNewItem({
xtype : 'storiescarousel',
items : cards
}, 'fade');
},
onAppChannelUpdate : function(channel, originalChannelName) {
this.showMask();
var currentChannel = this.getCurrentChannel(),
isAllStories = channel == 'all stories';
if (! currentChannel && isAllStories || this.getCurrentChannel() == originalChannelName) {
return;
}
this.setCurrentChannel(originalChannelName);
var excludeTags = [],
includeTags = [],
tags = this.getTags(),
add = 'add';
// grab the slugs
tags.each(function (tag) {
var slug = tag.data.slug;
if (tag.data.status == add) {
includeTags.push(slug);
}
else {
excludeTags.push(slug);
}
});
if (isAllStories) {
this.fetchArticles();
}
else if (channel == 'mywire') {
this.fetchArticles(undefined, includeTags, excludeTags);
}
else {
this.fetchArticles(channel);
}
},
onArticleTap : function(view, resource_uri) {
this.showMask();
Ext.data.JsonP.request({
url : 'http://www.theatlanticwire.com' + resource_uri,
scope : this,
success : this.onAfterArticleLoad,
params : {
format : 'jsonp'
}
});
},
onMoreStoriesTap : function() {
var rawStories = this.getRawStories();
Ext.Viewport.add({
xtype : 'morestories',
title : this.getCurrentChannel(),
store : Ext.create('AW.store.MoreStories', {
data : rawStories
})
}).show();
},
onSearchTap : function(button) {
Ext.Viewport.add({
xclass : 'AW.view.Search'
}).show();
},
onAfterArticleLoad : function(article) {
this.hideMask();
// TODO : Clean up or put in a util method
article.content = article.content.replace(/\r/g, '').replace(/\n/g, '').replace(/\t/g, '');
var publishTime = article.pub_date.split('T')[1].split(':'),
publishHour = +publishTime[0],
publishMinute = publishTime[1],
amPm = publishHour >= 12 ? 'PM' : 'AM',
tags = this.getTags(),
slug = 'slug',
resume = 'resume',
negOne = -1,
foundTagIndex;
if (publishHour > 12) {
publishHour -= 12;
}
Ext.each(article.tags, function(tag) {
foundTagIndex = tags.find(slug, tag.slug);
if (foundTagIndex > negOne) {
tag.status = tags.getAt(foundTagIndex).data.status;
}
else {
tag.status = resume;
}
});
article.pub_time = publishHour + ':' + publishMinute + ' ' + amPm;
this.getApplication().fireEvent('showarticle', article, this.getCurrentChannel(), this.getStories());
},
onArticleTagTap : function(article, slug, msgCfg) {
// TODO: Reuse popup
var tagPopup = Ext.create('AW.view.TagPopup', msgCfg);
Ext.Viewport.add(tagPopup);
Ext.Function.defer(tagPopup.hide, 500, tagPopup);
this.doUpdateTags(slug);
},
showMask : function() {
Ext.Viewport.setMasked({
xtype : 'loadmask',
message : 'Loading...'
});
},
hideMask : function() {
Ext.Viewport.setMasked(false)
},
doUpdateTags : function(tag) {
var tags = this.getTags(),
status = tag.status,
model,
foundTagIndex,
foundTag;
foundTagIndex = tags.find('slug', tag.slug);
foundTag = tags.getAt(foundTagIndex);
if (status == 'resume') {
tags.remove(foundTag);
}
else if (! foundTag) {
model = tags.getModel();
tags.add(new model(tag));
}
else {
foundTag.set('status', status);
}
tags.sync();
},
onTagsBtnTap : function() {
var tags = this.getTags(),
items = [];
tags.each(function(tag) {
items.push(tag.data);
});
Ext.Viewport.add({
xclass : 'AW.view.TagManager',
data : items
}).show();
},
onTagManagerTagTap : function(view, slug, status) {
var tags = this.getTags(),
tagIndex = tags.find('slug', slug),
tag = tags.getAt(tagIndex);
if (status == 'resume') {
tags.remove(tag);
}
else {
tag.set('status', status);
tag.commit();
}
tags.sync();
},
onSavedStoriesTap : function() {
this.getApplication().fireEvent('showSavedStories');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment