orator 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/orator/base.rb CHANGED
@@ -42,7 +42,7 @@ module Orator
42
42
  # A map of the events that determine how this class will respond.
43
43
  #
44
44
  # @return [Hash]
45
- # @see [ClassMethods::event_list]
45
+ # @see [::event_list]
46
46
  def __event_list
47
47
  self.class.event_list
48
48
  end
@@ -1,5 +1,5 @@
1
1
  module Orator
2
2
 
3
3
  # Version of Orator.
4
- VERSION = '0.3.2'
4
+ VERSION = '0.3.3'
5
5
  end
data/lib/orator.rb CHANGED
@@ -12,7 +12,7 @@ require 'orator/version'
12
12
  require 'orator/event_handler'
13
13
  require 'orator/middle_ground'
14
14
 
15
- require 'orator/engine' if defined? Rails
15
+ require 'orator/engine' if defined?(Rails)
16
16
 
17
17
  module Orator
18
18
 
@@ -0,0 +1,166 @@
1
+ ###
2
+ # Handles creating logs for use in the element.
3
+ ###
4
+ class Orator.Libraries.ChatLogger
5
+
6
+ @DEFAULT_OPTIONS:
7
+ format: """
8
+ <{{element}} class='{{classes}}'>
9
+ <span class='sender'>{{from}}</span>
10
+ <span class='body'>{{-formatted_body}}</span>
11
+ </{{element}}>
12
+ """
13
+ format_maps: {}
14
+ format_field: 'event'
15
+ element: $("<ul/>")
16
+
17
+ ###
18
+ # The constructor. This takes a signle options object. This is merged with
19
+ # the default options and set as the class's options.
20
+ ###
21
+ constructor: (options)->
22
+ @options = $.extend(true, {}, Orator.Libraries.ChatLoger.DEFAULT_OPTIONS, options)
23
+ @format_maps = @options.format_maps || {}
24
+
25
+ format_maps: {}
26
+
27
+ ###
28
+ # This creates a new chat log and returns it. The argument is an object
29
+ # that will be used as options for the ChatLog.
30
+ ###
31
+ log: (options)->
32
+ new ChatLog(options, @options)
33
+
34
+ ###
35
+ # This appends the log to the element specified in the options. The argument
36
+ # is an object that will be used as the options for ChatLog.
37
+ ###
38
+ append: (options)->
39
+ @log(options).to(@options.element)
40
+
41
+ ###
42
+ # Handles formatting the logs for appending to an element.
43
+ ###
44
+ class ChatLog
45
+
46
+ ###
47
+ # The default options to be used by the constructor to merge the given
48
+ # optiosn to.
49
+ ###
50
+ @DEFAULT_OPTIONS:
51
+ # The tag that the element will use.
52
+ tag: 'li'
53
+ # Data to format the body with.
54
+ data: {}
55
+ # The body to put in the log.
56
+ body: null
57
+ # The sender of the log. Defaults to "(unknown)".
58
+ sender: null
59
+ # The classes the log should have.
60
+ class: ''
61
+
62
+ ###
63
+ # Constructor for the ChatLog. The first argument is an object of user
64
+ # defined options, and the second one is the options from the ChatLogger.
65
+ ###
66
+ constructor: (options, chat_logger_options)->
67
+ @options = $.extend({}, ChatLog.DEFAULT_OPTIONS, options)
68
+ @cl_opts = chat_logger_options
69
+
70
+ ###
71
+ # Appends this ChatLog to the element passed by the first parameter.
72
+ # Formats the body string, and the format string.
73
+ #
74
+ # The format base (@see ChatLogger.DEFAULT_OPTIONS) can access any data
75
+ # from the options of this class using {{moustache tags}}. The body can
76
+ # acess any data from the options' data element; if the data is empty,
77
+ # the body is escaped and passed through.
78
+ #
79
+ # @see _format for more information on formatting.
80
+ ###
81
+ to: (element)->
82
+ @options.formatted_body = _format(@_get_body(), @options.data)
83
+ @options.from = @options.sender || @options.data.name || "(unknown)"
84
+ @options.classes = @_define_classes()
85
+ _format(@cl_opts.format, @options)
86
+
87
+ ###
88
+ # Defines the classes to be used by the element.
89
+ ###
90
+ _define_classes: ->
91
+ str_buf = new Orator.Libraries.StrBuf
92
+
93
+ str_buf.append(@_escape(@options.class), ' ')
94
+ if @options.data.history then str_buf.append("history")
95
+ str_buf.to_s()
96
+
97
+ ###
98
+ # Formats a given string `str` with the data from `data`, which are the
99
+ # first and second arguments, respectively. It replaces stuff from data
100
+ # that match the {{moustache}} format. It will check to see if `with`
101
+ # contains the element named by the {{moustache}}. If it does, it will
102
+ # escape the data and replace the {{moustache}} with it. If it doesn't,
103
+ # it will check to see if it starts with a `-` and if an element matches
104
+ # the rest of the name. If it does, it will replace the moustache with
105
+ # the data (unescaped). Otherwise, it will leave the {{moustache}} alone.
106
+ ###
107
+ _format: (str, data)->
108
+ return @_escape(str) if $.isEmptyObject(data)
109
+
110
+ str.replace /\{\{(-?.+?)\}\}/g, (_, name)=>
111
+ if data[name] isnt undefined
112
+ @_escape(data[name])
113
+ else if name[0] == '-' and data[name.slice(1)] isnt undefined
114
+ data[name.slice(1)]
115
+ else
116
+ _
117
+
118
+ ###
119
+ # Gets the body for use by the formatter. If the body was provided in the
120
+ # options, return that. Otherwise, try to look up the format in the
121
+ # format maps provided by the options from the ChatLogger. If it can't
122
+ # find it, returns an empty string.
123
+ ###
124
+ _get_body: ->
125
+ return @options.body if @options.body
126
+
127
+ if @cl_opts.format_maps[@options.data[@cl_opts.format_field]]
128
+ @cl_opts.format_maps[@options.data[@cl_opts.format_field]]
129
+ else
130
+ ""
131
+
132
+ # The map of escape characters for escaping stuff.
133
+ ESCAPE_CHARS:
134
+ "&": "&amp;",
135
+ "<": "&lt;",
136
+ ">": "&gt;",
137
+ '"': '&quot;',
138
+ "'": '&#39;',
139
+ "/": '&#x2F;',
140
+ "{": '&#x7b;',
141
+ "}": '&#x7d;'
142
+
143
+ grab_keys = (hash)->
144
+ keys = []
145
+
146
+ for k of hash
147
+ keys.push(k)
148
+
149
+ keys
150
+
151
+ for_regexp = (array)->
152
+ array.join('').replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
153
+
154
+ # The regular expression to match against.
155
+ ESCAPE_REGEX: new RegExp("[#{for_regexp(grab_keys(@prototype.ESCAPE_CHARS))}]", "g")
156
+
157
+ ###
158
+ # Escapes the data passed to it. Matches the data against a regular
159
+ # expression and if it finds anything, replaces the character with the map
160
+ # from ESCAPE_CHARS.
161
+ ###
162
+ _escape: (data)->
163
+ (data || "").replace @ESCAPE_REGEX, (s)->
164
+ @ESCAPE_CHARS[s]
165
+
166
+
@@ -0,0 +1,85 @@
1
+ ###
2
+ # Handles commands that need to be run.
3
+ ###
4
+ class Orator.Libraries.CommandHandler
5
+
6
+ ###
7
+ # Constructs the command handler. No arguments.
8
+ ###
9
+ constructor:->
10
+ @commands = []
11
+
12
+ ###
13
+ # Lists all of the commands that are registered.
14
+ ###
15
+ all_commands:->
16
+
17
+ for k of @commands
18
+ k
19
+
20
+ ###
21
+ # Sets a command with the given name and function.
22
+ ###
23
+ command: (name, callback)->
24
+ @commands[name] = callback
25
+
26
+ ###
27
+ # Runs a command in the context of inst with the arguments given. The first
28
+ # argument is the command, the second the context, and the rest of the
29
+ # arguments are passed to the command.
30
+ ###
31
+ trigger: (command, inst, args...)->
32
+ return false unless @commands[command]
33
+ @commands[command].apply(inst, args)
34
+
35
+ ###
36
+ # Parses the line and runs the command given from the line in the context of
37
+ # inst. First argument is a string containing the command and arguments,
38
+ # Second argument is the context to run under.
39
+ ###
40
+ run: (line, inst)->
41
+ [command, args] = @parse(line)
42
+
43
+ @trigger.apply(@, [command, inst].concat(args))
44
+
45
+ ###
46
+ # Parses a string containing the command and arguments. Handles quotes, but
47
+ # not escaping of them.
48
+ #
49
+ # @TODO: handle escaping quotes.
50
+ # @TODO: handle escaping spaces.
51
+ ###
52
+ parse: (line)->
53
+ command = new Orator.Libraries.StrBuf
54
+ args = []
55
+ state = { in_quote: false, argument: -1 }
56
+
57
+ next_argument = ()->
58
+ state.argument++
59
+ args.push new Orator.Libraries.StrBuf
60
+
61
+ append = (c)->
62
+ if state.argument == -1
63
+ command.append(c)
64
+ else
65
+ args.slice(-1)[0].append(c)
66
+
67
+
68
+ for c in line
69
+
70
+ switch c
71
+ when ' '
72
+ if state.in_quote
73
+ append(c)
74
+ else
75
+ next_argument()
76
+ when '"', "'"
77
+ if state.in_quote == c
78
+ state.in_quote = false
79
+ else if state.in_quote isnt false
80
+ append(c)
81
+ else
82
+ state.in_quote = c
83
+ else append(c)
84
+
85
+ [command.toString(), args.map (arg)-> arg.toString()]
@@ -0,0 +1,45 @@
1
+ ###
2
+ # Handles the events that the server passes.
3
+ ###
4
+ class Orator.Libraries.EventHandler
5
+
6
+ ###
7
+ # Constructor.
8
+ ###
9
+ constructor: (@context)->
10
+ @events = []
11
+
12
+ ###
13
+ # Sets up a callback for the event given. First argument is the event, the
14
+ # second is the callback.
15
+ ###
16
+ on: (event, cb, times = Infinity)->
17
+ @events.push([event, cb, times])
18
+
19
+ ###
20
+ # Runs the event with the context and the arguments. This runs all of the
21
+ # events that match, and returns an array of the results.
22
+ ###
23
+ trigger: (event, args...)->
24
+ events = @_find_events(event)
25
+
26
+ return false if events.length == 0
27
+
28
+ for event in events
29
+ event[1].apply(@context, args)
30
+
31
+ if event[2]-- <= 0
32
+ @events.splice(@events.indexOf(event), 1)
33
+
34
+ ###
35
+ # Finds the events that match the given event.
36
+ ###
37
+ _find_events: (event)->
38
+ out = []
39
+
40
+ for r in @events
41
+ if r[0] == event
42
+ out.push(r)
43
+
44
+ out
45
+
@@ -0,0 +1,3 @@
1
+ window.Orator =
2
+ Libraries: {},
3
+ setup: (()-> )
@@ -0,0 +1,19 @@
1
+ ###
2
+ # This is placed in between the socket and the developer to make sending events
3
+ # easier.
4
+ ###
5
+ class Orator.Libraries.MiddleGround
6
+
7
+ ###
8
+ # Constructor.
9
+ ###
10
+ constructor: (@conn)->
11
+
12
+ ###
13
+ # Sends an event to the server with the data provided. First argument is the
14
+ # event, second argument is the data.
15
+ ###
16
+ send: (event, data)->
17
+ data.event = event
18
+
19
+ @conn.send $.stringify(data)
@@ -0,0 +1,39 @@
1
+ ###
2
+ # Creates a buffer for a string.
3
+ ###
4
+ class Orator.Libraries.StrBuf
5
+
6
+ ###
7
+ # Creates the buffer. First argument is the string it starts with, the
8
+ # second one is what is used when joining the strings together.
9
+ ###
10
+ constructor: (start = '', @join = '')->
11
+ @parts = [start]
12
+ @cache = null
13
+
14
+ ###
15
+ # Appends to the buffer. Can take any number of arguments. If the argument
16
+ # is a StrBuf, concats its buffer to this buffer. If it's anything else, it
17
+ # tries its best to convert it to a string.
18
+ ###
19
+ append:->
20
+ @cache = null
21
+
22
+ for arg in arguments
23
+
24
+ if arg._is_strbuf
25
+ @parts = @parts.concat(arg.parts)
26
+ else
27
+ @parts.push((arg || "").toString())
28
+
29
+ push: @prototype.append
30
+
31
+ ###
32
+ # Converts the buffer to a string. Caches the result, if it can.
33
+ ###
34
+ toString:->
35
+ @cache ||= @parts.join(@join)
36
+
37
+ _is_strbuf: true
38
+
39
+ to_s: @prototype.toString
@@ -0,0 +1,59 @@
1
+ if(!jQuery.stringify) {
2
+ jQuery.extend({
3
+ stringify: function stringify(obj) {
4
+ var t = typeof (obj);
5
+ if (t != "object" || obj === null) {
6
+ // simple data type
7
+ if (t == "string") obj = '"' + obj + '"';
8
+ return String(obj);
9
+ } else {
10
+ // recurse array or object
11
+ var n, v, json = [], arr = (obj && obj.constructor == Array);
12
+
13
+ for (n in obj) {
14
+ v = obj[n];
15
+ t = typeof(v);
16
+ if (obj.hasOwnProperty(n)) {
17
+ if (t == "string") v = '"' + v + '"'; else if (t == "object" && v !== null) v = jQuery.stringify(v);
18
+ json.push((arr ? "" : '"' + n + '":') + String(v));
19
+ }
20
+ }
21
+ return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
22
+ }
23
+ }
24
+ });
25
+ }
26
+
27
+ // thank you, MDN!
28
+ if (!Array.prototype.indexOf) {
29
+ Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
30
+ "use strict";
31
+ if (this == null) {
32
+ throw new TypeError();
33
+ }
34
+ var t = Object(this);
35
+ var len = t.length >>> 0;
36
+ if (len === 0) {
37
+ return -1;
38
+ }
39
+ var n = 0;
40
+ if (arguments.length > 1) {
41
+ n = Number(arguments[1]);
42
+ if (n != n) { // shortcut for verifying if it's NaN
43
+ n = 0;
44
+ } else if (n != 0 && n != Infinity && n != -Infinity) {
45
+ n = (n > 0 || -1) * Math.floor(Math.abs(n));
46
+ }
47
+ }
48
+ if (n >= len) {
49
+ return -1;
50
+ }
51
+ var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
52
+ for (; k < len; k++) {
53
+ if (k in t && t[k] === searchElement) {
54
+ return k;
55
+ }
56
+ }
57
+ return -1;
58
+ }
59
+ }
@@ -0,0 +1,4 @@
1
+ /* SWFObject v2.2 <http://code.google.com/p/swfobject/>
2
+ is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
3
+ */
4
+ var swfobject=function(){var D="undefined",r="object",S="Shockwave Flash",W="ShockwaveFlash.ShockwaveFlash",q="application/x-shockwave-flash",R="SWFObjectExprInst",x="onreadystatechange",O=window,j=document,t=navigator,T=false,U=[h],o=[],N=[],I=[],l,Q,E,B,J=false,a=false,n,G,m=true,M=function(){var aa=typeof j.getElementById!=D&&typeof j.getElementsByTagName!=D&&typeof j.createElement!=D,ah=t.userAgent.toLowerCase(),Y=t.platform.toLowerCase(),ae=Y?/win/.test(Y):/win/.test(ah),ac=Y?/mac/.test(Y):/mac/.test(ah),af=/webkit/.test(ah)?parseFloat(ah.replace(/^.*webkit\/(\d+(\.\d+)?).*$/,"$1")):false,X=!+"\v1",ag=[0,0,0],ab=null;if(typeof t.plugins!=D&&typeof t.plugins[S]==r){ab=t.plugins[S].description;if(ab&&!(typeof t.mimeTypes!=D&&t.mimeTypes[q]&&!t.mimeTypes[q].enabledPlugin)){T=true;X=false;ab=ab.replace(/^.*\s+(\S+\s+\S+$)/,"$1");ag[0]=parseInt(ab.replace(/^(.*)\..*$/,"$1"),10);ag[1]=parseInt(ab.replace(/^.*\.(.*)\s.*$/,"$1"),10);ag[2]=/[a-zA-Z]/.test(ab)?parseInt(ab.replace(/^.*[a-zA-Z]+(.*)$/,"$1"),10):0}}else{if(typeof O.ActiveXObject!=D){try{var ad=new ActiveXObject(W);if(ad){ab=ad.GetVariable("$version");if(ab){X=true;ab=ab.split(" ")[1].split(",");ag=[parseInt(ab[0],10),parseInt(ab[1],10),parseInt(ab[2],10)]}}}catch(Z){}}}return{w3:aa,pv:ag,wk:af,ie:X,win:ae,mac:ac}}(),k=function(){if(!M.w3){return}if((typeof j.readyState!=D&&j.readyState=="complete")||(typeof j.readyState==D&&(j.getElementsByTagName("body")[0]||j.body))){f()}if(!J){if(typeof j.addEventListener!=D){j.addEventListener("DOMContentLoaded",f,false)}if(M.ie&&M.win){j.attachEvent(x,function(){if(j.readyState=="complete"){j.detachEvent(x,arguments.callee);f()}});if(O==top){(function(){if(J){return}try{j.documentElement.doScroll("left")}catch(X){setTimeout(arguments.callee,0);return}f()})()}}if(M.wk){(function(){if(J){return}if(!/loaded|complete/.test(j.readyState)){setTimeout(arguments.callee,0);return}f()})()}s(f)}}();function f(){if(J){return}try{var Z=j.getElementsByTagName("body")[0].appendChild(C("span"));Z.parentNode.removeChild(Z)}catch(aa){return}J=true;var X=U.length;for(var Y=0;Y<X;Y++){U[Y]()}}function K(X){if(J){X()}else{U[U.length]=X}}function s(Y){if(typeof O.addEventListener!=D){O.addEventListener("load",Y,false)}else{if(typeof j.addEventListener!=D){j.addEventListener("load",Y,false)}else{if(typeof O.attachEvent!=D){i(O,"onload",Y)}else{if(typeof O.onload=="function"){var X=O.onload;O.onload=function(){X();Y()}}else{O.onload=Y}}}}}function h(){if(T){V()}else{H()}}function V(){var X=j.getElementsByTagName("body")[0];var aa=C(r);aa.setAttribute("type",q);var Z=X.appendChild(aa);if(Z){var Y=0;(function(){if(typeof Z.GetVariable!=D){var ab=Z.GetVariable("$version");if(ab){ab=ab.split(" ")[1].split(",");M.pv=[parseInt(ab[0],10),parseInt(ab[1],10),parseInt(ab[2],10)]}}else{if(Y<10){Y++;setTimeout(arguments.callee,10);return}}X.removeChild(aa);Z=null;H()})()}else{H()}}function H(){var ag=o.length;if(ag>0){for(var af=0;af<ag;af++){var Y=o[af].id;var ab=o[af].callbackFn;var aa={success:false,id:Y};if(M.pv[0]>0){var ae=c(Y);if(ae){if(F(o[af].swfVersion)&&!(M.wk&&M.wk<312)){w(Y,true);if(ab){aa.success=true;aa.ref=z(Y);ab(aa)}}else{if(o[af].expressInstall&&A()){var ai={};ai.data=o[af].expressInstall;ai.width=ae.getAttribute("width")||"0";ai.height=ae.getAttribute("height")||"0";if(ae.getAttribute("class")){ai.styleclass=ae.getAttribute("class")}if(ae.getAttribute("align")){ai.align=ae.getAttribute("align")}var ah={};var X=ae.getElementsByTagName("param");var ac=X.length;for(var ad=0;ad<ac;ad++){if(X[ad].getAttribute("name").toLowerCase()!="movie"){ah[X[ad].getAttribute("name")]=X[ad].getAttribute("value")}}P(ai,ah,Y,ab)}else{p(ae);if(ab){ab(aa)}}}}}else{w(Y,true);if(ab){var Z=z(Y);if(Z&&typeof Z.SetVariable!=D){aa.success=true;aa.ref=Z}ab(aa)}}}}}function z(aa){var X=null;var Y=c(aa);if(Y&&Y.nodeName=="OBJECT"){if(typeof Y.SetVariable!=D){X=Y}else{var Z=Y.getElementsByTagName(r)[0];if(Z){X=Z}}}return X}function A(){return !a&&F("6.0.65")&&(M.win||M.mac)&&!(M.wk&&M.wk<312)}function P(aa,ab,X,Z){a=true;E=Z||null;B={success:false,id:X};var ae=c(X);if(ae){if(ae.nodeName=="OBJECT"){l=g(ae);Q=null}else{l=ae;Q=X}aa.id=R;if(typeof aa.width==D||(!/%$/.test(aa.width)&&parseInt(aa.width,10)<310)){aa.width="310"}if(typeof aa.height==D||(!/%$/.test(aa.height)&&parseInt(aa.height,10)<137)){aa.height="137"}j.title=j.title.slice(0,47)+" - Flash Player Installation";var ad=M.ie&&M.win?"ActiveX":"PlugIn",ac="MMredirectURL="+O.location.toString().replace(/&/g,"%26")+"&MMplayerType="+ad+"&MMdoctitle="+j.title;if(typeof ab.flashvars!=D){ab.flashvars+="&"+ac}else{ab.flashvars=ac}if(M.ie&&M.win&&ae.readyState!=4){var Y=C("div");X+="SWFObjectNew";Y.setAttribute("id",X);ae.parentNode.insertBefore(Y,ae);ae.style.display="none";(function(){if(ae.readyState==4){ae.parentNode.removeChild(ae)}else{setTimeout(arguments.callee,10)}})()}u(aa,ab,X)}}function p(Y){if(M.ie&&M.win&&Y.readyState!=4){var X=C("div");Y.parentNode.insertBefore(X,Y);X.parentNode.replaceChild(g(Y),X);Y.style.display="none";(function(){if(Y.readyState==4){Y.parentNode.removeChild(Y)}else{setTimeout(arguments.callee,10)}})()}else{Y.parentNode.replaceChild(g(Y),Y)}}function g(ab){var aa=C("div");if(M.win&&M.ie){aa.innerHTML=ab.innerHTML}else{var Y=ab.getElementsByTagName(r)[0];if(Y){var ad=Y.childNodes;if(ad){var X=ad.length;for(var Z=0;Z<X;Z++){if(!(ad[Z].nodeType==1&&ad[Z].nodeName=="PARAM")&&!(ad[Z].nodeType==8)){aa.appendChild(ad[Z].cloneNode(true))}}}}}return aa}function u(ai,ag,Y){var X,aa=c(Y);if(M.wk&&M.wk<312){return X}if(aa){if(typeof ai.id==D){ai.id=Y}if(M.ie&&M.win){var ah="";for(var ae in ai){if(ai[ae]!=Object.prototype[ae]){if(ae.toLowerCase()=="data"){ag.movie=ai[ae]}else{if(ae.toLowerCase()=="styleclass"){ah+=' class="'+ai[ae]+'"'}else{if(ae.toLowerCase()!="classid"){ah+=" "+ae+'="'+ai[ae]+'"'}}}}}var af="";for(var ad in ag){if(ag[ad]!=Object.prototype[ad]){af+='<param name="'+ad+'" value="'+ag[ad]+'" />'}}aa.outerHTML='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+ah+">"+af+"</object>";N[N.length]=ai.id;X=c(ai.id)}else{var Z=C(r);Z.setAttribute("type",q);for(var ac in ai){if(ai[ac]!=Object.prototype[ac]){if(ac.toLowerCase()=="styleclass"){Z.setAttribute("class",ai[ac])}else{if(ac.toLowerCase()!="classid"){Z.setAttribute(ac,ai[ac])}}}}for(var ab in ag){if(ag[ab]!=Object.prototype[ab]&&ab.toLowerCase()!="movie"){e(Z,ab,ag[ab])}}aa.parentNode.replaceChild(Z,aa);X=Z}}return X}function e(Z,X,Y){var aa=C("param");aa.setAttribute("name",X);aa.setAttribute("value",Y);Z.appendChild(aa)}function y(Y){var X=c(Y);if(X&&X.nodeName=="OBJECT"){if(M.ie&&M.win){X.style.display="none";(function(){if(X.readyState==4){b(Y)}else{setTimeout(arguments.callee,10)}})()}else{X.parentNode.removeChild(X)}}}function b(Z){var Y=c(Z);if(Y){for(var X in Y){if(typeof Y[X]=="function"){Y[X]=null}}Y.parentNode.removeChild(Y)}}function c(Z){var X=null;try{X=j.getElementById(Z)}catch(Y){}return X}function C(X){return j.createElement(X)}function i(Z,X,Y){Z.attachEvent(X,Y);I[I.length]=[Z,X,Y]}function F(Z){var Y=M.pv,X=Z.split(".");X[0]=parseInt(X[0],10);X[1]=parseInt(X[1],10)||0;X[2]=parseInt(X[2],10)||0;return(Y[0]>X[0]||(Y[0]==X[0]&&Y[1]>X[1])||(Y[0]==X[0]&&Y[1]==X[1]&&Y[2]>=X[2]))?true:false}function v(ac,Y,ad,ab){if(M.ie&&M.mac){return}var aa=j.getElementsByTagName("head")[0];if(!aa){return}var X=(ad&&typeof ad=="string")?ad:"screen";if(ab){n=null;G=null}if(!n||G!=X){var Z=C("style");Z.setAttribute("type","text/css");Z.setAttribute("media",X);n=aa.appendChild(Z);if(M.ie&&M.win&&typeof j.styleSheets!=D&&j.styleSheets.length>0){n=j.styleSheets[j.styleSheets.length-1]}G=X}if(M.ie&&M.win){if(n&&typeof n.addRule==r){n.addRule(ac,Y)}}else{if(n&&typeof j.createTextNode!=D){n.appendChild(j.createTextNode(ac+" {"+Y+"}"))}}}function w(Z,X){if(!m){return}var Y=X?"visible":"hidden";if(J&&c(Z)){c(Z).style.visibility=Y}else{v("#"+Z,"visibility:"+Y)}}function L(Y){var Z=/[\\\"<>\.;]/;var X=Z.exec(Y)!=null;return X&&typeof encodeURIComponent!=D?encodeURIComponent(Y):Y}var d=function(){if(M.ie&&M.win){window.attachEvent("onunload",function(){var ac=I.length;for(var ab=0;ab<ac;ab++){I[ab][0].detachEvent(I[ab][1],I[ab][2])}var Z=N.length;for(var aa=0;aa<Z;aa++){y(N[aa])}for(var Y in M){M[Y]=null}M=null;for(var X in swfobject){swfobject[X]=null}swfobject=null})}}();return{registerObject:function(ab,X,aa,Z){if(M.w3&&ab&&X){var Y={};Y.id=ab;Y.swfVersion=X;Y.expressInstall=aa;Y.callbackFn=Z;o[o.length]=Y;w(ab,false)}else{if(Z){Z({success:false,id:ab})}}},getObjectById:function(X){if(M.w3){return z(X)}},embedSWF:function(ab,ah,ae,ag,Y,aa,Z,ad,af,ac){var X={success:false,id:ah};if(M.w3&&!(M.wk&&M.wk<312)&&ab&&ah&&ae&&ag&&Y){w(ah,false);K(function(){ae+="";ag+="";var aj={};if(af&&typeof af===r){for(var al in af){aj[al]=af[al]}}aj.data=ab;aj.width=ae;aj.height=ag;var am={};if(ad&&typeof ad===r){for(var ak in ad){am[ak]=ad[ak]}}if(Z&&typeof Z===r){for(var ai in Z){if(typeof am.flashvars!=D){am.flashvars+="&"+ai+"="+Z[ai]}else{am.flashvars=ai+"="+Z[ai]}}}if(F(Y)){var an=u(aj,am,ah);if(aj.id==ah){w(ah,true)}X.success=true;X.ref=an}else{if(aa&&A()){aj.data=aa;P(aj,am,ah,ac);return}else{w(ah,true)}}if(ac){ac(X)}})}else{if(ac){ac(X)}}},switchOffAutoHideShow:function(){m=false},ua:M,getFlashPlayerVersion:function(){return{major:M.pv[0],minor:M.pv[1],release:M.pv[2]}},hasFlashPlayerVersion:F,createSWF:function(Z,Y,X){if(M.w3){return u(Z,Y,X)}else{return undefined}},showExpressInstall:function(Z,aa,X,Y){if(M.w3&&A()){P(Z,aa,X,Y)}},removeSWF:function(X){if(M.w3){y(X)}},createCSS:function(aa,Z,Y,X){if(M.w3){v(aa,Z,Y,X)}},addDomLoadEvent:K,addLoadEvent:s,getQueryParamValue:function(aa){var Z=j.location.search||j.location.hash;if(Z){if(/\?/.test(Z)){Z=Z.split("?")[1]}if(aa==null){return L(Z)}var Y=Z.split("&");for(var X=0;X<Y.length;X++){if(Y[X].substring(0,Y[X].indexOf("="))==aa){return L(Y[X].substring((Y[X].indexOf("=")+1)))}}}return""},expressInstallCallback:function(){if(a){var X=c(R);if(X&&l){X.parentNode.replaceChild(l,X);if(Q){w(Q,true);if(M.ie&&M.win){l.style.display="block"}}if(E){E(B)}}a=false}}}}();
@@ -0,0 +1,391 @@
1
+ // Copyright: Hiroshi Ichikawa <http://gimite.net/en/>
2
+ // License: New BSD License
3
+ // Reference: http://dev.w3.org/html5/websockets/
4
+ // Reference: http://tools.ietf.org/html/rfc6455
5
+
6
+ (function() {
7
+
8
+ if (window.WEB_SOCKET_FORCE_FLASH) {
9
+ // Keeps going.
10
+ } else if (window.WebSocket) {
11
+ return;
12
+ } else if (window.MozWebSocket) {
13
+ // Firefox.
14
+ window.WebSocket = MozWebSocket;
15
+ return;
16
+ }
17
+
18
+ var logger;
19
+ if (window.WEB_SOCKET_LOGGER) {
20
+ logger = WEB_SOCKET_LOGGER;
21
+ } else if (window.console && window.console.log && window.console.error) {
22
+ // In some environment, console is defined but console.log or console.error is missing.
23
+ logger = window.console;
24
+ } else {
25
+ logger = {log: function(){ }, error: function(){ }};
26
+ }
27
+
28
+ // swfobject.hasFlashPlayerVersion("10.0.0") doesn't work with Gnash.
29
+ if (swfobject.getFlashPlayerVersion().major < 10) {
30
+ logger.error("Flash Player >= 10.0.0 is required.");
31
+ return;
32
+ }
33
+ if (location.protocol == "file:") {
34
+ logger.error(
35
+ "WARNING: web-socket-js doesn't work in file:///... URL " +
36
+ "unless you set Flash Security Settings properly. " +
37
+ "Open the page via Web server i.e. http://...");
38
+ }
39
+
40
+ /**
41
+ * Our own implementation of WebSocket class using Flash.
42
+ * @param {string} url
43
+ * @param {array or string} protocols
44
+ * @param {string} proxyHost
45
+ * @param {int} proxyPort
46
+ * @param {string} headers
47
+ */
48
+ window.WebSocket = function(url, protocols, proxyHost, proxyPort, headers) {
49
+ var self = this;
50
+ self.__id = WebSocket.__nextId++;
51
+ WebSocket.__instances[self.__id] = self;
52
+ self.readyState = WebSocket.CONNECTING;
53
+ self.bufferedAmount = 0;
54
+ self.__events = {};
55
+ if (!protocols) {
56
+ protocols = [];
57
+ } else if (typeof protocols == "string") {
58
+ protocols = [protocols];
59
+ }
60
+ // Uses setTimeout() to make sure __createFlash() runs after the caller sets ws.onopen etc.
61
+ // Otherwise, when onopen fires immediately, onopen is called before it is set.
62
+ self.__createTask = setTimeout(function() {
63
+ WebSocket.__addTask(function() {
64
+ self.__createTask = null;
65
+ WebSocket.__flash.create(
66
+ self.__id, url, protocols, proxyHost || null, proxyPort || 0, headers || null);
67
+ });
68
+ }, 0);
69
+ };
70
+
71
+ /**
72
+ * Send data to the web socket.
73
+ * @param {string} data The data to send to the socket.
74
+ * @return {boolean} True for success, false for failure.
75
+ */
76
+ WebSocket.prototype.send = function(data) {
77
+ if (this.readyState == WebSocket.CONNECTING) {
78
+ throw "INVALID_STATE_ERR: Web Socket connection has not been established";
79
+ }
80
+ // We use encodeURIComponent() here, because FABridge doesn't work if
81
+ // the argument includes some characters. We don't use escape() here
82
+ // because of this:
83
+ // https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Functions#escape_and_unescape_Functions
84
+ // But it looks decodeURIComponent(encodeURIComponent(s)) doesn't
85
+ // preserve all Unicode characters either e.g. "\uffff" in Firefox.
86
+ // Note by wtritch: Hopefully this will not be necessary using ExternalInterface. Will require
87
+ // additional testing.
88
+ var result = WebSocket.__flash.send(this.__id, encodeURIComponent(data));
89
+ if (result < 0) { // success
90
+ return true;
91
+ } else {
92
+ this.bufferedAmount += result;
93
+ return false;
94
+ }
95
+ };
96
+
97
+ /**
98
+ * Close this web socket gracefully.
99
+ */
100
+ WebSocket.prototype.close = function() {
101
+ if (this.__createTask) {
102
+ clearTimeout(this.__createTask);
103
+ this.__createTask = null;
104
+ this.readyState = WebSocket.CLOSED;
105
+ return;
106
+ }
107
+ if (this.readyState == WebSocket.CLOSED || this.readyState == WebSocket.CLOSING) {
108
+ return;
109
+ }
110
+ this.readyState = WebSocket.CLOSING;
111
+ WebSocket.__flash.close(this.__id);
112
+ };
113
+
114
+ /**
115
+ * Implementation of {@link <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration">DOM 2 EventTarget Interface</a>}
116
+ *
117
+ * @param {string} type
118
+ * @param {function} listener
119
+ * @param {boolean} useCapture
120
+ * @return void
121
+ */
122
+ WebSocket.prototype.addEventListener = function(type, listener, useCapture) {
123
+ if (!(type in this.__events)) {
124
+ this.__events[type] = [];
125
+ }
126
+ this.__events[type].push(listener);
127
+ };
128
+
129
+ /**
130
+ * Implementation of {@link <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration">DOM 2 EventTarget Interface</a>}
131
+ *
132
+ * @param {string} type
133
+ * @param {function} listener
134
+ * @param {boolean} useCapture
135
+ * @return void
136
+ */
137
+ WebSocket.prototype.removeEventListener = function(type, listener, useCapture) {
138
+ if (!(type in this.__events)) return;
139
+ var events = this.__events[type];
140
+ for (var i = events.length - 1; i >= 0; --i) {
141
+ if (events[i] === listener) {
142
+ events.splice(i, 1);
143
+ break;
144
+ }
145
+ }
146
+ };
147
+
148
+ /**
149
+ * Implementation of {@link <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration">DOM 2 EventTarget Interface</a>}
150
+ *
151
+ * @param {Event} event
152
+ * @return void
153
+ */
154
+ WebSocket.prototype.dispatchEvent = function(event) {
155
+ var events = this.__events[event.type] || [];
156
+ for (var i = 0; i < events.length; ++i) {
157
+ events[i](event);
158
+ }
159
+ var handler = this["on" + event.type];
160
+ if (handler) handler.apply(this, [event]);
161
+ };
162
+
163
+ /**
164
+ * Handles an event from Flash.
165
+ * @param {Object} flashEvent
166
+ */
167
+ WebSocket.prototype.__handleEvent = function(flashEvent) {
168
+
169
+ if ("readyState" in flashEvent) {
170
+ this.readyState = flashEvent.readyState;
171
+ }
172
+ if ("protocol" in flashEvent) {
173
+ this.protocol = flashEvent.protocol;
174
+ }
175
+
176
+ var jsEvent;
177
+ if (flashEvent.type == "open" || flashEvent.type == "error") {
178
+ jsEvent = this.__createSimpleEvent(flashEvent.type);
179
+ } else if (flashEvent.type == "close") {
180
+ jsEvent = this.__createSimpleEvent("close");
181
+ jsEvent.wasClean = flashEvent.wasClean ? true : false;
182
+ jsEvent.code = flashEvent.code;
183
+ jsEvent.reason = flashEvent.reason;
184
+ } else if (flashEvent.type == "message") {
185
+ var data = decodeURIComponent(flashEvent.message);
186
+ jsEvent = this.__createMessageEvent("message", data);
187
+ } else {
188
+ throw "unknown event type: " + flashEvent.type;
189
+ }
190
+
191
+ this.dispatchEvent(jsEvent);
192
+
193
+ };
194
+
195
+ WebSocket.prototype.__createSimpleEvent = function(type) {
196
+ if (document.createEvent && window.Event) {
197
+ var event = document.createEvent("Event");
198
+ event.initEvent(type, false, false);
199
+ return event;
200
+ } else {
201
+ return {type: type, bubbles: false, cancelable: false};
202
+ }
203
+ };
204
+
205
+ WebSocket.prototype.__createMessageEvent = function(type, data) {
206
+ if (document.createEvent && window.MessageEvent && !window.opera) {
207
+ var event = document.createEvent("MessageEvent");
208
+ event.initMessageEvent("message", false, false, data, null, null, window, null);
209
+ return event;
210
+ } else {
211
+ // IE and Opera, the latter one truncates the data parameter after any 0x00 bytes.
212
+ return {type: type, data: data, bubbles: false, cancelable: false};
213
+ }
214
+ };
215
+
216
+ /**
217
+ * Define the WebSocket readyState enumeration.
218
+ */
219
+ WebSocket.CONNECTING = 0;
220
+ WebSocket.OPEN = 1;
221
+ WebSocket.CLOSING = 2;
222
+ WebSocket.CLOSED = 3;
223
+
224
+ // Field to check implementation of WebSocket.
225
+ WebSocket.__isFlashImplementation = true;
226
+ WebSocket.__initialized = false;
227
+ WebSocket.__flash = null;
228
+ WebSocket.__instances = {};
229
+ WebSocket.__tasks = [];
230
+ WebSocket.__nextId = 0;
231
+
232
+ /**
233
+ * Load a new flash security policy file.
234
+ * @param {string} url
235
+ */
236
+ WebSocket.loadFlashPolicyFile = function(url){
237
+ WebSocket.__addTask(function() {
238
+ WebSocket.__flash.loadManualPolicyFile(url);
239
+ });
240
+ };
241
+
242
+ /**
243
+ * Loads WebSocketMain.swf and creates WebSocketMain object in Flash.
244
+ */
245
+ WebSocket.__initialize = function() {
246
+
247
+ if (WebSocket.__initialized) return;
248
+ WebSocket.__initialized = true;
249
+
250
+ if (WebSocket.__swfLocation) {
251
+ // For backword compatibility.
252
+ window.WEB_SOCKET_SWF_LOCATION = WebSocket.__swfLocation;
253
+ }
254
+ if (!window.WEB_SOCKET_SWF_LOCATION) {
255
+ logger.error("[WebSocket] set WEB_SOCKET_SWF_LOCATION to location of WebSocketMain.swf");
256
+ return;
257
+ }
258
+ if (!window.WEB_SOCKET_SUPPRESS_CROSS_DOMAIN_SWF_ERROR &&
259
+ !WEB_SOCKET_SWF_LOCATION.match(/(^|\/)WebSocketMainInsecure\.swf(\?.*)?$/) &&
260
+ WEB_SOCKET_SWF_LOCATION.match(/^\w+:\/\/([^\/]+)/)) {
261
+ var swfHost = RegExp.$1;
262
+ if (location.host != swfHost) {
263
+ logger.error(
264
+ "[WebSocket] You must host HTML and WebSocketMain.swf in the same host " +
265
+ "('" + location.host + "' != '" + swfHost + "'). " +
266
+ "See also 'How to host HTML file and SWF file in different domains' section " +
267
+ "in README.md. If you use WebSocketMainInsecure.swf, you can suppress this message " +
268
+ "by WEB_SOCKET_SUPPRESS_CROSS_DOMAIN_SWF_ERROR = true;");
269
+ }
270
+ }
271
+ var container = document.createElement("div");
272
+ container.id = "webSocketContainer";
273
+ // Hides Flash box. We cannot use display: none or visibility: hidden because it prevents
274
+ // Flash from loading at least in IE. So we move it out of the screen at (-100, -100).
275
+ // But this even doesn't work with Flash Lite (e.g. in Droid Incredible). So with Flash
276
+ // Lite, we put it at (0, 0). This shows 1x1 box visible at left-top corner but this is
277
+ // the best we can do as far as we know now.
278
+ container.style.position = "absolute";
279
+ if (WebSocket.__isFlashLite()) {
280
+ container.style.left = "0px";
281
+ container.style.top = "0px";
282
+ } else {
283
+ container.style.left = "-100px";
284
+ container.style.top = "-100px";
285
+ }
286
+ var holder = document.createElement("div");
287
+ holder.id = "webSocketFlash";
288
+ container.appendChild(holder);
289
+ document.body.appendChild(container);
290
+ // See this article for hasPriority:
291
+ // http://help.adobe.com/en_US/as3/mobile/WS4bebcd66a74275c36cfb8137124318eebc6-7ffd.html
292
+ swfobject.embedSWF(
293
+ WEB_SOCKET_SWF_LOCATION,
294
+ "webSocketFlash",
295
+ "1" /* width */,
296
+ "1" /* height */,
297
+ "10.0.0" /* SWF version */,
298
+ null,
299
+ null,
300
+ {hasPriority: true, swliveconnect : true, allowScriptAccess: "always"},
301
+ null,
302
+ function(e) {
303
+ if (!e.success) {
304
+ logger.error("[WebSocket] swfobject.embedSWF failed");
305
+ }
306
+ }
307
+ );
308
+
309
+ };
310
+
311
+ /**
312
+ * Called by Flash to notify JS that it's fully loaded and ready
313
+ * for communication.
314
+ */
315
+ WebSocket.__onFlashInitialized = function() {
316
+ // We need to set a timeout here to avoid round-trip calls
317
+ // to flash during the initialization process.
318
+ setTimeout(function() {
319
+ WebSocket.__flash = document.getElementById("webSocketFlash");
320
+ WebSocket.__flash.setCallerUrl(location.href);
321
+ WebSocket.__flash.setDebug(!!window.WEB_SOCKET_DEBUG);
322
+ for (var i = 0; i < WebSocket.__tasks.length; ++i) {
323
+ WebSocket.__tasks[i]();
324
+ }
325
+ WebSocket.__tasks = [];
326
+ }, 0);
327
+ };
328
+
329
+ /**
330
+ * Called by Flash to notify WebSockets events are fired.
331
+ */
332
+ WebSocket.__onFlashEvent = function() {
333
+ setTimeout(function() {
334
+ try {
335
+ // Gets events using receiveEvents() instead of getting it from event object
336
+ // of Flash event. This is to make sure to keep message order.
337
+ // It seems sometimes Flash events don't arrive in the same order as they are sent.
338
+ var events = WebSocket.__flash.receiveEvents();
339
+ for (var i = 0; i < events.length; ++i) {
340
+ WebSocket.__instances[events[i].webSocketId].__handleEvent(events[i]);
341
+ }
342
+ } catch (e) {
343
+ logger.error(e);
344
+ }
345
+ }, 0);
346
+ return true;
347
+ };
348
+
349
+ // Called by Flash.
350
+ WebSocket.__log = function(message) {
351
+ logger.log(decodeURIComponent(message));
352
+ };
353
+
354
+ // Called by Flash.
355
+ WebSocket.__error = function(message) {
356
+ logger.error(decodeURIComponent(message));
357
+ };
358
+
359
+ WebSocket.__addTask = function(task) {
360
+ if (WebSocket.__flash) {
361
+ task();
362
+ } else {
363
+ WebSocket.__tasks.push(task);
364
+ }
365
+ };
366
+
367
+ /**
368
+ * Test if the browser is running flash lite.
369
+ * @return {boolean} True if flash lite is running, false otherwise.
370
+ */
371
+ WebSocket.__isFlashLite = function() {
372
+ if (!window.navigator || !window.navigator.mimeTypes) {
373
+ return false;
374
+ }
375
+ var mimeType = window.navigator.mimeTypes["application/x-shockwave-flash"];
376
+ if (!mimeType || !mimeType.enabledPlugin || !mimeType.enabledPlugin.filename) {
377
+ return false;
378
+ }
379
+ return mimeType.enabledPlugin.filename.match(/flashlite/i) ? true : false;
380
+ };
381
+
382
+ if (!window.WEB_SOCKET_DISABLE_AUTO_INITIALIZATION) {
383
+ // NOTE:
384
+ // This fires immediately if web_socket.js is dynamically loaded after
385
+ // the document is loaded.
386
+ swfobject.addDomLoadEvent(function() {
387
+ WebSocket.__initialize();
388
+ });
389
+ }
390
+
391
+ })();
@@ -0,0 +1,29 @@
1
+ #= require ./orator/header
2
+ #= require_self
3
+ #= require_tree ./orator/
4
+
5
+ window.WEB_SOCKET_SWF_LOCATION = "/assets/WebSocketMain.swf"
6
+
7
+ Orator.setup = (address, routes = ()->)->
8
+ libs = Orator.Libraries
9
+
10
+ event_handler = new libs.EventHandler({})
11
+ console.log(event_handler)
12
+ socket = new WebSocket(address)
13
+ event_handler.context.server = new libs.MiddleGround(socket)
14
+ routes.apply(event_handler, event_handler)
15
+
16
+ socket.onopen = (args...)->
17
+ event_handler.trigger('socket.open', args...)
18
+
19
+ socket.onmessage = (event)->
20
+ json_data = $.parseJSON(event.data)
21
+ event_handler.trigger(json_data.event, json_data, event)
22
+
23
+ socket.onclose = (args...)->
24
+ event_handler.trigger('socket.close', args...)
25
+
26
+ socket.onerror = (args...)->
27
+ event_handler.trigger('socket.error', args...)
28
+
29
+ Orator.event_handler = event_handler
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -105,6 +105,17 @@ files:
105
105
  - spec/client_spec.rb
106
106
  - spec/base_orator_spec.rb
107
107
  - spec/spec_helper.rb
108
+ - vendor/assets/swfs/WebSocketMain.swf
109
+ - vendor/assets/javascripts/orator.coffee
110
+ - vendor/assets/javascripts/orator/web_socket.js
111
+ - vendor/assets/javascripts/orator/stringify.jquery.js
112
+ - vendor/assets/javascripts/orator/header.coffee
113
+ - vendor/assets/javascripts/orator/event_handler.coffee
114
+ - vendor/assets/javascripts/orator/chat_logger.coffee
115
+ - vendor/assets/javascripts/orator/swfobject.js
116
+ - vendor/assets/javascripts/orator/command_handler.coffee
117
+ - vendor/assets/javascripts/orator/strbuf.coffee
118
+ - vendor/assets/javascripts/orator/middle_ground.coffee
108
119
  - README.md
109
120
  - Rakefile
110
121
  - LICENSE