main.js
( function() {"use strict";
var TESTFLAG = true;
var EVENT_NAME = 'touchstart';
var PROGRAM_Directory = "EFOLAB_ver3_6";
var CLASS_NAME = "EFOLABclass";
var Page, Container, Device, Main;
var PAGE_CSS_JSON = {
"" : "top.css",
"index.html" : "top.css",
"guide/service.html" : "guide_service.css"
};
var SUBMENU_JSON = [{
"text" : "TOP",
"href" : "index.html",
"id" : "EFOLAB_submenu_top"
}, {
"text" : "活動の流れ",
"href" : "guide/service.html",
"id" : "EFOLAB_submenu_guide_service"
}, {
"text" : "料金プラン",
"href" : "guide/price.html",
"id" : "EFOLAB_submenu_guide_service"
}, {
"text" : "婚活パーティー",
"href" : "party/index.html",
"id" : "EFOLAB_submenu_party_index"
}, {
"text" : "魅力アップセミナー",
"href" : "seminar/index.html",
"id" : "EFOLAB_submenu_seminar_index"
}, {
"text" : "よくあるご質問",
"href" : "question/index.html",
"id" : "EFOLAB_submenu_question_index"
}, {
"text" : "ヴィーノの特徴",
"href" : "about/index.html",
"id" : "EFOLAB_submenu_about_index"
}, {
"text" : "独身vs結婚",
"href" : "advise/index.html",
"id" : "EFOLAB_submenu_advise_index"
}, {
"text" : "結婚でプラスなこと",
"href" : "advise/plus.html",
"id" : "EFOLAB_submenu_advise_plus"
}, {
"text" : "幸せな家族とは",
"href" : "advise/family.html",
"id" : "EFOLAB_submenu_advise_family"
}, {
"text" : "人生は戦略!",
"href" : "advise/image.html",
"id" : "EFOLAB_submenu_advise_image"
}, {
"text" : "成婚カップルの声",
"href" : "voice/index.html",
"id" : "EFOLAB_submenu_voice_index"
}, {
"text" : "大阪サロンのご案内[アクセス]",
"href" : "company/access.html",
"id" : "EFOLAB_submenu_company_access"
}, {
"text" : "京都サロンのご案内",
"href" : "/kyoto/index.html",
"id" : "EFOLAB_submenu_kyoto_index"
}, {
"text" : "社長インタビュー",
"href" : "ceo-intaview/index.html",
"id" : "EFOLAB_submenu_kyoto_index"
}, {
"text" : "ヴィーノの求人情報",
"href" : "recruit/index.html",
"id" : "EFOLAB_submenu_recruit_index"
}, {
"text" : "",
"href" : "",
"id" : ""
}];
Page = function(device) {
this.accessCounter = 0;
this.device = device;
this.container = {};
this.contents = {};
this.url = window.location.href;
this.cssLink = "";
this.initialize(this.url);
};
Page.prototype = {
initialize : function(newUrl) {
var self = this;
window.addEventListener('DOMContentLoaded', function(event) {
self.setHeightToContainers(self);
}, false);
document.addEventListener('DOMContentLoaded', self.removeDefaultCssDisable, false);
if (this.accessCounter === 0) {
this.firstTimeProcess();
this.accessCounter += 1;
} else {
if (newUrl === this.url) {
return;
}
this.secondTimeProcess(newUrl);
this.url = newUrl;
}
},
firstTimeProcess : function() {
var self = this;
this.createAndAppendNewPageCssLink();
this.setNewCssToChangedPage(this.url);
var setEventListenerToSlide = function() {
self.container.menuAnchor.addEventListener(EVENT_NAME, self, false);
XXX
self.container.mainContainer.addEventListener('touchstart', self, false);
self.container.mainContainer.addEventListener('touchmove', self, false);
self.container.mainContainer.addEventListener('touchend', self, false);
self.container.mainContainer.addEventListener('click', self, false);
};
var appendContainerToBody = function() {
var setWhenDOMContentLoaded = function() {
window.stop();
var sectionFragment = document.createDocumentFragment();
sectionFragment.appendChild(document.getElementById('maincol'));
var footerFragment = document.createDocumentFragment();
footerFragment.appendChild(document.getElementById('footer'));
document.body.innerHTML = "";
document.body.appendChild(self.container.outerContainer);
self.container.sectionContainer.appendChild(sectionFragment);
self.container.footerContainer.appendChild(footerFragment);
self.crateAneAppendSideMenu();
setEventListenerToSlide();
self.setSectionContainerLinksControll();
self.setFooterContainerLinksControll();
self.setSubContainerLinksControll();
};
if (document.body) {
setWhenDOMContentLoaded();
} else {
document.addEventListener('DOMContentLoaded', setWhenDOMContentLoaded, false);
}
};
this.container = new Container(appendContainerToBody);
this.setCSSbodyBlock();
},
secondTimeProcess : function(url) {
var self = this;
this.setNewCssToChangedPage(url);
var xhr = null;
var xhrCallback = function(event) {
TODO
if (xhr.status === 200 && xhr.readyState === 4) {
var htmlDoc = self.getDomFromText(xhr.responseText);
self.setContentsTosectionContainer(htmlDoc);
}
};
xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.addEventListener('readystatechange', xhrCallback, false);
xhr.send();
},
crateAneAppendSideMenu : function() {
var fragment = document.createDocumentFragment();
var ul = document.createElement('ul');
fragment.appendChild(ul);
for (var i = 0; i < SUBMENU_JSON.length; i++) {
var li = document.createElement('li');
var a = document.createElement('a');
var txt = document.createTextNode(SUBMENU_JSON[i].text);
a.appendChild(txt);
li.appendChild(a);
li.id = SUBMENU_JSON[i].id;
a.href = SUBMENU_JSON[i].href;
ul.appendChild(li);
}
this.container.subContainer.appendChild(fragment);
},
createAndAppendNewPageCssLink : function() {
this.cssLink = document.createElement('link');
this.cssLink.rel = "stylesheet";
this.cssLink.className = CLASS_NAME;
document.head.appendChild(this.cssLink);
},
setSubContainerLinksControll : function() {
var self = this;
var callback = function(event) {
event.preventDefault();
self.initialize(event.target.href);
self.moveSlideMenu();
};
var targetContainer = self.container.subContainer;
var links = targetContainer.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
var link = links[i];
link.addEventListener(EVENT_NAME, callback, false);
}
},
setSectionContainerLinksControll : function() {
var self = this;
var callback = function(event) {
event.preventDefault();
self.initialize(event.target.href);
};
var targetContainer = self.container.sectionContainer;
var links = targetContainer.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
var link = links[i];
link.addEventListener(EVENT_NAME, callback, false);
}
},
setFooterContainerLinksControll : function() {
var self = this;
var callback = function(event) {
event.preventDefault();
self.initialize(event.target.href);
};
var targetContainer = self.container.footerContainer;
var links = targetContainer.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
var link = links[i];
link.addEventListener(EVENT_NAME, callback, false);
}
},
moveSlideMenu : function() {
var target = this.container.mainContainer;
var callback = function(event) {
};
XXX
if (target.style.left === '50%') {
target.setAttribute('style', 'left:0px;');
} else {
target.setAttribute('style', 'left:50%;');
}
},
setHeightToContainers : function(self) {
var mainClientHeight = 0;
var subClientHeight = 0;
var MARGIN = 0;
self.container.mainContainer.style.height = 'auto';
self.container.subContainer.style.height = 'auto';
console.log("mainContainer.clientHeight:" + self.container.mainContainer.clientHeight);
mainClientHeight = self.container.mainContainer.clientHeight;
subClientHeight = self.container.subContainer.clientHeight;
var contents_height = mainClientHeight > subClientHeight ? mainClientHeight : subClientHeight;
self.container.mainContainer.setAttribute('style', 'height:' + (contents_height + MARGIN) + 'px;');
self.container.contentsContainer.setAttribute('style', 'height:' + (contents_height + MARGIN) + 'px;');
self.container.outerContainer.setAttribute('style', 'height:' + (contents_height + MARGIN ) + 'px;');
},
setCSSbodyBlock : function() {
var setBlock = function() {
document.body.setAttribute('style', 'display:block !important;');
};
if (document.body) {
setBlock();
} else {
document.addEventListener('DOMContentLoaded', setBlock, false);
}
},
TODO
setContentsTosectionContainer : function(newDom) {
var self = this;
var set = function() {
var main = newDom.getElementById('maincol');
self.container.sectionContainer.innerHTML = "";
self.container.sectionContainer.innerHTML = main.outerHTML;
self.setHeightToContainers(self);
self.setSectionContainerLinksControll();
};
if (document.body) {
set();
} else {
document.addEventListener('DOMContentLoaded', set, false);
}
},
getDomFromText : function(txt) {
var htmlDoc;
htmlDoc = document.implementation.createHTMLDocument('dom');
htmlDoc.documentElement.innerHTML = "<div>" + txt + "</div>";
console.log(htmlDoc);
return htmlDoc;
},
removeDefaultCssDisable : function() {
var head = document.head;
var sheets = document.getElementsByTagName('link');
for (var i = 0; i < sheets.length; i++) {
var sheet = sheets[i];
if (sheet.rel === "stylesheet") {
if (sheet.className !== CLASS_NAME) {
console.log(sheet);
sheet.setAttribute('disabled', true);
head.removeChild(sheet);
}
}
}
},
setNewCssToChangedPage : function(url) {
var baseURI = document.baseURI;
var pageIndex = url.split(baseURI)[1];
this.cssLink.href = PROGRAM_Directory + '/' + PAGE_CSS_JSON[pageIndex];
console.log(this.cssLink.href);
},
swipe : {
startX : 0,
diffX : 0,
startTime : 0,
endTime : 0,
swipedFlag : false
},
handleEvent : function(event) {
if (event.target === this.container.menuAnchor) {
if (event.type === EVENT_NAME) {
this.moveSlideMenu();
}
}
if (event.type != 'click') {
if (this.swipe.swipedFlag === false) {
if (event.target.offsetParent === this.container.mainContainer) {
if (event.targetTouches.length === 1) {
if (event.type === "touchstart") {
var touch = event.targetTouches[0];
this.swipe.startX = touch.pageX;
this.swipe.startTime = event.timeStamp;
}
if (event.type === "touchmove") {
var touch = event.targetTouches[0];
this.swipe.diffX = touch.pageX - this.swipe.startX;
this.swipe.endTime = event.timeStamp;
if (this.swipe.diffX >= 50) {
if ( (this.swipe.endTime - this.swipe.startTime) <= 400) {
event.preventDefault();
this.swipe.swipedFlag = true;
this.moveSlideMenu();
}
}
}
}
}
if (event.type === "touchend") {
this.swipe.startX = 0;
this.swipe.diffX = 0;
this.swipe.startTime = 0;
this.swipe.endTime = 0;
}
} else {
if (event.target.offsetParent === this.container.mainContainer) {
if (event.targetTouches.length === 1) {
if (event.type === "touchstart") {
var touch = event.targetTouches[0];
this.swipe.startX = touch.pageX;
this.swipe.startTime = event.timeStamp;
}
if (event.type === "touchmove") {
var touch = event.targetTouches[0];
this.swipe.diffX = this.swipe.startX - touch.pageX;
this.swipe.endTime = event.timeStamp;
if (this.swipe.diffX >= 15) {
if ( (this.swipe.endTime - this.swipe.startTime) <= 400) {
event.preventDefault();
this.swipe.swipedFlag = false;
this.moveSlideMenu();
}
}
}
}
}
if (event.type === "touchend") {
mLog(event.type);
this.swipe.startX = 0;
this.swipe.diffX = 0;
this.swipe.startTime = 0;
this.swipe.endTime = 0;
}
}
}
}
};
Container = function(callback) {
this._callback = callback;
this.outerContainer = null;
this.topContainer = null;
this.contentsContainer = null;
this.mainContainer = null;
this.sectionContainer = null;
this.subContainer = null;
this.footerContainer = null;
this.menuAnchor = null;
this.initialize();
};
Container.prototype = {
initialize : function() {
this.createContainers();
this.setIdToContainers();
this.appendContainers();
this.createAndAppendMenuAssy();
this._callback();
},
createContainers : function() {
this.outerContainer = document.createElement('div');
this.topContainer = document.createElement('header');
this.contentsContainer = document.createElement('div');
this.mainContainer = document.createElement('div');
this.sectionContainer = document.createElement('section');
this.subContainer = document.createElement('nav');
this.footerContainer = document.createElement('footer');
},
setIdToContainers : function() {
this.outerContainer.id = "outerContainer";
this.topContainer.id = "topContainer";
this.contentsContainer.id = "contentsContainer";
this.mainContainer.id = "mainContainer";
this.sectionContainer.id = "sectionContainer";
this.subContainer.id = "subContainer";
this.footerContainer.id = "footerContainer";
},
appendContainers : function() {
this.outerContainer.appendChild(this.topContainer);
this.outerContainer.appendChild(this.contentsContainer);
this.contentsContainer.appendChild(this.subContainer);
this.contentsContainer.appendChild(this.mainContainer);
this.mainContainer.appendChild(this.sectionContainer);
this.mainContainer.appendChild(this.footerContainer);
},
createAndAppendMenuAssy : function() {
var menuAssy = document.createElement('div');
var span = document.createElement('span');
span.innerHTML = "≡";
this.menuAnchor = document.createElement('a');
this.menuAnchor.innerHTML = "MENU";
this.menuAnchor.appendChild(span);
menuAssy.appendChild(this.menuAnchor);
this.topContainer.appendChild(menuAssy);
},
getouterContainer : function() {
return this.outerContainer;
}
};
Device = function() {
this.SMARTPHONES = ['Android', 'iphone', 'Windows Phone', 'BlackBerry', 'IEMobile'];
this.TABLETS = ['iPod', 'iPad'];
this.TARGETMAXWIDTH = 767;
this._userAgent = "";
this._deviceWidth = 0;
this.initialize();
};
Device.prototype = {
initialize : function() {
this.checkTargetUserAgent();
this.checkDeviceWidth();
},
checkTargetUserAgent : function() {
this._userAgent = navigator.userAgent;
},
checkDeviceWidth : function() {
this._deviceWidth = document.documentElement.clientWidth;
},
isTargetDevice : function() {
for (var i = 0; i < this.SMARTPHONES.length; i++) {
if (this._userAgent.indexOf(this.SMARTPHONES[i]) > 0) {
if (this._deviceWidth <= this.TARGETMAXWIDTH) {
return true;
}
}
}
return false;
},
isOperamini : function() {
if ( typeof operamini !== 'undefined') {
if (operamini) {
return true;
}
}
return false;
}
};
Main = function() {
this.device = {};
this.page = {};
this.start();
};
Main.prototype = {
start : function() {
this.device = new Device();
TODO
if (TESTFLAG != true) {
if (this.device.isTargetDevice() === false) {
this.stop();
return;
};
}
if (this.device.isOperamini() === true) {
this.stop();
return;
}
this.page = new Page(this.device);
},
stop : function() {
this.device = null;
this.page = null;
var setBlock = function() {
document.body.setAttribute('style', 'display:block !important;');
};
if (document.body) {
setBlock();
} else {
document.addEventListener('DOMContentLoaded', setBlock, false);
}
}
};
new Main();
}());