/** * All editors should extend from this class */ JSONEditor.AbstractEditor = Class.extend({ onChildEditorChange: function(editor) { this.onChange(true); }, notify: function() { this.jsoneditor.notifyWatchers(this.path); }, change: function() { if(this.parent) this.parent.onChildEditorChange(this); else this.jsoneditor.onChange(); }, onChange: function(bubble) { this.notify(); if(this.watch_listener) this.watch_listener(); if(bubble) this.change(); }, register: function() { this.jsoneditor.registerEditor(this); this.onChange(); }, unregister: function() { if(!this.jsoneditor) return; this.jsoneditor.unregisterEditor(this); }, getNumColumns: function() { return 12; }, init: function(options) { this.jsoneditor = options.jsoneditor; this.theme = this.jsoneditor.theme; this.template_engine = this.jsoneditor.template; this.iconlib = this.jsoneditor.iconlib; this.translate = this.jsoneditor.translate || JSONEditor.defaults.translate; this.original_schema = options.schema; this.schema = this.jsoneditor.expandSchema(this.original_schema); this.options = $extend({}, (this.options || {}), (options.schema.options || {}), options); if(!options.path && !this.schema.id) this.schema.id = 'root'; this.path = options.path || 'root'; this.formname = options.formname || this.path.replace(/\.([^.]+)/g,'[$1]'); if(this.jsoneditor.options.form_name_root) this.formname = this.formname.replace(/^root\[/,this.jsoneditor.options.form_name_root+'['); this.key = this.path.split('.').pop(); this.parent = options.parent; this.link_watchers = []; if(options.container) this.setContainer(options.container); }, setContainer: function(container) { this.container = container; if(this.schema.id) this.container.setAttribute('data-schemaid',this.schema.id); if(this.schema.type && typeof this.schema.type === "string") this.container.setAttribute('data-schematype',this.schema.type); this.container.setAttribute('data-schemapath',this.path); }, preBuild: function() { }, build: function() { }, postBuild: function() { this.setupWatchListeners(); this.addLinks(); this.setValue(this.getDefault(), true); this.updateHeaderText(); this.register(); this.onWatchedFieldChange(); }, setupWatchListeners: function() { var self = this; // Watched fields this.watched = {}; if(this.schema.vars) this.schema.watch = this.schema.vars; this.watched_values = {}; this.watch_listener = function() { if(self.refreshWatchedFieldValues()) { self.onWatchedFieldChange(); } }; this.register(); if(this.schema.hasOwnProperty('watch')) { var path,path_parts,first,root,adjusted_path; for(var name in this.schema.watch) { if(!this.schema.watch.hasOwnProperty(name)) continue; path = this.schema.watch[name]; if(Array.isArray(path)) { if(path.length<2) continue; path_parts = [path[0]].concat(path[1].split('.')); } else { path_parts = path.split('.'); if(!self.theme.closest(self.container,'[data-schemaid="'+path_parts[0]+'"]')) path_parts.unshift('#'); } first = path_parts.shift(); if(first === '#') first = self.jsoneditor.schema.id || 'root'; // Find the root node for this template variable root = self.theme.closest(self.container,'[data-schemaid="'+first+'"]'); if(!root) throw "Could not find ancestor node with id "+first; // Keep track of the root node and path for use when rendering the template adjusted_path = root.getAttribute('data-schemapath') + '.' + path_parts.join('.'); self.jsoneditor.watch(adjusted_path,self.watch_listener); self.watched[name] = adjusted_path; } } // Dynamic header if(this.schema.headerTemplate) { this.header_template = this.jsoneditor.compileTemplate(this.schema.headerTemplate, this.template_engine); } }, addLinks: function() { // Add links if(!this.no_link_holder) { this.link_holder = this.theme.getLinksHolder(); this.container.appendChild(this.link_holder); if(this.schema.links) { for(var i=0; i=0) { holder = this.theme.getBlockLinkHolder(); link = this.theme.getBlockLink(); link.setAttribute('target','_blank'); var media = document.createElement(type); media.setAttribute('controls','controls'); this.theme.createMediaLink(holder,link,media); // When a watched field changes, update the url this.link_watchers.push(function(vars) { var url = href(vars); link.setAttribute('href',url); link.textContent = data.rel || url; media.setAttribute('src',url); }); } // Text links else { link = holder = this.theme.getBlockLink(); holder.setAttribute('target','_blank'); holder.textContent = data.rel; // When a watched field changes, update the url this.link_watchers.push(function(vars) { var url = href(vars); holder.setAttribute('href',url); holder.textContent = data.rel || url; }); } if(download && link) { if(download === true) { link.setAttribute('download',''); } else { this.link_watchers.push(function(vars) { link.setAttribute('download',download(vars)); }); } } if(data.class) link.className = link.className + ' ' + data.class; return holder; }, refreshWatchedFieldValues: function() { if(!this.watched_values) return; var watched = {}; var changed = false; var self = this; if(this.watched) { var val,editor; for(var name in this.watched) { if(!this.watched.hasOwnProperty(name)) continue; editor = self.jsoneditor.getEditor(this.watched[name]); val = editor? editor.getValue() : null; if(self.watched_values[name] !== val) changed = true; watched[name] = val; } } watched.self = this.getValue(); if(this.watched_values.self !== watched.self) changed = true; this.watched_values = watched; return changed; }, getWatchedFieldValues: function() { return this.watched_values; }, updateHeaderText: function() { if(this.header) { // If the header has children, only update the text node's value if(this.header.children.length) { for(var i=0; i -1; else if(this.jsoneditor.options.required_by_default) return true; else return false; }, getDisplayText: function(arr) { var disp = []; var used = {}; // Determine how many times each attribute name is used. // This helps us pick the most distinct display text for the schemas. $each(arr,function(i,el) { if(el.title) { used[el.title] = used[el.title] || 0; used[el.title]++; } if(el.description) { used[el.description] = used[el.description] || 0; used[el.description]++; } if(el.format) { used[el.format] = used[el.format] || 0; used[el.format]++; } if(el.type) { used[el.type] = used[el.type] || 0; used[el.type]++; } }); // Determine display text for each element of the array $each(arr,function(i,el) { var name; // If it's a simple string if(typeof el === "string") name = el; // Object else if(el.title && used[el.title]<=1) name = el.title; else if(el.format && used[el.format]<=1) name = el.format; else if(el.type && used[el.type]<=1) name = el.type; else if(el.description && used[el.description]<=1) name = el.descripton; else if(el.title) name = el.title; else if(el.format) name = el.format; else if(el.type) name = el.type; else if(el.description) name = el.description; else if(JSON.stringify(el).length < 50) name = JSON.stringify(el); else name = "type"; disp.push(name); }); // Replace identical display text with "text 1", "text 2", etc. var inc = {}; $each(disp,function(i,name) { inc[name] = inc[name] || 0; inc[name]++; if(used[name] > 1) disp[i] = name + " " + inc[name]; }); return disp; }, getOption: function(key) { try { throw "getOption is deprecated"; } catch(e) { window.console.error(e); } return this.options[key]; }, showValidationErrors: function(errors) { } });