ponymail-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From humbed...@apache.org
Subject [01/17] incubator-ponymail git commit: First batch of coffee files
Date Fri, 02 Sep 2016 07:44:03 GMT
Repository: incubator-ponymail
Updated Branches:
  refs/heads/coffee-and-cake [created] f2cdcb474


First batch of coffee files

miscellaneous functions, DOM handling, HTTP requests
and a simple localstorage wrapper.


Project: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/commit/6dad4bc8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/tree/6dad4bc8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/diff/6dad4bc8

Branch: refs/heads/coffee-and-cake
Commit: 6dad4bc8620962c90326eaa4fc2283969b5b5157
Parents: e8e5b9f
Author: Daniel Gruno <humbedooh@apache.org>
Authored: Thu Sep 1 16:48:57 2016 +0200
Committer: Daniel Gruno <humbedooh@apache.org>
Committed: Thu Sep 1 16:48:57 2016 +0200

----------------------------------------------------------------------
 site/js/coffee/colors.coffee       | 108 ++++++++++++++++++++++++
 site/js/coffee/combine.sh          |   3 +
 site/js/coffee/dom_utils.coffee    | 101 ++++++++++++++++++++++
 site/js/coffee/http_utils.coffee   | 143 ++++++++++++++++++++++++++++++++
 site/js/coffee/localstorage.coffee |  49 +++++++++++
 site/js/coffee/misc.coffee         |  18 ++++
 6 files changed, 422 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/6dad4bc8/site/js/coffee/colors.coffee
----------------------------------------------------------------------
diff --git a/site/js/coffee/colors.coffee b/site/js/coffee/colors.coffee
new file mode 100644
index 0000000..e843e1d
--- /dev/null
+++ b/site/js/coffee/colors.coffee
@@ -0,0 +1,108 @@
+###
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements.  See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License.  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+###
+
+hsl2rgb = (h, s, l) ->
+    
+    h = h % 1;
+    s = 1 if s > 1
+    l = 1 if l > 1
+    if l <= 0.5
+        v = (l * (1 + s))
+    else
+        v = (l + s - l * s);
+    if v == 0
+        return {
+            r: 0,
+            g: 0,
+            b: 0
+        }
+
+    min = 2 * l - v;
+    sv = (v - min) / v;
+    sh = (6 * h) % 6;
+    switcher = Math.floor(sh);
+    fract = sh - switcher;
+    vsf = v * sv * fract;
+
+    switch switcher
+        when 0
+            return {
+                r: v,
+                g: min + vsf,
+                b: min
+            };
+        when 1
+            return {
+                r: v - vsf,
+                g: v,
+                b: min
+            };
+        when 2
+            return {
+                r: min,
+                g: v,
+                b: min + vsf
+            };
+        when 3
+            return {
+                r: min,
+                g: v - vsf,
+                b: v
+            };
+        when 4
+            return {
+                r: min + vsf,
+                g: min,
+                b: v
+            };
+        when 5
+            return {
+                r: v,
+                g: min,
+                b: v - vsf
+            };
+    
+    return {
+        r: 0,
+        g: 0,
+        b: 0
+    };
+
+
+genColors = (numColors, saturation, lightness, hex) ->
+    cls = []
+    baseHue = 1.34;
+    for i in [1..numColors]
+        c = hsl2rgb(baseHue, saturation, lightness)
+        if (hex) 
+            h = ( Math.round(c.r*255*255*255) + Math.round(c.g * 255*255) + Math.round(c.b*255)
).toString(16)
+            while h.length < 6
+                h = '0' + h
+            h = '#' + h
+            cls.push(h);
+        else
+                cls.push({
+                    r: parseInt(c.r * 255),
+                    g: parseInt(c.g * 255),
+                    b: parseInt(c.b * 255)
+                })
+        
+        baseHue -= 0.23
+        if (baseHue < 0) 
+            baseHue += 1
+    
+    return cls

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/6dad4bc8/site/js/coffee/combine.sh
----------------------------------------------------------------------
diff --git a/site/js/coffee/combine.sh b/site/js/coffee/combine.sh
new file mode 100644
index 0000000..4644a34
--- /dev/null
+++ b/site/js/coffee/combine.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+coffee -b --join ../ponymail.coffee.js -c *.coffee
+

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/6dad4bc8/site/js/coffee/dom_utils.coffee
----------------------------------------------------------------------
diff --git a/site/js/coffee/dom_utils.coffee b/site/js/coffee/dom_utils.coffee
new file mode 100644
index 0000000..43bc815
--- /dev/null
+++ b/site/js/coffee/dom_utils.coffee
@@ -0,0 +1,101 @@
+###
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements.  See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License.  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+###
+
+# mk: DOM creator
+# args:
+# - type: HTML element type (div, table, p etc) to produce
+# - params: hash of element params to add (class, style etc)
+# - children: optional child or children objects to insert into the new element
+# Example: mk('div', { class: "footer", style: "font-weight: bold;"}, "Some text inside a
div")
+mk = (type, params, children) ->
+    # create the raw element
+    r = document.createElement(type)
+    
+    # If params have been passed, set them
+    if params
+        for k, v of params
+            if v
+                r.setAttribute(k, v)
+    
+    # If any children have been passed, add them to the element 
+    if children
+        # If string, convert to textNode using txt()
+        if typeof children == "string"
+            app(r, txt(children))
+        else
+            # If children is an array of elems, iterate and add
+            if isArray children
+                for k in children
+                    # String? Convert via txt() then
+                    if typeof k == "string"
+                        app(r, txt(k))
+                    # Plain element, add normally
+                    else
+                        app(r, k)
+            # Just a single element, add it
+            else
+                app(r, children)
+    return r
+
+# App: Shortcut for document.appendChild with modifications
+# - a: the element to add things to
+# - b: one or more elements to add.
+# Example: app(get('mydiv'), "Add some text to mydiv")
+app = (a,b) ->
+    # If it's a list of elems, iterate
+    if isArray b
+        for item in b
+            # String? Convert to textNode first then
+            if typeof item == "string"
+                item = txt(item)
+            # Otherwise just add it
+            a.appendChild(item)
+    # Otherwise, just add
+    else
+        # String? Convert first
+        if typeof b == "string"
+            a.appendChild(txt(b))
+        # Not a string, add normally
+        return a.appendChild(b)
+
+
+# Set: shortcut for a.setAttribute(b,c)
+set = (a, b, c) ->
+    return a.setAttribute(b,c)
+
+# txt: shortcut for creating a text node
+txt = (a) ->
+    return document.createTextNode(a)
+
+# Get: Shortcut for doc.getElementById
+get = (a) ->
+    return document.getElementById(a)
+
+
+# Cog: Loading panel for when waiting for a response
+cog = (div, size = 200) ->
+        idiv = document.createElement('div')
+        idiv.setAttribute("class", "icon")
+        idiv.setAttribute("style", "text-align: center; vertical-align: middle; height: 500px;")
+        i = document.createElement('i')
+        i.setAttribute("class", "fa fa-spin fa-cog")
+        i.setAttribute("style", "font-size: " + size + "pt !important; color: #AAB;")
+        idiv.appendChild(i)
+        idiv.appendChild(document.createElement('br'))
+        idiv.appendChild(document.createTextNode('Loading data, please wait...'))
+        div.innerHTML = ""
+        div.appendChild(idiv)

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/6dad4bc8/site/js/coffee/http_utils.coffee
----------------------------------------------------------------------
diff --git a/site/js/coffee/http_utils.coffee b/site/js/coffee/http_utils.coffee
new file mode 100644
index 0000000..5101a00
--- /dev/null
+++ b/site/js/coffee/http_utils.coffee
@@ -0,0 +1,143 @@
+###
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements.  See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License.  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+###
+
+# fetch: Fetches a URL.
+# Params:
+# - url: URL to fetch
+# - xstate: JS state object to pass on to callback
+# - callback: callback function to utilize the response
+# - snap: optional callback if fetch fails (error 500)
+# - nocreds: set to True to disable sending credentials (cookies etc)
+# Example: fetch("/api/foo.lua", {pony: true}, callbackfunc, null, true)
+fetch = (url, xstate, callback, snap, nocreds) ->
+    xmlHttp = null;
+    
+    # Set up request object
+    if window.XMLHttpRequest
+        xmlHttp = new XMLHttpRequest();
+    else
+        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
+    if not nocreds
+        xmlHttp.withCredentials = true
+    
+    # GET URL
+    xmlHttp.open("GET", url, true);
+    xmlHttp.send(null);
+    
+    # Check for request state response change
+    xmlHttp.onreadystatechange = (state) ->
+            # Internal Server Error: Try to call snap
+            if xmlHttp.readyState == 4 and xmlHttp.status == 500
+                if snap
+                    snap(xstate)
+            # 200 OK, everything is okay, try to parse JSON response
+            if xmlHttp.readyState == 4 and xmlHttp.status == 200
+                if callback
+                    # Try to parse as JSON and deal with cache objects, fall back to old
style parse-and-pass
+                    try
+                        # Parse JSON response
+                        response = JSON.parse(xmlHttp.responseText)
+                        # If loginRequired (rare!), redirect to oauth page
+                        if response && response.loginRequired
+                            location.href = "/oauth.html"
+                            return
+                        # Otherwise, call the callback function
+                        callback(response, xstate);
+                    # JSON parse failed? Pass on the response as plain text then
+                    catch e
+                        callback(xmlHttp.responseText, xstate)
+
+# post: like fetch, but do a POST with standard text fields
+# - url: URL to POST to
+# - args: hash of keys/vals to convert into a POST request
+# - xstate: state to pass on to callback
+# - callback: calback function for response
+# - snap: callback in case of error 500
+post = (url, args, xstate, callback, snap) ->
+    xmlHttp = null;
+    # Set up request object
+    if window.XMLHttpRequest
+        xmlHttp = new XMLHttpRequest();
+    else
+        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
+    xmlHttp.withCredentials = true
+    
+    # Construct form data string to POST.
+    ar = []
+    for k,v of args
+        if v and v != ""
+            ar.push(k + "=" + encodeURIComponent(v))
+    fdata = ar.join("&")
+    
+    
+    # POST URL
+    xmlHttp.open("POST", url, true);
+    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
+    xmlHttp.send(fdata);
+    
+    # Check for response
+    xmlHttp.onreadystatechange = (state) ->
+            # Internal Server Error: call snap function
+            if xmlHttp.readyState == 4 and xmlHttp.status == 500
+                if snap
+                    snap(xstate)
+            # 200 Okay, parse response and run callback
+            if xmlHttp.readyState == 4 and xmlHttp.status == 200
+                if callback
+                    # Try to parse as JSON and deal with cache objects, fall back to old
style parse-and-pass
+                    try
+                        response = JSON.parse(xmlHttp.responseText)
+                        callback(response, xstate);
+                    # JSON parse failed? Try passing on as plain text
+                    catch e
+                        callback(xmlHttp.responseText, xstate)
+
+# postJSON: Same as post, but send vars as a JSON object
+postJSON = (url, json, xstate, callback, snap) ->
+    xmlHttp = null;
+    # Set up request object
+    if window.XMLHttpRequest
+        xmlHttp = new XMLHttpRequest();
+    else
+        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
+    xmlHttp.withCredentials = true
+    
+    # Construct form data
+    fdata = JSON.stringify(json)
+    
+    # POST URL
+    xmlHttp.open("POST", url, true);
+    xmlHttp.setRequestHeader("Content-type", "application/json");
+    xmlHttp.send(fdata);
+    
+    # Check for response
+    xmlHttp.onreadystatechange = (state) ->
+            # Internal Server Error: call snap!
+            if xmlHttp.readyState == 4 and xmlHttp.status == 500
+                if snap
+                    snap(xstate)
+                    
+            # 200 Okay, parse response and pass to callback
+            if xmlHttp.readyState == 4 and xmlHttp.status == 200
+                if callback
+                    # Try to parse as JSON and deal with cache objects, fall back to old
style parse-and-pass
+                    try
+                        response = JSON.parse(xmlHttp.responseText)
+                        callback(response, xstate);
+                    # Fall back to plain text if parse failed
+                    catch e
+                        callback(xmlHttp.responseText, xstate)

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/6dad4bc8/site/js/coffee/localstorage.coffee
----------------------------------------------------------------------
diff --git a/site/js/coffee/localstorage.coffee b/site/js/coffee/localstorage.coffee
new file mode 100644
index 0000000..874944e
--- /dev/null
+++ b/site/js/coffee/localstorage.coffee
@@ -0,0 +1,49 @@
+###
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements.  See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License.  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+###
+
+# Init: Test if localStorage is available or not
+# If not, fall back to plain global var storage (not effective, but meh)
+pm_storage_available = false
+pm_storage_globvar = {}
+try 
+    if typeof window.localStorage != "undefined"
+        window.localStorage.setItem("pm_test", "1")
+        pm_storage_available = true
+catch e
+    pm_storage_available = false
+    
+
+# dbWrite: Store a key/val pair
+# Example: dbWrite("ponies", "They are awesome!")
+dbWrite = (key, value) ->
+    # Can we use localStorage?
+    if pm_storage_available
+        return window.localStorage.setItem(key, value)
+    # Guess not, fall back to (ineffective) global var
+    else
+        pm_storage_globvar[key] = value
+        return true
+    
+# dbRead: Given a key, read the corresponding value from storage
+dbRead = (key) ->
+    # Do we have localStorage?
+    if pm_storage_available
+        return window.localStorage.getItem(key)
+    # Nope, try global var
+    else
+        return pm_storage_globvar[key]
+    

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/6dad4bc8/site/js/coffee/misc.coffee
----------------------------------------------------------------------
diff --git a/site/js/coffee/misc.coffee b/site/js/coffee/misc.coffee
new file mode 100644
index 0000000..92db090
--- /dev/null
+++ b/site/js/coffee/misc.coffee
@@ -0,0 +1,18 @@
+# Miscellaneous functions
+
+# Number prettification prototype:
+# Converts 1234567 into 1,234,567 etc
+Number.prototype.pretty = (fix) ->
+    if (fix)
+        return String(this.toFixed(fix)).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
+    return String(this.toFixed(0)).replace(/(\d)(?=(\d{3})+$)/g, '$1,');
+
+
+# isArray: function to detect if an object is an array
+isArray = ( value ) ->
+    value and
+        typeof value is 'object' and
+        value instanceof Array and
+        typeof value.length is 'number' and
+        typeof value.splice is 'function' and
+        not ( value.propertyIsEnumerable 'length' )


Mime
View raw message