Skip to content

Instantly share code, notes, and snippets.

@hendricius
Last active August 29, 2015 14:06
Show Gist options
  • Save hendricius/93a646b031d23dba6847 to your computer and use it in GitHub Desktop.
Save hendricius/93a646b031d23dba6847 to your computer and use it in GitHub Desktop.
app = angular.module("page", ["keywordFilters"])
filters = angular.module("keywordFilters", [])
filters.filter "attrGreaterThan", ->
(arr, minAttr, filterAttr) ->
if !minAttr || !filterAttr
return arr
_.select arr, (kw)->
[kw[filterAttr] || 1] >= minAttr
class Keyword
constructor: (opts = {})->
@name = opts.name
@impressions = opts.impressions
@cpc = opts.cpc
@competition_real = opts.competition_real
@rank = opts.rank
hasAdwordsData: ->
@impressions && @cpc && @competition_real
class KeywordConverter
convertKeywordArray: (keywords)->
_.map keywords, (kw)->
new Keyword kw
convertStringToKeywords: (string)->
names = _.uniq(_.map(string.split("\n"), (e) -> e.trim()))
names = _.select names, (e) -> e.length > 0
_.map names, (name) -> new Keyword name: name
class KeywordCombiner
constructor: (opts = {}) ->
@base_keywords = opts.base_keywords
@combination_keywords = opts.combination_keywords
@suffix = opts.suffix
@prefix = opts.prefix
combine: ->
new_data = _.map @base_keywords, (kw) =>
_.map @combination_keywords, (comb) =>
result = [kw.name]
if @prefix
result.push "#{comb.name} #{kw.name}".trim()
if @suffix
result.push "#{kw.name} #{comb.name}".trim()
result
keyword_strings = _.uniq _.select(_.flatten(new_data), (e) -> e.length > 0)
_.map keyword_strings, (kws) ->
new Keyword name: kws
class EstimateFetcher
# Array of keywords
@fetchQueue = []
@loading = false
#{keywordCallback: Array, callback: function}
@addToQueue = (keywordCallback)=>
@fetchQueue.push(keywordCallback)
fetcher = new EstimateFetcher
fetcher.perform()
constructor: (@keywords, @callback)->
# By default always only query the last member in the array
perform: ->
# Take last element. As they are added in order.
data = _.last(EstimateFetcher.fetchQueue)
# Reset the queue. We will always only load the last element.
EstimateFetcher.fetchQueue = []
# Abort we are done.
return unless data
keywords = data.keywords
callback = data.callback
# FIXME query max 2000 keywords
if EstimateFetcher.loading
# Already loading data.
return true
@load(keywords, callback)
load: (keywords, callback)=>
$.ajax
url: "/adwords_tools/keyword_estimates"
data:
keywords: _.map(keywords, (kw)-> kw.name)
success: (response)->
# ERROR HERE Uncaught TypeError: Cannot read property 'apply' of null
# includes.js?v=898a13ca6797c6bc1fee313e17d388b0:548onReadyStateChangeReplacement
#keywords = _.map(response, (kw) -> new Keyword(kw))
#callback(keywords)
error: ->
console.log "error"
beforeSend: ->
console.log "requesting data"
EstimateFetcher.loading = true
complete: ->
EstimateFetcher.loading = false
app.directive "splitArray", ->
restrict: "A"
require: "ngModel"
link: (scope, element, attr, ngModel) ->
fromUser = (text) ->
text.split "\n"
toUser = (array) ->
array.join "\n"
ngModel.$parsers.push fromUser
ngModel.$formatters.push toUser
app.controller("KeywordController", ['$scope', ($scope)->
$scope.keywordConverter = new KeywordConverter
$scope.keywords = $scope.keywordConverter.convertKeywordArray(window.App.keywords)
$scope.keywordCombinationLists = [
{
admin_link: "what"
name: "what list"
keywords: ["what", "no"],
id: 1
},
{
admin_link: "noes"
name: "noes list"
keywords: ["never", "noes"]
id: 2
}
]
$scope.combinationList = $scope.keywordCombinationLists[0]
$scope.prefixCombination = true
$scope.suffixCombination = true
$scope.automaticallyCombine = true
$scope.newKeywords = ""
$scope.filterRank = 0
$scope.removeKeyword = ($event, keyword)->
$event.preventDefault()
$scope.keywords = _.reject $scope.keywords, (kw) ->
kw.name == keyword.name
$scope.addKeywordsTextarea = ($event) ->
$event.preventDefault()
kws = $scope.keywordConverter.convertStringToKeywords($scope.newKeywords)
if $scope.automaticallyCombine
opts = {
base_keywords: kws
combination_keywords: _.map $scope.combinationList.keywords, (kw) -> new Keyword name: kw
prefix: $scope.prefixCombination
suffix: $scope.suffixCombination
}
combinator = new KeywordCombiner opts
kws = combinator.combine()
# Add all keywords
_.each kws, (kw) ->
$scope.addKeyword kw
# Reset input field
$scope.newKeywords = ""
$scope.getEstimatesForKeywords()
$scope.addKeyword = (keyword)->
# Keyword not added yet, make sure we only have unique keywords
unless _.findWhere($scope.keywords, {name: keyword.name})
$scope.keywords.push keyword
# Enqueue fetching data for keywords
$scope.$watchCollection 'keywords', (oldKeywords, newKeywords)->
console.log "added to queue"
EstimateFetcher.addToQueue({keywords: newKeywords, callback: $scope.addEstimatesForKeywods})
$scope.addEstimatesForKeywods = ->
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment