Skip to content

Instantly share code, notes, and snippets.

@ETBlue
Created May 26, 2013 06:19
Show Gist options
  • Save ETBlue/5651882 to your computer and use it in GitHub Desktop.
Save ETBlue/5651882 to your computer and use it in GitHub Desktop.
_public/js/app.js
(function(/*! Brunch !*/) {
'use strict';
var globals = typeof window !== 'undefined' ? window : global;
if (typeof globals.require === 'function') return;
var modules = {};
var cache = {};
var has = function(object, name) {
return ({}).hasOwnProperty.call(object, name);
};
var expand = function(root, name) {
var results = [], parts, part;
if (/^\.\.?(\/|$)/.test(name)) {
parts = [root, name].join('/').split('/');
} else {
parts = name.split('/');
}
for (var i = 0, length = parts.length; i < length; i++) {
part = parts[i];
if (part === '..') {
results.pop();
} else if (part !== '.' && part !== '') {
results.push(part);
}
}
return results.join('/');
};
var dirname = function(path) {
return path.split('/').slice(0, -1).join('/');
};
var localRequire = function(path) {
return function(name) {
var dir = dirname(path);
var absolute = expand(dir, name);
return globals.require(absolute);
};
};
var initModule = function(name, definition) {
var module = {id: name, exports: {}};
definition(module.exports, localRequire(name), module);
var exports = cache[name] = module.exports;
return exports;
};
var require = function(name) {
var path = expand(name, '.');
if (has(cache, path)) return cache[path];
if (has(modules, path)) return initModule(path, modules[path]);
var dirIndex = expand(path, './index');
if (has(cache, dirIndex)) return cache[dirIndex];
if (has(modules, dirIndex)) return initModule(dirIndex, modules[dirIndex]);
throw new Error('Cannot find module "' + name + '"');
};
var define = function(bundle, fn) {
if (typeof bundle === 'object') {
for (var key in bundle) {
if (has(bundle, key)) {
modules[key] = bundle[key];
}
}
} else {
modules[bundle] = fn;
}
};
globals.require = require;
globals.require.define = define;
globals.require.register = define;
globals.require.brunch = true;
})();
(function() {
angular.module('scroll', []).value('$anchorScroll', angular.noop);
angular.module('app', ['ui', 'partials', 'app.controllers', 'hub.g0v.tw', 'ui.state', 'ui.bootstrap']).config(['$stateProvider', '$urlRouterProvider', '$locationProvider'].concat(function($stateProvider, $urlRouterProvider, $locationProvider){
$stateProvider.state('about', {
url: '/about',
templateUrl: '/partials/about.html'
}).state('people', {
url: '/people',
templateUrl: '/partials/people.html',
controller: 'PeopleCtrl',
onEnter: function(){
return $('body').addClass('people');
},
onExit: function(){
return $('body').removeClass('people');
}
}).state('tag', {
url: '/tag/{tag}',
templateUrl: '/partials/tag.html',
controller: 'TagControl',
onEnter: function(){
return $('body').addClass('people');
},
onExit: function(){
return $('body').removeClass('people');
}
}).state('hack', {
url: '/{hackId}',
templateUrl: '/partials/hack.html',
controller: 'HackFolderCtrl'
}).state('hack.doc', {
url: '/{docId}'
});
$urlRouterProvider.otherwise('/g0v-hackath3n');
return $locationProvider.html5Mode(true);
})).run(['$rootScope', '$state', '$stateParams', '$location'].concat(function($rootScope, $state, $stateParams, $location){
$rootScope.$state = $state;
$rootScope.$stateParam = $stateParams;
return $rootScope.go = function(it){
return $location.path(it);
};
}));
}).call(this);
(function() {
var slice$ = [].slice, replace$ = ''.replace;
angular.module('app.controllers', ['ui.state']).controller({
AppCtrl: ['$scope', '$location', '$rootScope', '$timeout'].concat(function(s, $location, $rootScope, $timeout){
s.$location = $location;
s.$watch('$location.path()', function(activeNavId){
activeNavId || (activeNavId = '/');
return s.activeNavId = activeNavId, s;
});
s.getClass = function(id){
if (s.activeNavId.substring(0, id.length === id)) {
return 'active';
} else {
return '';
}
};
return $timeout(function(){
return $rootScope.hideGithubRibbon = true;
}, 10 * 1000);
})
}).controller({
HackFolderCtrl: ['$scope', '$state', 'HackFolder'].concat(function($scope, $state, HackFolder){
var that;
import$($scope, {
hasViewMode: function(it){
return it.match(/g(doc|present|draw)/);
},
sortableOptions: {
update: function(){
return typeof console != 'undefined' && console !== null ? console.log('notyetupdated') : void 8;
}
},
iframes: HackFolder.iframes,
docs: HackFolder.docs,
tree: HackFolder.tree,
open: function(doc){
window.open(doc.url, doc.id);
return false;
},
activate: HackFolder.activate,
HackFolder: HackFolder,
iframeCallback: function(doc){
return function(status){
return $scope.$apply(function(){
if (typeof console != 'undefined' && console !== null) {
console.log('iframecb', status, doc);
}
if (status === 'fail') {
doc.noiframe = true;
} else {
doc.noiframe = false;
}
if (status === 'unsure') {
return doc.iframeunsure = true;
}
});
};
},
debug: function(it){
return typeof console != 'undefined' && console !== null ? console.log(it, this) : void 8;
},
reload: function(hackId){
return HackFolder.getIndex(hackId, true, function(){});
}
});
$scope.$watch('hackId', function(hackId){
return HackFolder.getIndex(hackId, false, function(){
var that, ref$;
$scope.$watch('docId', function(docId){
if (docId) {
return HackFolder.activate(docId);
}
});
if (!$scope.docId) {
if (that = (ref$ = HackFolder.docs[0]) != null ? ref$.id : void 8) {
return $state.transitionTo('hack.doc', {
docId: that,
hackId: $scope.hackId
});
}
}
});
});
$scope.hackId = (that = $state.params.hackId) ? that : 'g0v-hackath3n';
return $scope.$watch('$state.params.docId', function(docId){
if (docId) {
return $scope.docId = encodeURIComponent(encodeURIComponent(docId));
}
});
})
}).directive('resize', ['$window'].concat(function($window){
return function(scope, element, attrs){
var refreshSize;
refreshSize = function(){
scope.width = $window.innerWidth;
scope.height = $window.innerHeight;
return scope.contentHeight = $window.innerHeight - $(element).offset().top;
};
angular.element($window).bind('resize', function(){
return scope.$apply(refreshSize);
});
return refreshSize();
};
})).directive('ngxIframe', ['$parse'].concat(function($parse){
return {
link: function($scope, element, attrs){
var cb, dispatch, fail;
cb = $parse(attrs.ngxIframe)($scope);
dispatch = function(iframe, loading){
var ok;
ok = !(function(){
try {
return iframe.location == 'about:blank';
} catch (e$) {}
}());
if (loading && $.browser.mozilla) {
return cb('unsure');
} else {
return cb(ok ? 'ok' : 'fail');
}
};
$(element).load(function(){
clearTimeout(fail);
return dispatch(this.contentWindow, true);
});
return fail = setTimeout(function(){
return dispatch(element[0].contentWindow);
}, 5000);
}
};
})).directive('ngxNoclick', function(){
return function($scope, element, attrs){
return $(element).click(function(it){
it.preventDefault();
return false;
});
};
}).directive('ngxClickMeta', ['$parse'].concat(function($parse){
return {
link: function($scope, element, attrs){
var cb;
cb = $parse(attrs.ngxClickMeta);
return $(element).click(function(e){
if (e.metaKey) {
if (!cb($scope)) {
e.preventDefault();
return false;
}
}
});
}
};
})).directive('ngxFinal', function(){
return function($scope, element, attrs){
return $(element).click(function(it){
return it.stopPropagation();
});
};
}).factory({
HackFolder: ['$http'].concat(function($http){
var iframes, docs, tree, hackId, self;
iframes = {};
docs = [];
tree = [];
return self = {
iframes: iframes,
docs: docs,
tree: tree,
activate: function(id, edit){
var d, doc, type, i$, ref$, len$, t, that, ref1$, mode, src;
edit == null && (edit = false);
doc = (function(){
var i$, ref$, len$, results$ = [];
for (i$ = 0, len$ = (ref$ = docs).length; i$ < len$; ++i$) {
d = ref$[i$];
if (d.id === id) {
results$.push(d);
}
}
return results$;
}())[0], type = doc.type;
for (i$ = 0, len$ = (ref$ = tree).length; i$ < len$; ++i$) {
t = ref$[i$];
if (that = t != null ? (ref1$ = t.children) != null ? ref1$.map(fn$) : void 8 : void 8) {
if (in$(id, that)) {
t.expand = true;
}
}
}
mode = edit ? 'edit' : 'view';
src = (function(){
var ref$;
switch (ref$ = [type], false) {
case 'gdoc' !== ref$[0]:
return "https://docs.google.com/document/d/" + id + "/" + mode + "?pli=1&overridemobile=true";
case 'gsheet' !== ref$[0]:
return "https://docs.google.com/spreadsheet/ccc?key=" + id;
case 'gpresent' !== ref$[0]:
return "https://docs.google.com/presentation/d/" + id + "/" + mode;
case 'gdraw' !== ref$[0]:
return "https://docs.google.com/drawings/d/" + id + "/" + mode;
case 'gsheet' !== ref$[0]:
return "https://docs.google.com/spreadsheet/ccc?key=" + id;
case 'hackpad' !== ref$[0]:
return "https://" + ((ref$ = doc.site) != null ? ref$ : '') + "hackpad.com/ep/api/embed-pad?padId=" + id;
case 'ethercalc' !== ref$[0]:
return "https://ethercalc.org/" + id;
case 'url' !== ref$[0]:
return decodeURIComponent(decodeURIComponent(id));
default:
return '';
}
}());
if (doc.hashtag) {
src += doc.hashtag;
}
if (that = iframes[id]) {
return that.src = src, that.mode = mode, that;
} else {
return iframes[id] = {
src: src,
doc: doc,
mode: mode
};
}
function fn$(it){
return it.id;
}
},
getIndex: function(id, force, cb){
if (hackId === id && !force) {
return cb(docs);
}
return $http.get("https://www.ethercalc.org/_/" + id + "/csv").success(function(csv){
var folderTitle, entries, res$, i$, ref$, len$, line, ref1$, url, title, opts, tags, rest, _, prefix, hashtag, entry, that, ref2$, lastParent, nested, i, x$, y$;
hackId = id;
docs.length = 0;
res$ = [];
for (i$ = 0, len$ = (ref$ = csv.split(/\n/)).length; i$ < len$; ++i$) {
line = ref$[i$];
if (line) {
ref1$ = line.split(/,/), url = ref1$[0], title = ref1$[1], opts = ref1$[2], tags = ref1$[3], rest = slice$.call(ref1$, 4);
title = replace$.call(title, /^"|"$/g, '');
if (opts) {
opts = replace$.call(opts, /^"|"$/g, '');
}
if (opts) {
opts = opts.replace(/""/g, '"');
}
if (tags) {
tags = replace$.call(tags, /^"|"$/g, '');
}
ref1$ = url.match(/^"?(\s*)(\S+?)?(#\S+)?"?$/), _ = ref1$[0], prefix = ref1$[1], url = ref1$[2], hashtag = ref1$[3];
entry = import$({
hashtag: hashtag,
url: url,
title: title,
indent: prefix.length,
opts: (fn$())
}, (fn1$()));
if (entry.type === 'dummy' && !((ref1$ = entry.title) != null && ref1$.length)) {
res$.push(null);
} else {
res$.push(import$(import$({
icon: "/img/" + entry.type + ".png"
}, entry), {
tags: ((ref1$ = (ref2$ = entry.opts) != null ? ref2$.tags : void 8) != null
? ref1$
: []).concat(((ref1$ = tags != null ? tags.split(',') : void 8) != null
? ref1$
: []).filter(fn2$).map(fn3$))
}));
}
}
}
entries = res$;
docs.splice.apply(docs, [0, docs.length].concat(slice$.call(entries.filter(function(it){
return it != null;
}))));
lastParent = 0;
res$ = [];
for (i$ = 0, len$ = (ref$ = docs).length; i$ < len$; ++i$) {
i = i$;
entry = ref$[i$];
if (i > 0 && entry.indent) {
x$ = docs[lastParent];
y$ = (ref1$ = x$.children) != null
? ref1$
: x$.children = [];
y$.push(entry);
res$.push(null);
} else {
lastParent = i;
res$.push(entry);
}
}
nested = res$;
nested = nested.filter(function(it){
return it != null;
});
nested = nested.map(function(it){
var ref$, ref1$;
if (it.children) {
it.expand = (ref$ = (ref1$ = it.opts) != null ? ref1$.expand : void 8) != null
? ref$
: it.children.length < 5;
}
return it;
});
tree.splice.apply(tree, [0, tree.length].concat(slice$.call(nested)));
self.folderTitle = folderTitle;
return cb(docs);
function fn$(){
try {
return JSON.parse(opts != null ? opts : '{}');
} catch (e$) {}
}
function fn1$(){
var ref$;
switch (ref$ = [url], false) {
case void 8 !== ref$[0]:
if (!folderTitle) {
if (title) {
folderTitle = title;
title = null;
}
}
return {
title: title,
type: 'dummy',
id: 'dummy'
};
case !(that = /^https?:\/\/www\.ethercalc\.(?:com|org)\/(.*)/.exec(ref$[0])):
return {
type: 'ethercalc',
id: that[1]
};
case !(that = /https:\/\/docs\.google\.com\/document\/(?:d\/)?([^\/]+)\//.exec(ref$[0])):
return {
type: 'gdoc',
id: that[1]
};
case !(that = /https:\/\/docs\.google\.com\/spreadsheet\/ccc\?key=([^\/?&]+)/.exec(ref$[0])):
return {
type: 'gsheet',
id: that[1]
};
case !(that = /https:\/\/docs\.google\.com\/drawings\/(?:d\/)?([^\/]+)\//.exec(ref$[0])):
return {
type: 'gdraw',
id: that[1]
};
case !(that = /https:\/\/docs\.google\.com\/presentation\/(?:d\/)?([^\/]+)\//.exec(ref$[0])):
return {
type: 'gpresent',
id: that[1]
};
case !(that = /https?:\/\/(\w+\.)?hackpad\.com\/(?:.*?-)?([\w]+)(\#.*)?$/.exec(ref$[0])):
return {
type: 'hackpad',
site: that[1],
id: that[2]
};
case !(that = /^(https?:\/\/[^\/]+)/.exec(ref$[0])):
return {
type: 'url',
id: encodeURIComponent(encodeURIComponent(url)),
icon: "http://g.etfv.co/" + that[1]
};
default:
return typeof console != 'undefined' && console !== null ? console.log('unrecognized', url) : void 8;
}
}
function fn2$(it){
return it.length;
}
function fn3$(tag){
var ref$, _, content, c, rest;
ref$ = tag.match(/^(.*?)(?::(.*))?$/), _ = ref$[0], content = ref$[1], c = ref$[2], rest = slice$.call(ref$, 3);
return {
content: content,
'class': c != null ? c : 'warning'
};
}
});
}
};
})
});
function import$(obj, src){
var own = {}.hasOwnProperty;
for (var key in src) if (own.call(src, key)) obj[key] = src[key];
return obj;
}
function in$(x, arr){
var i = -1, l = arr.length >>> 0;
while (++i < l) if (x === arr[i] && i in arr) return true;
return false;
}
}).call(this);
(function() {
angular.module('hub.g0v.tw', ['ui.state', 'firebase']).controller({
TagControl: ['$scope', '$state', '$location', 'Hub'].concat(function($scope, $state, $location, Hub){
$scope.$watch('$state.params.tag', function(tag){
$scope.tag = tag;
return $scope.loadDisqus(tag);
});
return import$($scope, {
projects: [
{
name: '立法院'
}, {
name: 'meta'
}
],
people: Hub.people,
loadDisqus: function(tag){
var oldDsq;
if ($location.host() === 'localhost') {
return;
window.disqus_developer = 1;
}
window.disqus_shortname = 'g0vhub';
window.disqus_identifier = "tag-" + tag;
window.disqus_url = "http://hack.g0v.tw/tag/" + tag;
if (typeof DISQUS !== 'undefined') {
DISQUS.reset({
reload: true,
config: function(){
this.page.identifier = window.disqus_identifier;
return this.page.url = window.disqus_url;
}
});
}
oldDsq = document.getElementById('disqusCommentScript');
if (oldDsq) {
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).removeChild(oldDsq);
}
console.log('url', window.disqus_url);
// http://docs.disqus.com/developers/universal/
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://angularjs.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] ||
document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
return angular.element(document.getElementById('disqus_thread')).html('');
}
});
})
}).controller({
PeopleCtrl: ['$scope', '$state', 'Hub', 'angularFire'].concat(function($scope, $state, Hub, angularFire){
$scope.safeApply = function(fn){
var phase;
phase = $scope.$root.$$phase;
if (phase === '$apply' || phase === '$digest') {
return typeof fn === 'function' ? fn() : void 8;
} else {
return $scope.$apply(fn);
}
};
import$($scope, {
remove_tag: function(person, tag){
var t;
return person.tags = (function(){
var i$, ref$, len$, results$ = [];
for (i$ = 0, len$ = (ref$ = person.tags).length; i$ < len$; ++i$) {
t = ref$[i$];
if (t !== tag) {
results$.push(t);
}
}
return results$;
}());
},
add_tag: function(person){
person.tags == null && (person.tags = []);
person.tags.push($scope.newtag);
$scope.newtag = '';
return false;
},
projects: [
{
name: '立法院'
}, {
name: 'meta'
}
],
people: Hub.people,
auth: Hub.auth,
setUsername: Hub.setUsername,
loginAndMerge: Hub.loginAndMerge,
loginAndLink: Hub.loginAndLink
});
$scope.$on('event:auth-login', function(e, arg$){
var user;
user = arg$.user;
return $scope.safeApply(function(){
var promise;
promise = angularFire(Hub.root.child("people/" + user.username), $scope, 'user', {});
return $scope.toSetUsername = false;
});
});
$scope.$on('event:auth-logout', function(){
return $scope.safeApply(function(){
delete $scope.user;
return $scope.toSetUsername = false;
});
});
return $scope.$on('event:auth-userNameRequired', function(e, arg$){
var existing;
existing = arg$.existing;
return $scope.safeApply(function(){
var ref$;
$scope.toSetUsername = true;
$scope.usernameInUse = existing;
return $scope.newUsername = (ref$ = Hub.authUser) != null ? ref$.username : void 8;
});
});
})
}).factory({
Hub: ['$http', 'angularFireCollection', '$rootScope'].concat(function($http, angularFireCollection, $rootScope){
var url, myDataRef, people, self, checkUsername;
url = window.global.config.FIREBASE;
myDataRef = new Firebase(url);
people = angularFireCollection(myDataRef.child('people'));
self = {};
checkUsername = function(username, alwaysPrompt, cb){
return myDataRef.child("people/" + username).once('value', function(inuse){
var existing;
existing = inuse.val();
if (alwaysPrompt || existing) {
$rootScope.$broadcast('event:auth-userNameRequired', {
existing: existing
});
}
if (!existing) {
return typeof cb === 'function' ? cb() : void 8;
}
});
};
self.setUsername = function(username){
if (!self.authUser) {
return;
}
return checkUsername(username, false, function(){
var info, ref$, _, gravatar, x$;
info = (ref$ = {
displayName: self.authUser.displayName
}, ref$.tags = [], ref$.username = username, ref$);
info.avatar = (function(){
var ref$;
switch (ref$ = [self.authUser.provider], false) {
case 'github' !== ref$[0]:
ref$ = self.authUser.avatar_url.match(/https:\/\/secure.gravatar.com\/avatar\/(\w+)/), _ = ref$[0], gravatar = ref$[1];
return "http://avatars.io/gravatar/" + gravatar;
case 'twitter' !== ref$[0]:
return "http://avatars.io/twitter/" + self.authUser.username;
default:
return "http://avatars.io/" + self.authUser.provider + "/" + self.authUser.id;
}
}());
x$ = myDataRef;
x$.child("auth-map/" + self.authUser.provider + "/" + self.authUser.id).set({
username: username
});
x$.child("people/" + username).set(info);
x$.child("people/" + username + "/auth/" + self.authUser.provider).set({
id: (ref$ = self.authUser).id,
username: ref$.username
});
return myDataRef.child("people/" + username).once('value', function(loginUser){
self.loginUser = loginUser.val();
return $rootScope.$broadcast('event:auth-login', {
user: self.loginUser
});
});
});
};
self.loginAndMerge = function(provider){
self.authMerge = self.authUser;
return self.auth.login(provider);
};
self.loginAndLink = function(provider){
self.authLink = self.authUser;
self.authLinkUser = self.loginUser;
return self.auth.login(provider);
};
self.auth = new FirebaseAuthClient(myDataRef, function(error, user){
if (error) {
return console.log(error);
} else if (user) {
self.authUser = user;
return myDataRef.child("auth-map/" + user.provider + "/" + user.id).once('value', function(auth){
var ref$, username, entry, link, newauth, ref1$;
if ((ref$ = auth.val()) != null && (username = ref$.username, ref$)) {
entry = myDataRef.child("people/" + username);
return entry.once('value', function(loginUser){
var merge, newauth, ref$, ref1$;
if (merge = self.authMerge) {
newauth = (ref$ = {}, ref$[merge.provider + ""] = {
id: merge.id,
username: (ref1$ = merge.username) != null ? ref1$ : ''
}, ref$);
entry.child('auth').update(newauth);
myDataRef.child("auth-map/" + merge.provider + "/" + merge.id).set({
username: username
});
delete self.authMerge;
}
self.loginUser = loginUser.val();
return $rootScope.$broadcast('event:auth-login', {
user: self.loginUser
});
});
} else {
if (link = self.authLink) {
username = self.authLinkUser.username;
entry = myDataRef.child("people/" + username);
newauth = (ref$ = {}, ref$[user.provider + ""] = {
id: user.id,
username: (ref1$ = user.username) != null ? ref1$ : ''
}, ref$);
entry.child('auth').update(newauth);
myDataRef.child("auth-map/" + user.provider + "/" + user.id).set({
username: username
});
return ref$ = self.authLink, delete self.authLink, ref$;
} else {
return checkUsername(self.authUser.username, true);
}
}
});
} else {
return $rootScope.$broadcast('event:auth-logout');
}
});
return import$(self, {
root: myDataRef,
people: people
});
})
});
function import$(obj, src){
var own = {}.hasOwnProperty;
for (var key in src) if (own.call(src, key)) obj[key] = src[key];
return obj;
}
}).call(this);
(function() {
var module = {};
module.exports = {"FIREBASE":"https://g0vhub.firebaseio.com"};
if (!window.global)
window.global = {};
window.global['app\config'] = module.exports;
}).call(this);
(function() {
angular.element(document).ready(function(){
return angular.bootstrap(document, ['app']);
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment