This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCssEditor.js
122 lines (105 loc) · 4.17 KB
/
CssEditor.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// JQUERY 1.4.4 is a minimum requirement
(function ($) {
$.fn.tmplEditor = function (options) {
// there's no need to do $(this) because
// "this" is already a jquery object
var formHolder = this;
var settings = {
dataNamespace: 'data-editable',
debug: false,
callback: function () { alert("Callback called"); return false; }
};
if (options) {
$.extend(settings, options);
}
var rules = [];
var setRule = function (elementSelector, rule, value) {
if (!(rules[elementSelector])) {
rules[elementSelector] = [];
}
rules[elementSelector][rule] = value;
if (settings.debug) {
updateCss();
console.log(rules);
}
}
var updateCss = function () {
var css = "";
for (elem in rules) {
css += elem;
css += ' { ';
for (rule in rules[elem]) {
css += rule;
css += ':';
css += rules[elem][rule];
css += ';';
}
css += ' }<br />';
}
currentCss.html(css);
}
var getFormElement = function (key, elemId) {
switch (key) {
case "size":
// TODO: use a number cycler control
var input = $('<input />').attr('id', key + "_" + elemId).val("SIZE INPUT");
input.change(function () {
var el = '#' + elemId;
var rule = 'font-size';
var value = $(this).val();
setRule(el, rule, value + 'px');
$(el).css(rule, parseInt(value));
});
return input;
case "fgcolor":
// TODO: use a color picker control
var input = $('<input />').attr('id', key + "_" + elemId).val("COLOR INPUT");
input.change(function () {
var el = '#' + elemId;
var rule = 'color';
var value = $(this).val();
setRule(el, rule, value);
$(el).css(rule, value);
});
return input;
case "bgcolor":
// TODO: use a color picker control
var input = $('<input />').attr('id', key + "_" + elemId).val("COLOR INPUT");
input.change(function () {
var el = '#' + elemId;
var rule = 'background-color';
var value = $(this).val();
setRule(el, rule, value);
$(el).css(rule, value);
});
return input;
default:
return $('<input />').attr('id', key + "_" + elemId).val("DEFAULT INPUT");
}
};
var frm = $("<form />");
var currentCss = $("<div id='current-css' />");
var subBtn = $("<input type='submit' />").click(settings.callback);
// Get all elements with the namespace attribute
$('[' + settings.dataNamespace + ']').each(function () {
// Get the object passed into the data-* attribute
var $this = $(this);
var values = $this.data('editable').split(',');
var h2 = $("<h2 />").html($this.attr('id'));
var ul = $("<ul />");
for (var value in values) {
if (values.hasOwnProperty(value)) {
var label = $("<label />").html(values[value]);
var element = getFormElement(values[value], $this.attr('id'));
var li = $("<li />").append(label).append(element);
ul.append(li);
}
}
frm.append(h2);
frm.append(ul);
});
frm.append(subBtn);
formHolder.html(frm);
formHolder.after(currentCss);
};
})(jQuery);