Преглед на файлове

Starting to wire up the server

Nathaniel van Diepen преди 10 години
родител
ревизия
f3fe8c5433
променени са 6 файла, в които са добавени 158 реда и са изтрити 10 реда
  1. 3 0
      .gitmodules
  2. 19 0
      server/OmnomIRC.js
  3. 1 0
      server/node_modules/socket.io
  4. 1 0
      www/index.html
  5. 82 0
      www/js/jquery.storage.js
  6. 52 10
      www/js/omnomirc.js

+ 3 - 0
.gitmodules

@@ -0,0 +1,3 @@
+[submodule "server/node_modules/socket.io"]
+	path = server/node_modules/socket.io
+	url = https://github.com/LearnBoost/socket.io.git

+ 19 - 0
server/OmnomIRC.js

@@ -0,0 +1,19 @@
+var io = require('socket.io').listen(9000);
+io.sockets.on('connection',function(socket){
+	socket.on('join',function(data){
+		socket.join(data.name);
+		io.sockets.in(data.name).emit('message',{
+			message: ' joined the channel',
+			room: data.name,
+			from: 0
+		});
+	});
+	socket.on('part',function(data){
+		socket.leave(data.name);
+		io.sockets.in(data.name).emit('message',{
+			message: ' parted the channel',
+			room: data.name,
+			from: 0
+		});
+	});
+});

+ 1 - 0
server/node_modules/socket.io

@@ -0,0 +1 @@
+Subproject commit 64f6b244b6bd79880ba2e0ba00778a2309b39d0b

+ 1 - 0
www/index.html

@@ -10,6 +10,7 @@
 		<script type="text/javascript" src="js/jquery.ui.position.js"></script>
 		<script type="text/javascript" src="js/jquery.contextMenu.js"></script>
 		<script type="text/javascript" src="js/jquery.hammer.js"></script>
+		<script type="text/javascript" src="js/jquery.storage.js"></script>
 		<script type="text/javascript" src="js/omnomirc.js"></script>
 		<link rel="stylesheet" href="css/style.css"></link>
 		<link rel="stylesheet" href="css/jquery.contextMenu.css"></link>

+ 82 - 0
www/js/jquery.storage.js

@@ -0,0 +1,82 @@
+/*!
+ * jquery.storage.js 0.0.3 - https://github.com/yckart/jquery.storage.js
+ * The client-side storage for every browser, on any device.
+ *
+ * Copyright (c) 2012 Yannick Albert (http://yckart.com)
+ * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
+ * 2013/02/10
+ **/
+;(function($, window, document) {
+    'use strict';
+
+    $.map(['localStorage', 'sessionStorage'], function( method ) {
+        var defaults = {
+            cookiePrefix : 'fallback:' + method + ':',
+            cookieOptions : {
+                path : '/',
+                domain : document.domain,
+                expires : ('localStorage' === method) ? { expires: 365 } : undefined
+            }
+        };
+
+        try {
+            $.support[method] = method in window && window[method] !== null;
+        } catch (e) {
+            $.support[method] = false;
+        }
+
+        $[method] = function(key, value) {
+            var options = $.extend({}, defaults, $[method].options);
+
+            this.getItem = function( key ) {
+                var returns = function(key){
+                    return JSON.parse($.support[method] ? window[method].getItem(key) : $.cookie(options.cookiePrefix + key));
+                };
+                if(typeof key === 'string') return returns(key);
+
+                var arr = [],
+                    i = key.length;
+                while(i--) arr[i] = returns(key[i]);
+                return arr;
+            };
+
+            this.setItem = function( key, value ) {
+                value = JSON.stringify(value);
+                return $.support[method] ? window[method].setItem(key, value) : $.cookie(options.cookiePrefix + key, value, options.cookieOptions);
+            };
+
+            this.removeItem = function( key ) {
+                return $.support[method] ? window[method].removeItem(key) : $.cookie(options.cookiePrefix + key, null, $.extend(options.cookieOptions, {
+                    expires: -1
+                }));
+            };
+
+            this.clear = function() {
+                if($.support[method]) {
+                    return window[method].clear();
+                } else {
+                    var reg = new RegExp('^' + options.cookiePrefix, ''),
+                        opts = $.extend(options.cookieOptions, {
+                            expires: -1
+                        });
+
+                    if(document.cookie && document.cookie !== ''){
+                        $.map(document.cookie.split(';'), function( cookie ){
+                            if(reg.test(cookie = $.trim(cookie))) {
+                                 $.cookie( cookie.substr(0,cookie.indexOf('=')), null, opts);
+                            }
+                        });
+                    }
+                }
+            };
+
+            if (typeof key !== "undefined") {
+                return typeof value !== "undefined" ? ( value === null ? this.removeItem(key) : this.setItem(key, value) ) : this.getItem(key);
+            }
+
+            return this;
+        };
+
+        $[method].options = defaults;
+    });
+}(jQuery, window, document));

+ 52 - 10
www/js/omnomirc.js

@@ -17,7 +17,7 @@
 	You should have received a copy of the GNU General Public License
 	along with OmnomIRC.  If not, see <http://www.gnu.org/licenses/>.
 */
-(function(window,$,undefined){
+(function(window,$,io,undefined){
 	var $o = window.OmnomIRC = window.$o = function(){
 			return 'Version: '+$o.version
 		},
@@ -48,7 +48,12 @@
 		properties = {
 			nick: 'User',
 			sig: '',
-			tabs: tabs
+			tabs: tabs,
+			server: location.origin,
+			autojoin: [
+				'#omnimaga',
+				'#irp'
+			]
 		},
 		commands = [
 			{
@@ -98,15 +103,47 @@
 				}
 			}
 		],
-		$i,$s,$h,$cl,$tl,hht;
+		handles = [
+			{
+				on: 'connect',
+				fn: function(){
+					for(var i in settings.autojoin){
+						socket.emit('join',{name: settings.autojoin[i]});
+					}
+				}
+			},
+			{
+				on: 'join',
+				fn: function(data){
+					$o.addTab(data.name,data.title);
+				}
+			}
+		],
+		socket,$i,$s,$h,$cl,$tl,hht;
 	$.extend($o,{
 		version: '3.0',
+		connect: function(server){
+			if($o.connected()){
+				socket.disconnect();
+			}
+			if(typeof server == 'undefined'){
+				server = settings.server;
+			}
+			socket = io.connect(server);
+			for(var i in handles){
+				socket.on(handles[i].on,handles[i].fn);
+			}
+		},
+		connected: function(){
+			return typeof socket != 'undefined';
+		},
 		get: function(name){
 			return exists(settings[name])?settings[name]:false;
 		},
 		set: function(name,value){
 			if(exists(settings[name])){
 				settings[name] = value;
+				$.localStorage('settings',$.stringify(settings));
 				return true;
 			}else{
 				return false;
@@ -173,10 +210,12 @@
 			$tl.append($o.tabObj(tabs.length-1));
 		},
 		removeTab: function(id){
-			event('Tab removed: '+tabs[id].name);
-			tabs.splice(id,1);
-			if(selectedTab==id&&selectedTab>0){
-				selectedTab--;
+			if(typeof tabs[id] != 'undefined'){
+				event('Tab removed: '+tabs[id].name);
+				tabs.splice(id,1);
+				if(selectedTab==id&&selectedTab>0){
+					selectedTab--;
+				}
 			}
 			$o.refreshTabs();
 		},
@@ -246,6 +285,8 @@
 		return this.replace(/&/g, '&amp;').replace(/\s/g, '&nbsp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
 	};
 	$(document).ready(function(){
+		$.extend(settings,$.parseJSON($.localStorage('settings')));
+		$.localStorage('settings',$.stringify(settings));
 		$i = $('#input');
 		$s = $('#send');
 		$cl = $('#content-list');
@@ -371,13 +412,13 @@
 			}
 		})();
 		//DEBUG
-		for(var i=0;i<20;i++){
+		/* for(var i=0;i<20;i++){
 			tabs.push({
 				name: '#Tab'+i,
 				title: 'Tab '+i,
 				topic: 'Topic for tab '+i
 			});
-		}
+		} */
 		//END DEBUG
 		$o.refreshTabs();
 		event('Date '+new Date,'ready');
@@ -385,5 +426,6 @@
 		setTimeout(function(){
 			$h.removeClass('hovered');
 		},1000);
+		$o.connect();
 	});
-})(window,jQuery);
+})(window,jQuery,io);