-
Notifications
You must be signed in to change notification settings - Fork 101
/
jquery.ui.colorpicker-memory.js
80 lines (75 loc) · 2.18 KB
/
jquery.ui.colorpicker-memory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
;jQuery(function($) {
$.colorpicker.parts.memory = function (inst) {
var that = this,
container,
selectNode = function(node) {
inst.color = inst._parseColor($(node).css('backgroundColor'));
inst._change();
},
deleteNode = function(node) {
node.remove();
},
addNode = function(color) {
var $node = $('<div/>').addClass('ui-colorpicker-swatch').css('backgroundColor', color);
$node.mousedown(function(e) {
e.stopPropagation();
if (!inst.options.disabled) {
switch (e.which) {
case 1:
selectNode(this);
break;
case 3:
deleteNode($node);
setMemory();
break;
}
}
}).on('contextmenu', function(e) {
e.preventDefault();
});
container.append($node);
},
getMemory = function() {
if (window.localStorage) {
var memory = localStorage.getItem('colorpicker-memory');
if (memory) {
return JSON.parse(memory);
}
}
return $.map((document.cookie.match(/\bcolorpicker-memory=([^;]*)/) || [0, ''])[1].split(','),unescape);
};
setMemory = function() {
var colors = [];
$('> *', container).each(function() {
colors.push($(this).css('backgroundColor'));
});
if (window.localStorage) {
localStorage.setItem('colorpicker-memory',JSON.stringify(colors));
}
else {
var expdate=new Date();
expdate.setDate(expdate.getDate() + (365 * 10));
document.cookie = 'colorpicker-memory='+$.map(colors,escape).join()+';expires='+expdate.toUTCString();
}
};
this.init = function () {
container = $('<div/>')
.addClass('ui-colorpicker-memory ui-colorpicker-border ui-colorpicker-swatches')
.css({
width: 84,
height: 84,
cursor: 'crosshair'
})
.appendTo($('.ui-colorpicker-memory-container', inst.dialog));
$.each(getMemory(), function() {
addNode(this);
});
container.mousedown(function(e) {
if (!inst.options.disabled) {
addNode(inst.color.toCSS());
setMemory();
}
});
};
};
});