전체 페이지뷰

2013년 12월 13일 금요일

superCookie example

// ie8  false check
// console.log("$.super_cookie().verify :::"+ $.super_cookie().verify("recentDocument"));

RecentKeyword = function() {};

RecentKeyword.prototype = {
setKeyword : function(keyword) {
this.keyword = keyword;
},
getKeyword : function() {
return this.keyword;
},
setDocumentList : function(documentList) {
this.documentList = documentList;
},
getDocumentList : function() {
return this.documentList;
}
};

RecentDocument = function() {};

RecentDocument.prototype = {
setKey : function (key) {
this.key = key;
}
,getKey : function () {
return this.key;
}
,setLink : function (link) {
this.link = link;
}
,getLink : function () {
return this.link;
}
,setTitle : function (title) {
this.title = title;
}
,getTitle : function () {
return this.title;
}
};


/**
 * save recent document to max 5
 * @param keyword
 * @param docKey
 * @param titleNm
 * @param linkNm
 */

function setRecentDocument(keyword , docKey, titleNm, linkNm)
{

var object  = {};

if (keyword != "" ){

if ($.super_cookie().check("recentDocument")) {
object = $.super_cookie().read_JSON("recentDocument");

var size = _.size(object);
console.log("size ::::  "+size);

for(var i = 0; i < size; ++i){
var data = object[i];
console.log("setRecentDocument :: "+data.keyword);

if (data.keyword == keyword){
console.log("setRecentDocument keyword ::" + keyword);
var documentList = data.documentList;
var innersize =  _.size(documentList);

var doc = new RecentDocument();

doc.setKey(docKey);
doc.setLink(linkNm);
doc.setTitle(titleNm);
object[i].documentList.push(doc);

console.log("data.Documentlist:::: add :::: "+JSON.stringify(documentList[innersize]));
}
}
}

$.super_cookie({expires: 7,path: "/"}).create("recentDocument",object);
}

getRecentDocumentList();

}

/**
 * save recent keyword to max 5
 * @param keyword
 */

///////////////////////////
//
function setRecentDocumentKeyword (keyword)
{
if (keyword != "" ){
var object  = new Array();
if ($.super_cookie().check("recentDocument")) {
object = $.super_cookie().read_JSON("recentDocument");

var size = _.size(object);
var flag = false;
console.log("size ::::  "+size);

for(var i = 0; i < size; ++i){
var data = object[i];
console.log(data.keyword);
if (data.keyword == keyword){
console.log("keyword ::" + keyword);

var tempObj = object[i];
object = _.without(object, _.findWhere(object, {keyword: keyword}));
object.push(tempObj);

flag = true;
}
}

if (flag == false){

if (size == 5){
object.splice(0,1);
}
var obj = new RecentKeyword();

obj.setKeyword(keyword);
var arrDocument = new Array();
obj.setDocumentList(arrDocument);
object.push(obj);
console.log("object : Stringfy :: "+size +"::::"+JSON.stringify(object));
}

}else {
var obj = new RecentKeyword();

obj.setKeyword(keyword);
var arrDocument = new Array();
obj.setDocumentList(arrDocument);
object.push(obj);

console.log("object : Stringfy ::Default::::"+JSON.stringify(object));
}
$.super_cookie({expires: 7,path: "/"}).create("recentDocument",object);
}

getRecentDocumentList();
}
//////////////////////////


/**
 * show recent keyword / document list to max 5
 */
function getRecentDocumentList()
{
// ie8 false check
// console.log("$.super_cookie().verify :::"+ $.super_cookie().verify("recentDocument"));

if ($.super_cookie().check("recentDocument"))
{
var object = $.super_cookie().read_JSON("recentDocument");
object = _.clone(object).reverse();
// object = object.reverse();
var size = _.size(object);
var html = "";

console.log("size ::"+size);
if (size > 0){
html += '<ul class="num_list">';
html += '<div class="content">';
for(var i = 0; i < size; ++i){

var data = object[i];

if (data != undefined && data != null){

var num = i+1;
var num_color = '';
if(num < 3 )
{
num_color = 'num_color1';
}
else
{
num_color = 'num_color2';
}

console.log("getRecentDocumentList :  keyword :: "+data.keyword);
html += '<li><div class="'+num_color+'"><span>'+num+'</span></div><a class="tooltip" data-href="#'+data.keyword+'" onclick="javascript:linkRecentKeyword(\'' + data.keyword + '\');" class="keyword">'+data.keyword+'</a></li>';

html += '<div id ="'+data.keyword+'" style="display: none">';

if (data.documentList != undefined && data.documentList != ""){
var documentList = data.documentList;
var in_size = _.size(documentList);

console.log("in_size ::"+in_size);
for( var j=0; j< in_size; j++ ) {
console.log(documentList[j].key);
console.log("getRecentDocumentList :  document :: "+documentList[j].title);
html += '<li><a href="'+documentList[j].link+'" >'+documentList[j].title+'</a></li>';
}
}
html += '</div>';
}
}

html += '</ul>';
$('#recentDocumentList').html(html);

}
}
var id = "";
$(".tooltip").mouseover(function(){
id = $(this).attr("data-href");
$(id).show();

$( id ).mouseleave(function() {
$( id ).hide();
});
}).mouseout(function(){
// id = $(this).attr("data-href");
// $( id ).hide();
}).click(function(){
id = $(this).attr("data-href");
$(id).hide();
});

}

댓글 없음:

댓글 쓰기