From a80db339f8d2a2b1b712c72805a9ff7d854cd38e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Sun, 24 Aug 2014 17:53:55 +0100 Subject: [PATCH] Use strict Requires turning IrcUtils into an Angular service, because the global variable trick won't work with use strict. Reuse is still easily possible by removing the angular wrapping around it. --- .jshintrc | 9 ++++++--- js/connection.js | 15 +++++++-------- js/filters.js | 9 ++++----- js/glowingbear.js | 7 ++++++- js/handlers.js | 4 ++++ js/inputbar.js | 9 +++++++-- js/irc-utils.js | 42 ++++++++++++++++++++++++++---------------- js/models.js | 7 ++++++- js/plugin-directive.js | 4 ++++ js/plugins.js | 10 +++++++--- js/websockets.js | 15 ++++++++++----- js/weechat.js | 24 +++++++++++++----------- 12 files changed, 100 insertions(+), 55 deletions(-) diff --git a/.jshintrc b/.jshintrc index f096376..37932d6 100644 --- a/.jshintrc +++ b/.jshintrc @@ -1,8 +1,11 @@ { + "browser": true, + "devel": true, "globals": { "angular": false, - "$": false, - "window": false, - "console": false + "weeChat": false, + "_": false, + "Notification": false, + "Favico": false } } diff --git a/js/connection.js b/js/connection.js index 2945d4a..e9cc0e9 100644 --- a/js/connection.js +++ b/js/connection.js @@ -1,3 +1,6 @@ +(function() { +'use strict'; + var weechat = angular.module('weechat'); weechat.factory('connection', @@ -7,7 +10,7 @@ weechat.factory('connection', models, ngWebsockets) { - protocol = new weeChat.Protocol(); + var protocol = new weeChat.Protocol(); // Takes care of the connection and websocket hooks @@ -120,7 +123,7 @@ weechat.factory('connection', * Handles websocket disconnection */ $log.info("Disconnected from relay"); - failCallbacks('disconnection'); + ngWebsockets.failCallbacks('disconnection'); $rootScope.connected = false; $rootScope.$emit('relayDisconnect'); if (ssl && evt.code === 1006) { @@ -142,16 +145,11 @@ weechat.factory('connection', $rootScope.lastError = Date.now(); if (evt.type === "error" && this.readyState !== 1) { - failCallbacks('error'); + ngWebsockets.failCallbacks('error'); $rootScope.errorMessage = true; } }; - protocol.setId = function(id, message) { - return '(' + id + ') ' + message; - }; - - try { ngWebsockets.connect(url, protocol, @@ -278,3 +276,4 @@ weechat.factory('connection', requestNicklist: requestNicklist }; }]); +})(); diff --git a/js/filters.js b/js/filters.js index 418b91c..cecf1fe 100644 --- a/js/filters.js +++ b/js/filters.js @@ -1,8 +1,9 @@ +(function() { +'use strict'; + var weechat = angular.module('weechat'); weechat.filter('toArray', function () { - 'use strict'; - return function (obj) { if (!(obj instanceof Object)) { return obj; @@ -15,7 +16,6 @@ weechat.filter('toArray', function () { }); weechat.filter('irclinky', ['$filter', function($filter) { - 'use strict'; return function(text, target) { if (!text) { return text; @@ -37,8 +37,6 @@ weechat.filter('irclinky', ['$filter', function($filter) { }]); weechat.filter('inlinecolour', ['$sce', function($sce) { - 'use strict'; - return function(text) { if (!text) { return text; @@ -51,3 +49,4 @@ weechat.filter('inlinecolour', ['$sce', function($sce) { return $sce.trustAsHtml(text.replace(hexColourRegex, substitute)); }; }]); +})(); diff --git a/js/glowingbear.js b/js/glowingbear.js index d307890..3e0093c 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -1,4 +1,7 @@ -var weechat = angular.module('weechat', ['ngRoute', 'localStorage', 'weechatModels', 'plugins', 'ngSanitize', 'ngWebsockets', 'ngTouch']); +(function() { +'use strict'; + +var weechat = angular.module('weechat', ['ngRoute', 'localStorage', 'weechatModels', 'plugins', 'IrcUtils', 'ngSanitize', 'ngWebsockets', 'ngTouch']); weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', '$log', 'models', 'connection', 'notifications', 'utils', function ($rootScope, $scope, $store, $timeout, $log, models, connection, notifications, utils) { @@ -642,3 +645,5 @@ weechat.config(['$routeProvider', }); } ]); + +})(); diff --git a/js/handlers.js b/js/handlers.js index 4bd7247..cd2e8a7 100644 --- a/js/handlers.js +++ b/js/handlers.js @@ -1,3 +1,6 @@ +(function() { +'use strict'; + var weechat = angular.module('weechat'); weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notifications', function($rootScope, $log, models, plugins, notifications) { @@ -205,3 +208,4 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific }; }]); +})(); diff --git a/js/inputbar.js b/js/inputbar.js index a7fa229..1d21a2e 100644 --- a/js/inputbar.js +++ b/js/inputbar.js @@ -1,3 +1,6 @@ +(function() { +'use strict'; + var weechat = angular.module('weechat'); weechat.directive('inputBar', function() { @@ -11,12 +14,13 @@ weechat.directive('inputBar', function() { command: '=command' }, - controller: ['$rootScope', '$scope', '$element', '$log', 'connection', 'models', function($rootScope, + controller: ['$rootScope', '$scope', '$element', '$log', 'connection', 'models', 'IrcUtils', function($rootScope, $scope, $element, //XXX do we need this? don't seem to be using it $log, connection, //XXX we should eliminate this dependency and use signals instead - models) { + models, + IrcUtils) { /* * Returns the input element @@ -309,3 +313,4 @@ weechat.directive('inputBar', function() { }] }; }); +})(); diff --git a/js/irc-utils.js b/js/irc-utils.js index 11e2465..6c596d5 100644 --- a/js/irc-utils.js +++ b/js/irc-utils.js @@ -2,14 +2,19 @@ * Portable utilities for IRC. */ -var IrcUtils = { +(function() { +'use strict'; + +var IrcUtils = angular.module('IrcUtils', []); + +IrcUtils.service('IrcUtils', [function() { /** * Get a new version of a nick list, sorted by last speaker * * @param nickList Original nick list * @return Sorted nick list */ - _ciNickList: function(nickList) { + var _ciNickList = function(nickList) { var newList = _(nickList).sortBy(function(nickObj) { return -nickObj.spokeAt; @@ -17,7 +22,7 @@ var IrcUtils = { newList = _(newList).pluck('name'); return newList; - }, + }; /** * Completes a single nick. @@ -26,7 +31,7 @@ var IrcUtils = { * @param nickList Array of current nicks sorted for case insensitive searching * @return Completed nick (null if not found) */ - _completeSingleNick: function(candidate, nickList) { + var _completeSingleNick = function(candidate, nickList) { var foundNick = null; nickList.some(function(nick) { @@ -39,7 +44,7 @@ var IrcUtils = { }); return foundNick; - }, + }; /** * Get the next nick when iterating nicks. @@ -49,7 +54,7 @@ var IrcUtils = { * @param nickList Array of current nicks sorted for case insensitive searching * @return Next nick (may be the same) */ - _nextNick: function(iterCandidate, currentNick, nickList) { + var _nextNick = function(iterCandidate, currentNick, nickList) { var matchingNicks = []; var at = null; var lcIterCandidate = iterCandidate.toLowerCase(); @@ -63,7 +68,7 @@ var IrcUtils = { if (lcCurrentNick === lcNick) { at = matchingNicks.length - 1; } - } + } /* Since we aren't sorted any more torhve disabled this: else if (matchingNicks.length > 0) { // end of group, no need to check after this @@ -82,7 +87,7 @@ var IrcUtils = { } return matchingNicks[at]; } - }, + }; /** * Nicks tab completion. @@ -98,14 +103,14 @@ var IrcUtils = { * foundNick: completed nick (or null if not possible) * iterCandidate: current iterating candidate */ - completeNick: function(text, caretPos, iterCandidate, nickList, suf) { + var completeNick = function(text, caretPos, iterCandidate, nickList, suf) { var doIterate = (iterCandidate !== null); if (suf === null) { suf = ':'; } // new nick list to search in - var searchNickList = IrcUtils._ciNickList(nickList); + var searchNickList = _ciNickList(nickList); // text before and after caret var beforeCaret = text.substring(0, caretPos); @@ -126,7 +131,7 @@ var IrcUtils = { if (m) { if (doIterate) { // try iterating - newNick = IrcUtils._nextNick(iterCandidate, m[1], searchNickList); + newNick = _nextNick(iterCandidate, m[1], searchNickList); beforeCaret = newNick + suf + ' '; return { text: beforeCaret + afterCaret, @@ -144,7 +149,7 @@ var IrcUtils = { m = beforeCaret.match(/^([a-zA-Z0-9_\\\[\]{}^`|-]+)$/); if (m) { // try completing - newNick = IrcUtils._completeSingleNick(m[1], searchNickList); + newNick = _completeSingleNick(m[1], searchNickList); if (newNick === null) { // no match return ret; @@ -167,7 +172,7 @@ var IrcUtils = { if (m) { if (doIterate) { // try iterating - newNick = IrcUtils._nextNick(iterCandidate, m[2], searchNickList); + newNick = _nextNick(iterCandidate, m[2], searchNickList); beforeCaret = m[1] + newNick + ' '; return { text: beforeCaret + afterCaret, @@ -185,7 +190,7 @@ var IrcUtils = { m = beforeCaret.match(/^(.* )([a-zA-Z0-9_\\\[\]{}^`|-]+)$/); if (m) { // try completing - newNick = IrcUtils._completeSingleNick(m[2], searchNickList); + newNick = _completeSingleNick(m[2], searchNickList); if (newNick === null) { // no match return ret; @@ -205,5 +210,10 @@ var IrcUtils = { // completion not possible return ret; - } -}; + }; + + return { + 'completeNick': completeNick + }; +}]); +})(); diff --git a/js/models.js b/js/models.js index 07b78c3..e8710f6 100644 --- a/js/models.js +++ b/js/models.js @@ -2,6 +2,9 @@ * This file contains the weechat models and various * helper methods to work with them. */ +(function() { +'use strict'; + var models = angular.module('weechatModels', []); models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) { @@ -282,6 +285,7 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) if (textEl.attrs.name !== null) { textEl.classes.push('coa-' + textEl.attrs.name); } + var val; for (var attr in textEl.attrs.override) { val = textEl.attrs.override[attr]; if (val) { @@ -449,7 +453,7 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) if (key === 'id') { activeBuffer = this.model.buffers[bufferId]; } - else { + else { activeBuffer = _.find(this.model.buffers, function(buffer) { if (buffer[key] === bufferId) { return buffer; @@ -526,3 +530,4 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) delete(this.model.buffers[bufferId]); }; }]); +})(); diff --git a/js/plugin-directive.js b/js/plugin-directive.js index 62ea463..4a60af4 100644 --- a/js/plugin-directive.js +++ b/js/plugin-directive.js @@ -1,3 +1,6 @@ +(function() { +'use strict'; + var weechat = angular.module('weechat'); weechat.directive('plugin', ['$rootScope', function($rootScope) { @@ -54,3 +57,4 @@ weechat.directive('plugin', ['$rootScope', function($rootScope) { }] }; }]); +})(); diff --git a/js/plugins.js b/js/plugins.js index d5f1bc4..7c749cd 100644 --- a/js/plugins.js +++ b/js/plugins.js @@ -2,7 +2,10 @@ * This file contains the plugin definitions */ -plugins = angular.module('plugins', []); +(function() { +'use strict'; + +var plugins = angular.module('plugins', []); /* * Definition of a user provided plugin with sensible default values @@ -143,7 +146,7 @@ plugins.factory('userPlugins', function() { document.body.appendChild(script); }; - var urlRegexp = RegExp(/(?:ftp|https?):\/\/\S*[^\s.;,(){}<>]/g); + var urlRegexp = new RegExp(/(?:ftp|https?):\/\/\S*[^\s.;,(){}<>]/g); var urlPlugin = function(callback) { return function(message) { @@ -168,7 +171,7 @@ plugins.factory('userPlugins', function() { */ var spotifyPlugin = new Plugin(function(message) { - content = []; + var content = []; var addMatch = function(match) { for (var i = 0; match && i < match.length; i++) { var id = match[i].substr(match[i].length - 22, match[i].length); @@ -394,3 +397,4 @@ plugins.factory('userPlugins', function() { }); +})(); diff --git a/js/websockets.js b/js/websockets.js index 598da14..939b31a 100644 --- a/js/websockets.js +++ b/js/websockets.js @@ -1,3 +1,6 @@ +(function() { +'use strict'; + var websockets = angular.module('ngWebsockets', []); websockets.factory('ngWebsockets', @@ -5,7 +8,7 @@ websockets.factory('ngWebsockets', function($rootScope, $q) { - this.protocol = null; + var protocol = null; var ws = null; var callbacks = {}; @@ -17,7 +20,7 @@ function($rootScope, $q) { * * @param reason reason for failure */ - failCallbacks = function(reason) { + var failCallbacks = function(reason) { for (var i in callbacks) { callbacks[i].cb.reject(reason); } @@ -111,11 +114,11 @@ function($rootScope, $q) { }; var connect = function(url, - protocol, + protocol_, properties) { ws = new WebSocket(url); - protocol = protocol; + protocol = protocol_; for (var property in properties) { ws[property] = properties[property]; } @@ -138,7 +141,9 @@ function($rootScope, $q) { send: send, sendAll: sendAll, connect: connect, - disconnect: disconnect + disconnect: disconnect, + failCallbacks: failCallbacks }; }]); +})(); diff --git a/js/weechat.js b/js/weechat.js index c2698fd..126eda6 100644 --- a/js/weechat.js +++ b/js/weechat.js @@ -1,4 +1,5 @@ (function(exports) {// http://weechat.org/files/doc/devel/weechat_dev.en.html#color_codes_in_strings +'use strict'; /** * WeeChat protocol handling. @@ -604,16 +605,6 @@ return defaults; }; - /** - * Add the ID to the previously formatted command - * - * @param id Command ID - * @param command previously formatted command - */ - WeeChatProtocol.setId = function(id, command) { - return '(' + id + ') ' + command; - }; - /** * Formats a command. * @@ -966,7 +957,7 @@ var objs = []; var hpath = this._getString(); - keys = this._getString().split(','); + var keys = this._getString().split(','); paths = hpath.split('/'); count = this._getInt(); @@ -1179,6 +1170,17 @@ this._data = data; }, + + /** + * Add the ID to the previously formatted command + * + * @param id Command ID + * @param command previously formatted command + */ + setId: function(id, command) { + return '(' + id + ') ' + command; + }, + /** * Parses a WeeChat message. *