Skip to content

Commit

Permalink
Use strict
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lorenzhs committed Sep 7, 2014
1 parent 8758cad commit a80db33
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 55 deletions.
9 changes: 6 additions & 3 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"browser": true,
"devel": true,
"globals": {
"angular": false,
"$": false,
"window": false,
"console": false
"weeChat": false,
"_": false,
"Notification": false,
"Favico": false
}
}
15 changes: 7 additions & 8 deletions js/connection.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
(function() {
'use strict';

var weechat = angular.module('weechat');

weechat.factory('connection',
Expand All @@ -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

Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -278,3 +276,4 @@ weechat.factory('connection',
requestNicklist: requestNicklist
};
}]);
})();
9 changes: 4 additions & 5 deletions js/filters.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,7 +16,6 @@ weechat.filter('toArray', function () {
});

weechat.filter('irclinky', ['$filter', function($filter) {
'use strict';
return function(text, target) {
if (!text) {
return text;
Expand All @@ -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;
Expand All @@ -51,3 +49,4 @@ weechat.filter('inlinecolour', ['$sce', function($sce) {
return $sce.trustAsHtml(text.replace(hexColourRegex, substitute));
};
}]);
})();
7 changes: 6 additions & 1 deletion js/glowingbear.js
Original file line number Diff line number Diff line change
@@ -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) {

Expand Down Expand Up @@ -642,3 +645,5 @@ weechat.config(['$routeProvider',
});
}
]);

})();
4 changes: 4 additions & 0 deletions js/handlers.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -205,3 +208,4 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific
};

}]);
})();
9 changes: 7 additions & 2 deletions js/inputbar.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
(function() {
'use strict';

var weechat = angular.module('weechat');

weechat.directive('inputBar', function() {
Expand All @@ -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
Expand Down Expand Up @@ -309,3 +313,4 @@ weechat.directive('inputBar', function() {
}]
};
});
})();
42 changes: 26 additions & 16 deletions js/irc-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@
* 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;
});
newList = _(newList).pluck('name');

return newList;
},
};

/**
* Completes a single nick.
Expand All @@ -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) {
Expand All @@ -39,7 +44,7 @@ var IrcUtils = {
});

return foundNick;
},
};

/**
* Get the next nick when iterating nicks.
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -82,7 +87,7 @@ var IrcUtils = {
}
return matchingNicks[at];
}
},
};

/**
* Nicks tab completion.
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -205,5 +210,10 @@ var IrcUtils = {

// completion not possible
return ret;
}
};
};

return {
'completeNick': completeNick
};
}]);
})();
7 changes: 6 additions & 1 deletion js/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -526,3 +530,4 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter)
delete(this.model.buffers[bufferId]);
};
}]);
})();
4 changes: 4 additions & 0 deletions js/plugin-directive.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
(function() {
'use strict';

var weechat = angular.module('weechat');

weechat.directive('plugin', ['$rootScope', function($rootScope) {
Expand Down Expand Up @@ -54,3 +57,4 @@ weechat.directive('plugin', ['$rootScope', function($rootScope) {
}]
};
}]);
})();
10 changes: 7 additions & 3 deletions js/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -394,3 +397,4 @@ plugins.factory('userPlugins', function() {


});
})();
Loading

0 comments on commit a80db33

Please sign in to comment.