summaryrefslogtreecommitdiffstats
path: root/web/widget/abstract/fields.js
blob: a2ec5e0bed04134abb10b76bd0bb8cf4ef4fa3ea (plain)
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
/*
 * Copyright (c) 2012-2014 Kaarle Ritvanen
 * See LICENSE file for license details
 */

define(
    [
	"acf2/widget/abstract/node",
	"jquery",
	"underscore",
	"acf2/widget/checkbox",
	"acf2/widget/checkboxes",
	"acf2/widget/combobox",
	"acf2/widget/date",
	"acf2/widget/field",
	"acf2/widget/inline",
	"acf2/widget/reference"
    ],
    function(Base, $, _) {
	return Base.extend({
	    render: function(data, meta) {
		Base.render.call(this, data, meta);

		this.appendAboveFields(data, meta);

		this.reqData = data;
		var self = this;

		if (meta.type == "model") {
		    this.fields = {};
		    _.each(meta.fields, function(field) {
			if (field.visible)
			    self.fields[field.name] = self.renderField(
				field.name,
				field,
				field["ui-name"],
				true,
				false
			    );
		    });

		    _.each(this.fields, function(f1, name) {
			_.each(self.fields, function(f2) {
			    if (f1 != f2)
				f1.on("validated", function(event) {
				    f2.trigger("updated", [name]);
				    event.stopPropagation();
				});
			});
		    });

		    _.each(this.fields, function(field) {
			field.trigger("start");
		    });
		}

		else _.each(data.data, function(value, name) {
		    if (meta.type == "set") name = data.data[name];
		    else if (_.isArray(data.data)) name++;
		    self.renderCollectionMember(name, meta);
		});

		this.appendBelowFields(data, meta);
	    },

	    appendAboveFields: function(data, meta) {},
	    appendBelowFields: function(data, meta) {},

	    renderField: function(
		name, meta, label, editable, removable
	    ) {
		var widget = this.widget(meta).new(
		    this.reqData,
		    name,
		    meta,
		    this.level,
		    editable,
		    removable
		);
		var container = this.appendWidget(
		    widget, label, meta.description
		);
		widget.on("setVisible", function(event, visible) {
		    if (visible) container.show();
		    else container.hide();
		    event.stopPropagation();
		});
		return widget;
	    },

	    renderCollectionMember: function(name, meta) {
		var set = meta.type == "set";
		this.renderField(
		    name,
		    meta.members,
		    meta["ui-member"] + " " + name,
		    !set,
		    !set && _.contains(meta.removable, name)
		).trigger("start");
	    },

	    widget: function(meta) {
		return require("acf2/widget/" + meta.widget);
	    }
	});
    }
);