From 7ef9e631f17e55dbcf91b3a35eba946d3f88ccf3 Mon Sep 17 00:00:00 2001 From: Andrew Buss Date: Mon, 1 Jun 2015 23:32:41 -0700 Subject: [PATCH] allow static dir --- .gitignore | 1 - Makefile | 7 +- scripts/ResetPasswordController.js | 64 +++--- scripts/SettingsController.js | 8 +- scripts/StudyController.js | 2 +- static/js/angular-contenteditable.js | 98 +++++++++ static/js/angular-websocket.js | 382 +++++++++++++++++++++++++++++++++++ 7 files changed, 522 insertions(+), 40 deletions(-) create mode 100644 static/js/angular-contenteditable.js create mode 100644 static/js/angular-websocket.js diff --git a/.gitignore b/.gitignore index 5c21f74..5e149a3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ *~ venv* -static* *.pyc .idea* .*.swp diff --git a/Makefile b/Makefile index 87d902f..2e896bd 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ all: styles/materialize.css styles/flashier.css fixjsstyle -JS_SOURCES = config.js $(wildcard scripts/*{Controller,Service,Directive}.js) +JS_SOURCES = config.js $(wildcard scripts/*Controller.js) $(wildcard scripts/*Directive.js) $(wildcard scripts/*Service.js) + +print-%: + @echo '$*=$($*)' styles/materialize.css: .PHONY sassc sass/materialize.scss styles/materialize.css @@ -9,7 +12,7 @@ styles/flashier.css: sass/flashier.scss sassc sass/flashier.scss styles/flashier.css fixjsstyle: .PHONY - $(foreach sourcefile,$(JS_SOURCES),expand $(sourcefile) | sponge > $(sourcefile);) + $(foreach sourcefile,$(JS_SOURCES),expand $(sourcefile) | sponge $(sourcefile);) fixjsstyle --flagfile gjslint.conf config.js scripts/*{Controller,Directive,Service}.js .PHONY: diff --git a/scripts/ResetPasswordController.js b/scripts/ResetPasswordController.js index 566f255..552d7f1 100644 --- a/scripts/ResetPasswordController.js +++ b/scripts/ResetPasswordController.js @@ -15,38 +15,38 @@ controller('ResetPasswordController', ['$scope', '$state', '$http', '$timeout', }*/ console.log('RESETTING'); $scope.confirmResetPass = function() { - if ($scope.newPassword.length < 8) { - $scope.unacceptable = true; - return; - } - if ($scope.newPassword != $scope.confirmPassword) { - $scope.mismatch = true; - $scope.confirmPassword.$setPristine(); - console.log('mismatch'); - return; - } - /*password passes local tests*/ - $http.post('/api/reset_password/', JSON.stringify({ - 'uid': uid, - 'token': token, - 'new_password': $scope.newPassword - })) - .success(function(data) { - $scope.error = false; - $scope.success = true; - //$state.go('resetpasssuccess'); - $timeout(function($state) { - $state.go('login'); - }, 1000); - console.log(data); - }) - .error(function(data, status, header, config) { - $scope.error = true; - $scope.success = false; - $scope.mismatch = false; - $scope.unacceptable = false; - console.log(data); - }); + if ($scope.newPassword.length < 8) { + $scope.unacceptable = true; + return; + } + if ($scope.newPassword != $scope.confirmPassword) { + $scope.mismatch = true; + $scope.confirmPassword.$setPristine(); + console.log('mismatch'); + return; + } + /*password passes local tests*/ + $http.post('/api/reset_password/', JSON.stringify({ + 'uid': uid, + 'token': token, + 'new_password': $scope.newPassword + })) + .success(function(data) { + $scope.error = false; + $scope.success = true; + //$state.go('resetpasssuccess'); + $timeout(function($state) { + $state.go('login'); + }, 1000); + console.log(data); + }) + .error(function(data, status, header, config) { + $scope.error = true; + $scope.success = false; + $scope.mismatch = false; + $scope.unacceptable = false; + console.log(data); + }); }; $scope.cancelReset = function() { $state.go('login'); diff --git a/scripts/SettingsController.js b/scripts/SettingsController.js index 58e55b3..e611703 100644 --- a/scripts/SettingsController.js +++ b/scripts/SettingsController.js @@ -1,13 +1,13 @@ angular.module('flashy.SettingsController', ['ui.router']). - controller('SettingsController', function($scope) { + controller('SettingsController', function($scope) { - $scope.changePassword = function(oldPassword, newPassword, confirmedNewPassword) { + $scope.changePassword = function(oldPassword, newPassword, confirmedNewPassword) { - }; + }; - }); + }); diff --git a/scripts/StudyController.js b/scripts/StudyController.js index 8819565..26c0a5d 100644 --- a/scripts/StudyController.js +++ b/scripts/StudyController.js @@ -4,7 +4,7 @@ controller('StudyController', ['$scope', '$stateParams', '$state', '$http', 'Use function($scope, $stateParams, $state, $http, UserService) { console.log('Flashy study controller content in this file. also hell0'); sectionId = $stateParams.sectionId; - $scope.isParamOpen = true; + $scope.isParamOpen = true; $(document).ready(function() { $('.datepicker').pickadate({ diff --git a/static/js/angular-contenteditable.js b/static/js/angular-contenteditable.js new file mode 100644 index 0000000..49b59ee --- /dev/null +++ b/static/js/angular-contenteditable.js @@ -0,0 +1,98 @@ +/** + * @see http://docs.angularjs.org/guide/concepts + * @see http://docs.angularjs.org/api/ng.directive:ngModel.NgModelController + * @see https://github.com/angular/angular.js/issues/528#issuecomment-7573166 + */ + +angular.module('contenteditable', []) + .directive('contenteditable', ['$timeout', function($timeout) { return { + restrict: 'A', + require: '?ngModel', + link: function(scope, element, attrs, ngModel) { + // don't do anything unless this is actually bound to a model + if (!ngModel) { + return + } + + // options + var opts = {} + angular.forEach([ + 'stripBr', + 'noLineBreaks', + 'selectNonEditable', + 'moveCaretToEndOnChange', + ], function(opt) { + var o = attrs[opt] + opts[opt] = o && o !== 'false' + }) + + // view -> model + element.bind('input', function(e) { + scope.$apply(function() { + var html, html2, rerender + html = element.html() + rerender = false + if (opts.stripBr) { + html = html.replace(/
$/, '') + } + if (opts.noLineBreaks) { + html2 = html.replace(/
/g, '').replace(/
/g, '').replace(/<\/div>/g, '') + if (html2 !== html) { + rerender = true + html = html2 + } + } + ngModel.$setViewValue(html) + if (rerender) { + ngModel.$render() + } + if (html === '') { + // the cursor disappears if the contents is empty + // so we need to refocus + $timeout(function(){ + element[0].blur() + element[0].focus() + }) + } + }) + }) + + // model -> view + var oldRender = ngModel.$render + ngModel.$render = function() { + var el, el2, range, sel + if (!!oldRender) { + oldRender() + } + element.html(ngModel.$viewValue || '') + if (opts.moveCaretToEndOnChange) { + el = element[0] + range = document.createRange() + sel = window.getSelection() + if (el.childNodes.length > 0) { + el2 = el.childNodes[el.childNodes.length - 1] + range.setStartAfter(el2) + } else { + range.setStartAfter(el) + } + range.collapse(true) + sel.removeAllRanges() + sel.addRange(range) + } + } + if (opts.selectNonEditable) { + element.bind('click', function(e) { + var range, sel, target + target = e.toElement + if (target !== this && angular.element(target).attr('contenteditable') === 'false') { + range = document.createRange() + sel = window.getSelection() + range.setStartBefore(target) + range.setEndAfter(target) + sel.removeAllRanges() + sel.addRange(range) + } + }) + } + } + }}]); diff --git a/static/js/angular-websocket.js b/static/js/angular-websocket.js new file mode 100644 index 0000000..a42ce90 --- /dev/null +++ b/static/js/angular-websocket.js @@ -0,0 +1,382 @@ +(function() { + 'use strict'; + + var noop = angular.noop; + var objectFreeze = (Object.freeze) ? Object.freeze : noop; + var objectDefineProperty = Object.defineProperty; + var isString = angular.isString; + var isFunction = angular.isFunction; + var isDefined = angular.isDefined; + var isObject = angular.isObject; + var isArray = angular.isArray; + var forEach = angular.forEach; + var arraySlice = Array.prototype.slice; + // ie8 wat + if (!Array.prototype.indexOf) { + Array.prototype.indexOf = function(elt /*, from*/) { + var len = this.length >>> 0; + var from = Number(arguments[1]) || 0; + from = (from < 0) ? Math.ceil(from) : Math.floor(from); + if (from < 0) { + from += len; + } + + for (; from < len; from++) { + if (from in this && this[from] === elt) { return from; } + } + return -1; + }; + } + + // $WebSocketProvider.$inject = ['$rootScope', '$q', '$timeout', '$websocketBackend']; + function $WebSocketProvider($rootScope, $q, $timeout, $websocketBackend) { + + function $WebSocket(url, protocols, options) { + if (!options && isObject(protocols) && !isArray(protocols)) { + options = protocols; + protocols = undefined; + } + + this.protocols = protocols; + this.url = url || 'Missing URL'; + this.ssl = /(wss)/i.test(this.url); + + // this.binaryType = ''; + // this.extensions = ''; + // this.bufferedAmount = 0; + // this.trasnmitting = false; + // this.buffer = []; + + // TODO: refactor options to use isDefined + this.scope = options && options.scope || $rootScope; + this.rootScopeFailover = options && options.rootScopeFailover && true; + this.useApplyAsync = options && options.useApplyAsync || false; + this.initialTimeout = options && options.initialTimeout || 500; // 500ms + this.maxTimeout = options && options.maxTimeout || 5 * 60 * 1000; // 5 minutes + this.reconnectIfNotNormalClose = options && options.reconnectIfNotNormalClose || false; + + this._reconnectAttempts = 0; + this.sendQueue = []; + this.onOpenCallbacks = []; + this.onMessageCallbacks = []; + this.onErrorCallbacks = []; + this.onCloseCallbacks = []; + + objectFreeze(this._readyStateConstants); + + if (url) { + this._connect(); + } else { + this._setInternalState(0); + } + + } + + + $WebSocket.prototype._readyStateConstants = { + 'CONNECTING': 0, + 'OPEN': 1, + 'CLOSING': 2, + 'CLOSED': 3, + 'RECONNECT_ABORTED': 4 + }; + + $WebSocket.prototype._normalCloseCode = 1000; + + $WebSocket.prototype._reconnectableStatusCodes = [ + 4000 + ]; + + $WebSocket.prototype.safeDigest = function safeDigest(autoApply) { + if (autoApply && !this.scope.$$phase) { + this.scope.$digest(); + } + }; + + $WebSocket.prototype.bindToScope = function bindToScope(scope) { + var self = this; + if (scope) { + this.scope = scope; + if (this.rootScopeFailover) { + this.scope.$on('$destroy', function() { + self.scope = $rootScope; + }); + } + } + return self; + }; + + $WebSocket.prototype._connect = function _connect(force) { + if (force || !this.socket || this.socket.readyState !== this._readyStateConstants.OPEN) { + this.socket = $websocketBackend.create(this.url, this.protocols); + this.socket.onmessage = angular.bind(this, this._onMessageHandler); + this.socket.onopen = angular.bind(this, this._onOpenHandler); + this.socket.onerror = angular.bind(this, this._onErrorHandler); + this.socket.onclose = angular.bind(this, this._onCloseHandler); + } + }; + + $WebSocket.prototype.fireQueue = function fireQueue() { + while (this.sendQueue.length && this.socket.readyState === this._readyStateConstants.OPEN) { + var data = this.sendQueue.shift(); + + this.socket.send( + isString(data.message) ? data.message : JSON.stringify(data.message) + ); + data.deferred.resolve(); + } + }; + + $WebSocket.prototype.notifyOpenCallbacks = function notifyOpenCallbacks(event) { + for (var i = 0; i < this.onOpenCallbacks.length; i++) { + this.onOpenCallbacks[i].call(this, event); + } + }; + + $WebSocket.prototype.notifyCloseCallbacks = function notifyCloseCallbacks(event) { + for (var i = 0; i < this.onCloseCallbacks.length; i++) { + this.onCloseCallbacks[i].call(this, event); + } + }; + + $WebSocket.prototype.notifyErrorCallbacks = function notifyErrorCallbacks(event) { + for (var i = 0; i < this.onErrorCallbacks.length; i++) { + this.onErrorCallbacks[i].call(this, event); + } + }; + + $WebSocket.prototype.onOpen = function onOpen(cb) { + this.onOpenCallbacks.push(cb); + return this; + }; + + $WebSocket.prototype.onClose = function onClose(cb) { + this.onCloseCallbacks.push(cb); + return this; + }; + + $WebSocket.prototype.onError = function onError(cb) { + this.onErrorCallbacks.push(cb); + return this; + }; + + + $WebSocket.prototype.onMessage = function onMessage(callback, options) { + if (!isFunction(callback)) { + throw new Error('Callback must be a function'); + } + + if (options && isDefined(options.filter) && !isString(options.filter) && !(options.filter instanceof RegExp)) { + throw new Error('Pattern must be a string or regular expression'); + } + + this.onMessageCallbacks.push({ + fn: callback, + pattern: options ? options.filter : undefined, + autoApply: options ? options.autoApply : true + }); + return this; + }; + + $WebSocket.prototype._onOpenHandler = function _onOpenHandler(event) { + this._reconnectAttempts = 0; + this.notifyOpenCallbacks(event); + this.fireQueue(); + }; + + $WebSocket.prototype._onCloseHandler = function _onCloseHandler(event) { + this.notifyCloseCallbacks(event); + if ((this.reconnectIfNotNormalClose && event.code !== this._normalCloseCode) || this._reconnectableStatusCodes.indexOf(event.code) > -1) { + this.reconnect(); + } + }; + + $WebSocket.prototype._onErrorHandler = function _onErrorHandler(event) { + this.notifyErrorCallbacks(event); + }; + + $WebSocket.prototype._onMessageHandler = function _onMessageHandler(message) { + var pattern; + var self = this; + var currentCallback; + for (var i = 0; i < self.onMessageCallbacks.length; i++) { + currentCallback = self.onMessageCallbacks[i]; + pattern = currentCallback.pattern; + if (pattern) { + if (isString(pattern) && message.data === pattern) { + applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message); + } + else if (pattern instanceof RegExp && pattern.exec(message.data)) { + applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message); + } + } + else { + applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message); + } + } + + function applyAsyncOrDigest(callback, autoApply, args) { + args = arraySlice.call(arguments, 2); + if (self.useApplyAsync) { + self.scope.$applyAsync(function() { + callback.apply(self, args); + }); + } else { + callback.apply(self, args); + self.safeDigest(autoApply); + } + } + + }; + + $WebSocket.prototype.close = function close(force) { + if (force || !this.socket.bufferedAmount) { + this.socket.close(); + } + return this; + }; + + $WebSocket.prototype.send = function send(data) { + var deferred = $q.defer(); + var self = this; + var promise = cancelableify(deferred.promise); + + if (self.readyState === self._readyStateConstants.RECONNECT_ABORTED) { + deferred.reject('Socket connection has been closed'); + } + else { + self.sendQueue.push({ + message: data, + deferred: deferred + }); + self.fireQueue(); + } + + // Credit goes to @btford + function cancelableify(promise) { + promise.cancel = cancel; + var then = promise.then; + promise.then = function() { + var newPromise = then.apply(this, arguments); + return cancelableify(newPromise); + }; + return promise; + } + + function cancel(reason) { + self.sendQueue.splice(self.sendQueue.indexOf(data), 1); + deferred.reject(reason); + return self; + } + + if ($websocketBackend.isMocked && $websocketBackend.isMocked() && + $websocketBackend.isConnected(this.url)) { + this._onMessageHandler($websocketBackend.mockSend()); + } + + return promise; + }; + + $WebSocket.prototype.reconnect = function reconnect() { + this.close(); + + var backoffDelay = this._getBackoffDelay(++this._reconnectAttempts); + + var backoffDelaySeconds = backoffDelay / 1000; + console.log('Reconnecting in ' + backoffDelaySeconds + ' seconds'); + + $timeout(angular.bind(this, this._connect), backoffDelay); + + return this; + }; + // Exponential Backoff Formula by Prof. Douglas Thain + // http://dthain.blogspot.co.uk/2009/02/exponential-backoff-in-distributed.html + $WebSocket.prototype._getBackoffDelay = function _getBackoffDelay(attempt) { + var R = Math.random() + 1; + var T = this.initialTimeout; + var F = 2; + var N = attempt; + var M = this.maxTimeout; + + return Math.floor(Math.min(R * T * Math.pow(F, N), M)); + }; + + $WebSocket.prototype._setInternalState = function _setInternalState(state) { + if (Math.floor(state) !== state || state < 0 || state > 4) { + throw new Error('state must be an integer between 0 and 4, got: ' + state); + } + + // ie8 wat + if (!objectDefineProperty) { + this.readyState = state || this.socket.readyState; + } + this._internalConnectionState = state; + + + forEach(this.sendQueue, function(pending) { + pending.deferred.reject('Message cancelled due to closed socket connection'); + }); + }; + + // Read only .readyState + if (objectDefineProperty) { + objectDefineProperty($WebSocket.prototype, 'readyState', { + get: function() { + return this._internalConnectionState || this.socket.readyState; + }, + set: function() { + throw new Error('The readyState property is read-only'); + } + }); + } + + return function(url, protocols) { + return new $WebSocket(url, protocols); + }; + } + + // $WebSocketBackendProvider.$inject = ['$window', '$log']; + function $WebSocketBackendProvider($window, $log) { + this.create = function create(url, protocols) { + var match = /wss?:\/\//.exec(url); + var Socket, ws; + if (!match) { + throw new Error('Invalid url provided'); + } + + // CommonJS + if (typeof exports === 'object' && require) { + try { + ws = require('ws'); + Socket = (ws.Client || ws.client || ws); + } catch(e) {} + } + + // Browser + Socket = Socket || $window.WebSocket || $window.MozWebSocket; + + if (protocols) { + return new Socket(url, protocols); + } + + return new Socket(url); + }; + this.createWebSocketBackend = function createWebSocketBackend(url, protocols) { + $log.warn('Deprecated: Please use .create(url, protocols)'); + return this.create(url, protocols); + }; + } + + angular.module('ngWebSocket', []) + .factory('$websocket', ['$rootScope', '$q', '$timeout', '$websocketBackend', $WebSocketProvider]) + .factory('WebSocket', ['$rootScope', '$q', '$timeout', 'WebsocketBackend', $WebSocketProvider]) + .service('$websocketBackend', ['$window', '$log', $WebSocketBackendProvider]) + .service('WebSocketBackend', ['$window', '$log', $WebSocketBackendProvider]); + + + angular.module('angular-websocket', ['ngWebSocket']); + + if (typeof module === 'object' && typeof define !== 'function') { + module.exports = angular.module('ngWebSocket'); + } +}()); -- 1.9.1