1 var MongoString = function() {2 var that = this;3 4 this.dom = document.createElement('div');5 this.dom.setAttribute('contentEditable', true);6 this.dom.setAttribute('component', 'MongoString');7 8 this.setData = function(data) {9 that.dom.innerHTML = data; // TODO: change this by contentText10 };11 12 };13 14 var MongoNumber = function() {15 var that = this;16 17 this.dom = document.createElement('div');18 this.dom.setAttribute('contentEditable', true);19 this.dom.setAttribute('component', 'MongoNumber');20 21 this.dom.addEventListener('keyup', function(){22 var sanitized = ""+parseFloat(this.innerHTML);23 if (this.innerHTML != sanitized) {24 this.innerHTML = sanitized;25 console.log("no es un número");26 }27 }, true);28 29 this.setData = function(data) {30 that.dom.innerHTML = data; // TODO: change this by contentText31 };32 };33 34 var MongoBoolean = function() {35 var that = this;36 37 this.dom = document.createElement('div');38 this.dom.setAttribute('component', 'MongoBoolean');39 40 this.check = document.createElement('input');41 this.check.setAttribute('type', 'checkbox');42 this.dom.appendChild(this.check);43 44 this.setData = function(data) {45 that.check.checked = data;46 };47 };48 49 var MongoId = function() {50 var that = this;51 52 this.dom = document.createElement('div');53 this.dom.setAttribute('component', 'MongoId');54 55 this.setData = function(data) {56 that.dom.innerHTML = data; // TODO: change this by contentText57 };58 };59 60 var MongoNull = function() {61 var that = this;62 63 this.dom = document.createElement('div');64 this.dom.setAttribute('component', 'MongoNull');65 this.dom.innerHTML = 'null';66 };67 68 var MongoUndefined = function() {69 var that = this;70 71 this.dom = document.createElement('div');72 this.dom.setAttribute('component', 'MongoUndefined');73 this.dom.innerHTML = 'undefined';74 };75 76 var MongoKeyValue = function() {77 var that = this;78 79 var key = '';80 81 this.dom = document.createElement('div');82 this.dom.setAttribute('component', 'MongoKeyValue');83 84 this.key = document.createElement('div');85 this.key.classList.add('key');86 this.dom.appendChild(this.key);87 88 this.value = document.createElement('div');89 this.value.classList.add('value');90 this.dom.appendChild(this.value);91 92 this.setKey = function(k) {93 key = k;94 that.key.innerHTML = k; // TODO user contentText (check spelling)95 };96 };97 98 var MongoArray = function() {99 100 var that = this;101 102 this.items = null;103 104 this.dom = document.createElement('div');105 this.dom.setAttribute('component', 'MongoArray');106 107 this.setData = function(data) {108 // TODO: clear this109 for (i in data) { // TODO: change i by key110 var type = typeof data[i];111 if ('_id' == i) {112 type = 'id';113 } else if ('object' == type && data[i] instanceof Array) {114 type = 'array';115 } else if ('object' == type && null === data[i]) {116 type = 'null';117 }118 119 var mkv = new MongoKeyValue();120 mkv.setKey(i);121 122 switch (type) {123 case 'id':124 var mi = new MongoId();125 mi.setData(data[i]);126 mkv.value.appendChild(mi.dom);127 break;128 case 'undefined':129 var mu = new MongoUndefined();130 mkv.value.appendChild(mu.dom);131 break;132 case 'null':133 var mn = new MongoNull();134 mkv.value.appendChild(mn.dom);135 break;136 case 'boolean':137 var mb = new MongoBoolean();138 mb.setData(data[i]);139 mkv.value.appendChild(mb.dom);140 break;141 case 'string':142 var ms = new MongoString();143 ms.setData(data[i]);144 mkv.value.appendChild(ms.dom);145 break;146 case 'number':147 var mn = new MongoNumber();148 mn.setData(data[i]);149 mkv.value.appendChild(mn.dom);150 break;151 case 'object':152 var mo = new MongoDocument();153 mo.setData(data[i]);154 mkv.value.appendChild(mo.dom);155 break;156 case 'array':157 var ma = new MongoArray();158 ma.setData(data[i]);159 mkv.value.appendChild(ma.dom);160 break;161 }162 163 that.dom.appendChild(mkv.dom);164 }165 };166 };167 168 var MongoDocument = function() {169 var that = this;170 171 this.items = null;172 173 this.dom = document.createElement('div');174 this.dom.setAttribute('component', 'MongoDocument');175 176 this.setData = function(data) {177 // TODO: clear this178 for (i in data) { // TODO: change i by key179 var type = typeof data[i];180 if ('_id' == i) {181 type = 'id';182 } else if ('object' == type && data[i] instanceof Array) {183 type = 'array';184 } else if ('object' == type && null === data[i]) {185 type = 'null';186 }187 188 var mkv = new MongoKeyValue();189 mkv.setKey(i);190 191 switch (type) {192 case 'id':193 var mi = new MongoId();194 mi.setData(data[i]);195 mkv.value.appendChild(mi.dom);196 break;197 case 'undefined':198 var mu = new MongoUndefined();199 mkv.value.appendChild(mu.dom);200 break;201 case 'null':202 var mn = new MongoNull();203 mkv.value.appendChild(mn.dom);204 break;205 case 'boolean':206 var mb = new MongoBoolean();207 mb.setData(data[i]);208 mkv.value.appendChild(mb.dom);209 break;210 case 'string':211 var ms = new MongoString();212 ms.setData(data[i]);213 mkv.value.appendChild(ms.dom);214 break;215 case 'number':216 var mn = new MongoNumber();217 mn.setData(data[i]);218 mkv.value.appendChild(mn.dom);219 break;220 case 'object':221 var mo = new MongoDocument();222 mo.setData(data[i]);223 mkv.value.appendChild(mo.dom);224 break;225 case 'array':226 var ma = new MongoArray();227 ma.setData(data[i]);228 mkv.value.appendChild(ma.dom);229 break;230 }231 232 that.dom.appendChild(mkv.dom);233 }234 };235 };236 237 var MongoCollection = function() {238 239 var that = this;240 241 this.documents = [];242 243 this.dom = document.createElement('div');244 this.dom.setAttribute('component', 'MongoCollection');245 246 this.SetData = function(data) {247 // TODO: destroy all items248 for (i in data) {249 var d = data[i];250 var item = new MongoDocument();251 item.setData(d);252 that.dom.appendChild(item.dom);253 }254 };255 256 };
Enlace
El enlace para compartir es: