Skip to content

Commit

Permalink
Core features
Browse files Browse the repository at this point in the history
Core features based on source code from the Sitemagic CMS project and
other minor projects - permission granted by Jimmy Thomsen.
  • Loading branch information
Jemt committed Jan 10, 2015
1 parent 4f1fe8c commit fda66ae
Show file tree
Hide file tree
Showing 13 changed files with 1,797 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Core/Array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Fit.Array = {};

Fit.Array.ForEach = function(arr, callback)
{
for (var i = 0 ; i < arr.length ; i++)
if (callback(arr[i]) === false)
break;
}

Fit.Array.Add = function(arr, elm)
{
arr.push(elm);
}

Fit.Array.Remove = function(arr, elm)
{
var idx = Fit.Array.GetIndex(arr, elm);

if (idx !== -1)
arr.splice(idx, 1);
}

Fit.Array.Clear = function(arr)
{
arr = [];
}

Fit.Array.GetIndex = function(arr, elm)
{
for (var i = 0 ; i < arr.length ; i++)
if (arr[i] === elm)
return i;

return -1;
}

/*
// Difference between using ordinary for loop and Fit.Array.ForEach
// can be easily demonstrated with the code below.
// Fit.Array.ForEach creates a new execution scope (closure) which
// ensure the expected array value is used, while an ordinary loop
// results in the same index (i) variable being used, which will
// have the value 3 when all setTimeout callbacks finally fire.
// Will output 1, 3, 6, 8 as expected
Fit.Array.ForEach([1,3,6,8], function(val)
{
setTimeout(function() { console.log(val); }, 100);
});
// Will output "8" 4 times
for (var i in vals = [1,3,6,8])
{
setTimeout(function() { console.log(vals[i]); }, 100);
}
*/
157 changes: 157 additions & 0 deletions Core/Browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
Fit.Browser = {};

Fit.Browser.GetBrowser = function()
{
var agent = navigator.userAgent;

if (agent.indexOf("Chrome") > -1)
return "Chrome";
if (agent.indexOf("Safari") > -1)
return "Safari";
if (agent.indexOf("MSIE") > -1 || agent.indexOf("Trident") > -1)
return "MSIE";
if (agent.indexOf("Firefox") > -1)
return "Firefox";
if (agent.indexOf("Opera") > -1)
return "Opera";

return "Unknown";
}

Fit.Browser.GetVersion = function()
{
var start = 0;
var end = 0;
var agent = navigator.userAgent;

if (Fit.Browser.GetBrowser() === "Chrome")
{
start = agent.indexOf("Chrome/");
start = (start !== -1 ? start + 7 : 0);
end = agent.indexOf(".", start);
end = (end !== -1 ? end : 0);
}
if (Fit.Browser.GetBrowser() === "Safari")
{
start = agent.indexOf("Version/");
start = (start !== -1 ? start + 8 : 0);
end = agent.indexOf(".", start);
end = (end !== -1 ? end : 0);
}
if (Fit.Browser.GetBrowser() === "MSIE")
{
if (agent.indexOf("MSIE") > -1)
{
start = agent.indexOf("MSIE ");
start = (start !== -1 ? start + 5 : 0);
end = agent.indexOf(".", start);
end = (end !== -1 ? end : 0);
}
else if (agent.indexOf("Trident") > -1) // IE11+
{
start = agent.indexOf("rv:");
start = (start !== -1 ? start + 3 : 0);
end = agent.indexOf(".", start);
end = (end !== -1 ? end : 0);
}
}
if (Fit.Browser.GetBrowser() === "Firefox")
{
start = agent.indexOf("Firefox/");
start = (start !== -1 ? start + 8 : 0);
end = agent.indexOf(".", start);
end = (end !== -1 ? end : 0);
}
if (Fit.Browser.GetBrowser() === "Opera")
{
start = agent.indexOf("Version/");
start = (start !== -1 ? start + 8 : -1);

if (start === -1)
{
start = agent.indexOf("Opera/");
start = (start !== -1 ? start + 6 : -1);
}

if (start === -1)
{
start = agent.indexOf("Opera ");
start = (start !== -1 ? start + 6 : -1);
}

end = agent.indexOf(".", start);
end = (end !== -1 ? end : 0);
}

if (start !== 0 && start !== 0)
return parseInt(agent.substring(start, end));

return -1;
}

Fit.Browser.GetLanguage = function()
{
var lang = null;

if (navigator.language)
lang = navigator.language.toLowerCase();
else if (navigator.browserLanguage)
lang = navigator.browserLanguage.toLowerCase();

if (lang === null || lang === "")
return "en";

if (lang.length === 2)
return lang;

if (lang.length === 5)
return lang.substring(0, 2);

return "en";
}

Fit.Browser.GetPageWidth = function()
{
var w = -1;

if (window.innerWidth) // W3C
w = window.innerWidth;
else if (document.documentElement && document.documentElement.clientWidth) // IE 6-8 (not quirks mode)
w = document.documentElement.clientWidth;

return w;
}

Fit.Browser.GetPageHeight = function()
{
var h = -1;

if (window.innerHeight) // W3C
h = window.innerHeight;
else if (document.documentElement && document.documentElement.clientHeight) // IE 6-8 (not quirks mode)
h = document.documentElement.clientHeight;

return h;
}

Fit.Browser.GetScreenWidth = function(onlyAvailable)
{
if (onlyAvailable === true)
return window.screen.availWidth;

return window.screen.width;
}

Fit.Browser.GetScreenHeight = function(onlyAvailable)
{
if (onlyAvailable === true)
return window.screen.availHeight;

return window.screen.height;
}

Fit.Browser.Log = function(msg)
{
if (window.console)
console.log(msg);
}
41 changes: 41 additions & 0 deletions Core/Cookies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Fit.Cookies = {};

Fit.Cookies.Set = function(name, value, seconds)
{
if (value.indexOf(';') > -1)
{
throw new Error("Unable to set cookie - value contains illegal character: ';'");
return false;
}

var date = new Date();
date.setTime(date.getTime() + (seconds * 1000));
document.cookie = name + "=" + value + "; expires=" + date.toGMTString() + "; path=/"; // TODO: Is this wise? I don't think it should be hardcoded to root path!

return true;
}

Fit.Cookies.Get = function(name)
{
var name = name + "=";
var cookies = document.cookie.split(";");
var cookie = null;

for (i = 0 ; i < cookies.length ; i++)
{
cookie = cookies[i];

while (cookie.charAt(0) === " ")
cookie = cookie.substring(1, cookie.length);

if (cookie.indexOf(name) === 0)
return cookie.substring(name.length, cookie.length);
}

return null;
}

Fit.Cookies.Remove = function(name)
{
return this.Set(name, "", -1);
}
22 changes: 22 additions & 0 deletions Core/Core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Fit = {};
Fit.Core = {};

Fit.Core.Extend = function(subInstance, superType)
{
var binder =
{
Apply: function()
{
superType.apply(subInstance, arguments);
}
}
return binder;
}

Fit._internal = {};

(function()
{
var src = document.scripts[document.scripts.length - 1].src;
Fit._internal.BasePath = src.substring(0, src.lastIndexOf("/"));
})();
112 changes: 112 additions & 0 deletions Core/Dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
Fit.Dom = {};

// CSS

Fit.Dom.AddClass = function(elm, cls)
{
if (Fit.Dom.HasClass(elm, cls) === false)
elm.className += ((elm.className !== "") ? " " : "") + cls;
}

Fit.Dom.RemoveClass = function(elm, cls)
{
var arr = elm.className.split(" ");
var newCls = "";

Fit.Array.ForEach(arr, function(item)
{
if (item !== cls)
newCls += ((newCls !== "") ? " " : "") + item;
});

elm.className = newCls;
}

Fit.Dom.HasClass = function(elm, cls)
{
var arr = elm.className.split(" ");
var found = false;

Fit.Array.ForEach(arr, function(item)
{
if (item === cls)
{
found = true;
return false; // Stop loop
}
});

return found;
}

Fit.Dom.GetComputedStyle = function(elm, style)
{
if (window.getComputedStyle)
return window.getComputedStyle(elm)[style];
else if (elm.currentStyle)
return elm.currentStyle[style];

return null;
}

// Structure

Fit.Dom.GetDepth = function(elm)
{
var i = 0;
var parent = elm.parentElement;

while (parent)
{
i++;
parent = parent.parentElement;
}

return i;
}

Fit.Dom.GetPosition = function(elm, relativeToViewport)
{
// Return position within viewport

if (relativeToViewport === true)
{
var res = elm.getBoundingClientRect();
return { X: res.left, Y: res.top };
}

// Return position within document

var pos = { X: 0, Y: 0 };

while (elm)
{
pos.X += elm.offsetLeft;
pos.Y += elm.offsetTop;
elm = elm.offsetParent;
}

return pos;
}

Fit.Dom.GetScrollPosition = function(elm)
{
// Get number of pixels specified element's container(s)
// have been scrolled. This gives us the total scroll value
// for nested scrollable elements which in combination with
// X,Y mouse coordinates can be used to determine the mouse
// position in the document rather than in the viewport:
// scrollX = mouseXviewport + GetScrollPosition(elm).X;
// scrollY = mouseYviewport + GetScrollPosition(elm).Y;

var pos = { X: 0, Y: 0 };

while (elm)
{
pos.X += elm.scrollLeft;
pos.Y += elm.scrollTop;
elm = elm.parentElement;
}

return pos;
}
Loading

0 comments on commit fda66ae

Please sign in to comment.