Skip to content

Instantly share code, notes, and snippets.

@jtremback
Created November 24, 2013 04:12
Show Gist options
  • Save jtremback/7623246 to your computer and use it in GitHub Desktop.
Save jtremback/7623246 to your computer and use it in GitHub Desktop.
object nonsense
'use strict';
/* global $, jQuery, alert */
// _.map(DB, function(value, key){
// return key;
// });
function Seed () {
var self = $.observable(this);
self.get = function(hash, callback) {
$.getJSON( './server/terms/seeds', function(data) {
self.items = data;
self.trigger('get');
callback(hash);
});
};
self.init = function(hash) {
self.get(hash, function(){
self.active = self.items[0].term;
self.trigger('init');
self.trigger('active');
});
};
self.drill = function(name) {
self.active = name;
self.trigger('active');
};
}
function Phrase() {
var self = $.observable(this);
self.get = function(name, callback) {
$.getJSON( './server/terms/rmm/' + name, function(data) {
self.items = data;
self.name = name;
console.log(self.items)
self.trigger('get');
if (callback) callback(name, data);
});
};
self.init = function(name) {
self.get(name, function(){
self.trigger('init');
});
};
self.drill = function(name) {
self.active = name;
self.trigger('active');
};
}
function Term() {
var self = $.observable(this);
self.get = function(name, items) {
self.items = items[name].search_terms;
self.name = name;
self.trigger('get');
};
}
function Stat() {
var self = $.observable(this);
self.progress_calc = function(data) {
data.excluded_percent = (data.excluded / data.search_terms) * 100;
data.included_percent = (data.coverage - data.excluded_percent);
data.wasted = Math.floor(data.cost);
return data;
};
self.include = function(name, seed) {
var req_name = name.replace(seed, '').trim();
$.getJSON( './server/terms/positive_keyword/' + req_name, function(data) {
self.data = self.progress_calc(data[0]);
console.log(self.data);
self.trigger('include');
});
};
self.exclude = function(name, seed) {
var req_name = name.replace(seed, '').trim();
$.getJSON( './server/terms/negative_keyword/' + req_name, function(data) {
self.data = self.progress_calc(data[0]);
console.log(self.data);
self.trigger('exclude');
});
};
}
'use strict';
/* global $, jQuery, alert */
var List, seedList, phraseList, termList, statPanel;
var seed = new Seed();
var phrase = new Phrase();
var term = new Term();
var stat = new Stat();
//PRESENTERS
seedList = function ($root, template) {
seed.on('get', load);
seed.on('init', init);
seed.on('active', function(){
$('[data-name]', $root).removeClass('active');
$('[data-name=' + seed.active + ']', $root).addClass('active');
});
function init () {
phrase.get(seed.active);
}
function load () {
$root.empty();
$.each(seed.items, function(index, value) {
var item = {};
item.name = value.term;
add(item);
});
}
function add (item) {
var el = $($.render(template, item)).appendTo($root);
el.click(function() {
drill(item.name);
});
}
function drill(name) {
seed.drill(name);
phrase.get(name);
}
}($('#seed-list'), $('[type="html/seed"]').html());
phraseList = function ($root, template) {
phrase.on('get', load);
phrase.on('active', function(){
$('[data-name]', $root).removeClass('active');
$('[data-name="' + phrase.active + '"]', $root).addClass('active');
});
function load () {
$root.find('.js-list').empty();
$root.find('.js-title').html('Phrases for "' + phrase.name + '"');
$.each(phrase.items, function(index, value) {
var item = {};
item.name = index;
item.included = value.included_positive_keyword ? 'included' : '';
add(item);
});
}
function add (item) {
var el = $($.render(template, item)).appendTo($root.find('.js-list'));
el.click(function() {
drill(item.name);
});
}
function drill(name) {
phrase.drill(name);
term.get(name, phrase.items);
}
}($('#phrase-list'), $('[type="html/phrase"]').html());
termList = function ($root, template) {
term.on('get', load);
$('.js-include').on('click', function(){
stat.include(term.name, seed.active);
});
$('.js-exclude').on('click', function(){
stat.exclude(term.name, seed.active);
});
function load () {
$root.find('.js-list').empty();
$root.find('.js-title').html('Terms for "' + term.name + '"');
$.each(term.items, function(index, value) {
var item = {};
item.name = value.search_term;
add(item);
});
}
function add (item) {
var el = $($.render(template, item)).appendTo($root.find('.js-list'));
}
}($('#term-list'), $('[type="html/term"]').html());
statPanel = function ($root, template) {
stat.on('include exclude', load);
function load () {
phrase.get(phrase.name);
$root.empty();
var el = $($.render(template, stat.data)).appendTo($root);
}
}($('#stats'), $('[type="html/stats"]').html());
//ROUTER
$.route(function(hash) {
seed.init(hash);
});
/*
Riot.js 0.9.3 | moot.it/riotjs | @license MIT
(c) 2013 Tero Piirainen, Moot Inc and other contributors.
*/
"use strict";
(function() {
// avoid multiple execution. popstate should be fired only once etc.
if ($.riot) return;
$.riot = "0.9.3";
// Precompiled templates (JavaScript functions)
var FN = {}, slice = [].slice;
// Render a template with data
$.render = function(template, data) {
return !template ? '' : (FN[template] = FN[template] || new Function("_",
"return '" + template
.replace(/\n/g, "\\n")
.replace(/\r/g, "\\r")
.replace(/'/g, "\\'")
.replace(/\{\s*(\w+)\s*\}/g, "' + (_.$1 === undefined || _.$1 === null ? '' : _.$1) + '") +
"'"
))(data);
}
// A classic pattern for separating concerns
$.observable = function(obj) {
var jq = $("<a/>"); // plain object not working on Zepto
$.each(['on', 'one', 'trigger', 'off'], function(i, name) {
obj[name] = function(names, fn) {
// on, one
if (i < 2) {
jq[name](names, function(e) {
var args = slice.call(arguments, 1);
if (names.split(" ")[1]) args.unshift(e.type);
fn.apply(obj, args);
});
// trigger
} else if (i === 2) {
jq.trigger(names, slice.call(arguments, 1));
// off
} else {
jq.off(names);
}
return obj;
};
});
return obj;
};
// jQueried window object
var win = $(window);
// emit window.popstate event consistently on page load, on every browser
var page_popped;
win.on("load", function(e) {
setTimeout(function() {
if (!page_popped) win.trigger("popstate");
}, 1);
}).on("popstate", function(e) {
if (!page_popped) page_popped = true;
});
// Change the browser URL or listen to changes on the URL
$.route = function(to) {
// listen
if ($.isFunction(to)) {
win.on("popstate", function(e, hash) {
// console.log(hash || location.hash)
to(hash || location.hash);
});
// fire
} else if (to != location.hash) {
if (history.pushState) history.pushState("", "", to);
win.trigger("popstate", [to]);
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment