summaryrefslogtreecommitdiffstats
path: root/src/main/webapp/javascripts/fiddle_backbone/views/SchemaDef.js
blob: ddf38d0d367d693da728a80e41cc77dff1979d8e (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
define (["jQuery", "Backbone", "Handlebars", "FiddleEditor", "libs/renderTerminator", 
		 "text!fiddle_backbone/templates/schemaOutput.html", "text!fiddle_backbone/templates/schemaBrowser.html"], 
		function ($,Backbone,Handlebars,fiddleEditor,renderTerminator,schemaOutputTemplate,schemaBrowserTemplate) {

	var SchemaDefView = Backbone.View.extend({
	
		initialize: function () {

			this.editor = new fiddleEditor(this.id,this.handleSchemaChange, this, 
											_.bind(function () {
												this.model.build(); 
											}, this));

			this.compiledOutputTemplate = Handlebars.compile(schemaOutputTemplate);
			
			this.compiledSchemaBrowserTemplate = Handlebars.compile(schemaBrowserTemplate); 

		},
		handleSchemaChange: function () {
			
			if (this.model.get("ddl") != this.editor.getValue() || this.model.get("statement_separator") != $(".panel.schema .terminator").data("statement_separator")) 
			{
				this.model.set({
					"ddl":this.editor.getValue(),
					"statement_separator":$(".panel.schema .terminator").data("statement_separator"),
					"ready": false
				});

				$(".schema .helpTip").css("display",  this.model.get("ddl").length ? "none" : "block");
				$(".sql .helpTip").css("display",  (!this.model.get("ready") || this.model.get("loading")) ? "none" : "block");

			}
			
		},
		render: function () {
			this.editor.setValue(this.model.get("ddl"));
			this.updateDependents();
			renderTerminator($(".panel.schema"), this.model.get("statement_separator"));			
		},
		renderOutput: function() {
			this.options.output_el.html(
				this.compiledOutputTemplate(this.model.toJSON())
			);		
		},
		renderSchemaBrowser: function () {
			this.options.browser_el.html(
				this.compiledSchemaBrowserTemplate({
					"objects": this.model.get('schema_structure')
				})
			);					
		},
		refresh: function () {
			this.editor.refresh();
		},
		updateDependents: function () {

			if (this.model.get("ready"))
			{
				$(".needsReadySchema").unblock();
				$("#schemaBrowser").attr("disabled", false);
				$(".schema .helpTip").css("display",  "none");				
				//$(".sql .helpTip").css("display",  (this.model.get('loading') || window.query.get("sql").length) ? "none" : "block");
			}
			else
			{
				$(".needsReadySchema").block({ message: "Please build schema." });
				$("#schemaBrowser").attr("disabled", true);
				$(".schema .helpTip").css("display",  (this.model.get('loading') || this.model.get("ddl").length) ? "none" : "block");
				
			}
			
		}
	
	});
	
	return SchemaDefView;
	
});