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.
This commit is contained in:
Lorenz Hübschle-Schneider 2014-08-24 17:53:55 +01:00
parent 8758cad336
commit a80db339f8
12 changed files with 100 additions and 55 deletions

View file

@ -1,8 +1,11 @@
{ {
"browser": true,
"devel": true,
"globals": { "globals": {
"angular": false, "angular": false,
"$": false, "weeChat": false,
"window": false, "_": false,
"console": false "Notification": false,
"Favico": false
} }
} }

View file

@ -1,3 +1,6 @@
(function() {
'use strict';
var weechat = angular.module('weechat'); var weechat = angular.module('weechat');
weechat.factory('connection', weechat.factory('connection',
@ -7,7 +10,7 @@ weechat.factory('connection',
models, models,
ngWebsockets) { ngWebsockets) {
protocol = new weeChat.Protocol(); var protocol = new weeChat.Protocol();
// Takes care of the connection and websocket hooks // Takes care of the connection and websocket hooks
@ -120,7 +123,7 @@ weechat.factory('connection',
* Handles websocket disconnection * Handles websocket disconnection
*/ */
$log.info("Disconnected from relay"); $log.info("Disconnected from relay");
failCallbacks('disconnection'); ngWebsockets.failCallbacks('disconnection');
$rootScope.connected = false; $rootScope.connected = false;
$rootScope.$emit('relayDisconnect'); $rootScope.$emit('relayDisconnect');
if (ssl && evt.code === 1006) { if (ssl && evt.code === 1006) {
@ -142,16 +145,11 @@ weechat.factory('connection',
$rootScope.lastError = Date.now(); $rootScope.lastError = Date.now();
if (evt.type === "error" && this.readyState !== 1) { if (evt.type === "error" && this.readyState !== 1) {
failCallbacks('error'); ngWebsockets.failCallbacks('error');
$rootScope.errorMessage = true; $rootScope.errorMessage = true;
} }
}; };
protocol.setId = function(id, message) {
return '(' + id + ') ' + message;
};
try { try {
ngWebsockets.connect(url, ngWebsockets.connect(url,
protocol, protocol,
@ -278,3 +276,4 @@ weechat.factory('connection',
requestNicklist: requestNicklist requestNicklist: requestNicklist
}; };
}]); }]);
})();

View file

@ -1,8 +1,9 @@
(function() {
'use strict';
var weechat = angular.module('weechat'); var weechat = angular.module('weechat');
weechat.filter('toArray', function () { weechat.filter('toArray', function () {
'use strict';
return function (obj) { return function (obj) {
if (!(obj instanceof Object)) { if (!(obj instanceof Object)) {
return obj; return obj;
@ -15,7 +16,6 @@ weechat.filter('toArray', function () {
}); });
weechat.filter('irclinky', ['$filter', function($filter) { weechat.filter('irclinky', ['$filter', function($filter) {
'use strict';
return function(text, target) { return function(text, target) {
if (!text) { if (!text) {
return text; return text;
@ -37,8 +37,6 @@ weechat.filter('irclinky', ['$filter', function($filter) {
}]); }]);
weechat.filter('inlinecolour', ['$sce', function($sce) { weechat.filter('inlinecolour', ['$sce', function($sce) {
'use strict';
return function(text) { return function(text) {
if (!text) { if (!text) {
return text; return text;
@ -51,3 +49,4 @@ weechat.filter('inlinecolour', ['$sce', function($sce) {
return $sce.trustAsHtml(text.replace(hexColourRegex, substitute)); return $sce.trustAsHtml(text.replace(hexColourRegex, substitute));
}; };
}]); }]);
})();

View file

@ -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) { 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',
}); });
} }
]); ]);
})();

View file

@ -1,3 +1,6 @@
(function() {
'use strict';
var weechat = angular.module('weechat'); var weechat = angular.module('weechat');
weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notifications', function($rootScope, $log, models, plugins, notifications) { 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
}; };
}]); }]);
})();

View file

@ -1,3 +1,6 @@
(function() {
'use strict';
var weechat = angular.module('weechat'); var weechat = angular.module('weechat');
weechat.directive('inputBar', function() { weechat.directive('inputBar', function() {
@ -11,12 +14,13 @@ weechat.directive('inputBar', function() {
command: '=command' command: '=command'
}, },
controller: ['$rootScope', '$scope', '$element', '$log', 'connection', 'models', function($rootScope, controller: ['$rootScope', '$scope', '$element', '$log', 'connection', 'models', 'IrcUtils', function($rootScope,
$scope, $scope,
$element, //XXX do we need this? don't seem to be using it $element, //XXX do we need this? don't seem to be using it
$log, $log,
connection, //XXX we should eliminate this dependency and use signals instead connection, //XXX we should eliminate this dependency and use signals instead
models) { models,
IrcUtils) {
/* /*
* Returns the input element * Returns the input element
@ -309,3 +313,4 @@ weechat.directive('inputBar', function() {
}] }]
}; };
}); });
})();

View file

@ -2,14 +2,19 @@
* Portable utilities for IRC. * 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 * Get a new version of a nick list, sorted by last speaker
* *
* @param nickList Original nick list * @param nickList Original nick list
* @return Sorted nick list * @return Sorted nick list
*/ */
_ciNickList: function(nickList) { var _ciNickList = function(nickList) {
var newList = _(nickList).sortBy(function(nickObj) { var newList = _(nickList).sortBy(function(nickObj) {
return -nickObj.spokeAt; return -nickObj.spokeAt;
@ -17,7 +22,7 @@ var IrcUtils = {
newList = _(newList).pluck('name'); newList = _(newList).pluck('name');
return newList; return newList;
}, };
/** /**
* Completes a single nick. * Completes a single nick.
@ -26,7 +31,7 @@ var IrcUtils = {
* @param nickList Array of current nicks sorted for case insensitive searching * @param nickList Array of current nicks sorted for case insensitive searching
* @return Completed nick (null if not found) * @return Completed nick (null if not found)
*/ */
_completeSingleNick: function(candidate, nickList) { var _completeSingleNick = function(candidate, nickList) {
var foundNick = null; var foundNick = null;
nickList.some(function(nick) { nickList.some(function(nick) {
@ -39,7 +44,7 @@ var IrcUtils = {
}); });
return foundNick; return foundNick;
}, };
/** /**
* Get the next nick when iterating nicks. * Get the next nick when iterating nicks.
@ -49,7 +54,7 @@ var IrcUtils = {
* @param nickList Array of current nicks sorted for case insensitive searching * @param nickList Array of current nicks sorted for case insensitive searching
* @return Next nick (may be the same) * @return Next nick (may be the same)
*/ */
_nextNick: function(iterCandidate, currentNick, nickList) { var _nextNick = function(iterCandidate, currentNick, nickList) {
var matchingNicks = []; var matchingNicks = [];
var at = null; var at = null;
var lcIterCandidate = iterCandidate.toLowerCase(); var lcIterCandidate = iterCandidate.toLowerCase();
@ -63,7 +68,7 @@ var IrcUtils = {
if (lcCurrentNick === lcNick) { if (lcCurrentNick === lcNick) {
at = matchingNicks.length - 1; at = matchingNicks.length - 1;
} }
} }
/* Since we aren't sorted any more torhve disabled this: /* Since we aren't sorted any more torhve disabled this:
else if (matchingNicks.length > 0) { else if (matchingNicks.length > 0) {
// end of group, no need to check after this // end of group, no need to check after this
@ -82,7 +87,7 @@ var IrcUtils = {
} }
return matchingNicks[at]; return matchingNicks[at];
} }
}, };
/** /**
* Nicks tab completion. * Nicks tab completion.
@ -98,14 +103,14 @@ var IrcUtils = {
* foundNick: completed nick (or null if not possible) * foundNick: completed nick (or null if not possible)
* iterCandidate: current iterating candidate * iterCandidate: current iterating candidate
*/ */
completeNick: function(text, caretPos, iterCandidate, nickList, suf) { var completeNick = function(text, caretPos, iterCandidate, nickList, suf) {
var doIterate = (iterCandidate !== null); var doIterate = (iterCandidate !== null);
if (suf === null) { if (suf === null) {
suf = ':'; suf = ':';
} }
// new nick list to search in // new nick list to search in
var searchNickList = IrcUtils._ciNickList(nickList); var searchNickList = _ciNickList(nickList);
// text before and after caret // text before and after caret
var beforeCaret = text.substring(0, caretPos); var beforeCaret = text.substring(0, caretPos);
@ -126,7 +131,7 @@ var IrcUtils = {
if (m) { if (m) {
if (doIterate) { if (doIterate) {
// try iterating // try iterating
newNick = IrcUtils._nextNick(iterCandidate, m[1], searchNickList); newNick = _nextNick(iterCandidate, m[1], searchNickList);
beforeCaret = newNick + suf + ' '; beforeCaret = newNick + suf + ' ';
return { return {
text: beforeCaret + afterCaret, text: beforeCaret + afterCaret,
@ -144,7 +149,7 @@ var IrcUtils = {
m = beforeCaret.match(/^([a-zA-Z0-9_\\\[\]{}^`|-]+)$/); m = beforeCaret.match(/^([a-zA-Z0-9_\\\[\]{}^`|-]+)$/);
if (m) { if (m) {
// try completing // try completing
newNick = IrcUtils._completeSingleNick(m[1], searchNickList); newNick = _completeSingleNick(m[1], searchNickList);
if (newNick === null) { if (newNick === null) {
// no match // no match
return ret; return ret;
@ -167,7 +172,7 @@ var IrcUtils = {
if (m) { if (m) {
if (doIterate) { if (doIterate) {
// try iterating // try iterating
newNick = IrcUtils._nextNick(iterCandidate, m[2], searchNickList); newNick = _nextNick(iterCandidate, m[2], searchNickList);
beforeCaret = m[1] + newNick + ' '; beforeCaret = m[1] + newNick + ' ';
return { return {
text: beforeCaret + afterCaret, text: beforeCaret + afterCaret,
@ -185,7 +190,7 @@ var IrcUtils = {
m = beforeCaret.match(/^(.* )([a-zA-Z0-9_\\\[\]{}^`|-]+)$/); m = beforeCaret.match(/^(.* )([a-zA-Z0-9_\\\[\]{}^`|-]+)$/);
if (m) { if (m) {
// try completing // try completing
newNick = IrcUtils._completeSingleNick(m[2], searchNickList); newNick = _completeSingleNick(m[2], searchNickList);
if (newNick === null) { if (newNick === null) {
// no match // no match
return ret; return ret;
@ -205,5 +210,10 @@ var IrcUtils = {
// completion not possible // completion not possible
return ret; return ret;
} };
};
return {
'completeNick': completeNick
};
}]);
})();

View file

@ -2,6 +2,9 @@
* This file contains the weechat models and various * This file contains the weechat models and various
* helper methods to work with them. * helper methods to work with them.
*/ */
(function() {
'use strict';
var models = angular.module('weechatModels', []); var models = angular.module('weechatModels', []);
models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) { 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) { if (textEl.attrs.name !== null) {
textEl.classes.push('coa-' + textEl.attrs.name); textEl.classes.push('coa-' + textEl.attrs.name);
} }
var val;
for (var attr in textEl.attrs.override) { for (var attr in textEl.attrs.override) {
val = textEl.attrs.override[attr]; val = textEl.attrs.override[attr];
if (val) { if (val) {
@ -449,7 +453,7 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter)
if (key === 'id') { if (key === 'id') {
activeBuffer = this.model.buffers[bufferId]; activeBuffer = this.model.buffers[bufferId];
} }
else { else {
activeBuffer = _.find(this.model.buffers, function(buffer) { activeBuffer = _.find(this.model.buffers, function(buffer) {
if (buffer[key] === bufferId) { if (buffer[key] === bufferId) {
return buffer; return buffer;
@ -526,3 +530,4 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter)
delete(this.model.buffers[bufferId]); delete(this.model.buffers[bufferId]);
}; };
}]); }]);
})();

View file

@ -1,3 +1,6 @@
(function() {
'use strict';
var weechat = angular.module('weechat'); var weechat = angular.module('weechat');
weechat.directive('plugin', ['$rootScope', function($rootScope) { weechat.directive('plugin', ['$rootScope', function($rootScope) {
@ -54,3 +57,4 @@ weechat.directive('plugin', ['$rootScope', function($rootScope) {
}] }]
}; };
}]); }]);
})();

View file

@ -2,7 +2,10 @@
* This file contains the plugin definitions * 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 * Definition of a user provided plugin with sensible default values
@ -143,7 +146,7 @@ plugins.factory('userPlugins', function() {
document.body.appendChild(script); 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) { var urlPlugin = function(callback) {
return function(message) { return function(message) {
@ -168,7 +171,7 @@ plugins.factory('userPlugins', function() {
*/ */
var spotifyPlugin = new Plugin(function(message) { var spotifyPlugin = new Plugin(function(message) {
content = []; var content = [];
var addMatch = function(match) { var addMatch = function(match) {
for (var i = 0; match && i < match.length; i++) { for (var i = 0; match && i < match.length; i++) {
var id = match[i].substr(match[i].length - 22, match[i].length); var id = match[i].substr(match[i].length - 22, match[i].length);
@ -394,3 +397,4 @@ plugins.factory('userPlugins', function() {
}); });
})();

View file

@ -1,3 +1,6 @@
(function() {
'use strict';
var websockets = angular.module('ngWebsockets', []); var websockets = angular.module('ngWebsockets', []);
websockets.factory('ngWebsockets', websockets.factory('ngWebsockets',
@ -5,7 +8,7 @@ websockets.factory('ngWebsockets',
function($rootScope, $q) { function($rootScope, $q) {
this.protocol = null; var protocol = null;
var ws = null; var ws = null;
var callbacks = {}; var callbacks = {};
@ -17,7 +20,7 @@ function($rootScope, $q) {
* *
* @param reason reason for failure * @param reason reason for failure
*/ */
failCallbacks = function(reason) { var failCallbacks = function(reason) {
for (var i in callbacks) { for (var i in callbacks) {
callbacks[i].cb.reject(reason); callbacks[i].cb.reject(reason);
} }
@ -111,11 +114,11 @@ function($rootScope, $q) {
}; };
var connect = function(url, var connect = function(url,
protocol, protocol_,
properties) { properties) {
ws = new WebSocket(url); ws = new WebSocket(url);
protocol = protocol; protocol = protocol_;
for (var property in properties) { for (var property in properties) {
ws[property] = properties[property]; ws[property] = properties[property];
} }
@ -138,7 +141,9 @@ function($rootScope, $q) {
send: send, send: send,
sendAll: sendAll, sendAll: sendAll,
connect: connect, connect: connect,
disconnect: disconnect disconnect: disconnect,
failCallbacks: failCallbacks
}; };
}]); }]);
})();

View file

@ -1,4 +1,5 @@
(function(exports) {// http://weechat.org/files/doc/devel/weechat_dev.en.html#color_codes_in_strings (function(exports) {// http://weechat.org/files/doc/devel/weechat_dev.en.html#color_codes_in_strings
'use strict';
/** /**
* WeeChat protocol handling. * WeeChat protocol handling.
@ -604,16 +605,6 @@
return defaults; 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. * Formats a command.
* *
@ -966,7 +957,7 @@
var objs = []; var objs = [];
var hpath = this._getString(); var hpath = this._getString();
keys = this._getString().split(','); var keys = this._getString().split(',');
paths = hpath.split('/'); paths = hpath.split('/');
count = this._getInt(); count = this._getInt();
@ -1179,6 +1170,17 @@
this._data = data; 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. * Parses a WeeChat message.
* *