svghmi/svghmi.js
branchsvghmi
changeset 2897 bf8a39cc65e4
parent 2896 99c5335ed59f
child 2899 f1baf2d0552c
equal deleted inserted replaced
2896:99c5335ed59f 2897:bf8a39cc65e4
     1 // svghmi.js
     1 // svghmi.js
     2 
     2 
     3 var cache = hmitree_types.map(_ignored => undefined);
     3 var cache = hmitree_types.map(_ignored => undefined);
     4 var updates = {};
     4 var updates = {};
       
     5 var need_cache_apply = []; 
     5 
     6 
     6 function dispatch_value_to_widget(widget, index, value, oldval) {
     7 function dispatch_value_to_widget(widget, index, value, oldval) {
     7     try {
     8     try {
     8         let idx = widget.offset ? index - widget.offset : index;
     9         let idx = widget.offset ? index - widget.offset : index;
     9         let idxidx = widget.indexes.indexOf(idx);
    10         let idxidx = widget.indexes.indexOf(idx);
    85     // Do the page swith if any one pending
    86     // Do the page swith if any one pending
    86     if(current_subscribed_page != current_visible_page){
    87     if(current_subscribed_page != current_visible_page){
    87         switch_visible_page(current_subscribed_page);
    88         switch_visible_page(current_subscribed_page);
    88     }
    89     }
    89 
    90 
    90     if(current_subscribed_page_index != current_visible_page_index){
    91     while(widget = need_cache_apply.pop()){
    91         apply_cache();
    92         widget.apply_cache();
    92     }
    93     }
       
    94 
    93     apply_updates();
    95     apply_updates();
    94     requestAnimationFrameID = null;
    96     requestAnimationFrameID = null;
    95 }
    97 }
    96 
    98 
    97 function requestHMIAnimation() {
    99 function requestHMIAnimation() {
   255     return new_val;
   257     return new_val;
   256 }
   258 }
   257 
   259 
   258 var current_visible_page;
   260 var current_visible_page;
   259 var current_subscribed_page;
   261 var current_subscribed_page;
   260 var current_visible_page_index;
       
   261 var current_subscribed_page_index;
       
   262 
   262 
   263 function prepare_svg() {
   263 function prepare_svg() {
   264     for(let eltid in detachable_elements){
   264     for(let eltid in detachable_elements){
   265         let [element,parent] = detachable_elements[eltid];
   265         let [element,parent] = detachable_elements[eltid];
   266         parent.removeChild(element);
   266         parent.removeChild(element);
   299     this.offset = new_offset;
   299     this.offset = new_offset;
   300     /* add this's subsribers */
   300     /* add this's subsribers */
   301     for(let index of this.indexes){
   301     for(let index of this.indexes){
   302         subscribers[index + new_offset].add(this);
   302         subscribers[index + new_offset].add(this);
   303     }
   303     }
       
   304     need_cache_apply.push(this); 
   304 }
   305 }
   305 
   306 
   306 function foreach_unsubscribe(){
   307 function foreach_unsubscribe(){
   307     for(let item of this.items){
   308     for(let item of this.items){
   308         for(let widget of item) {
   309         for(let widget of item) {
   309             unsubscribe.call(widget);
   310             unsubscribe.call(widget);
   310         }
   311         }
   311     }
   312     }
   312 }
   313     this.offset = 0;
   313 
   314 }
   314 function foreach_subscribe(new_offset=0){
   315 
       
   316 function foreach_widgets_do(new_offset, todo){
       
   317     this.offset = new_offset;
   315     for(let i = 0; i < this.items.length; i++) {
   318     for(let i = 0; i < this.items.length; i++) {
   316         let item = this.items[i];
   319         let item = this.items[i];
   317         let orig_item_index = this.index_pool[i];
   320         let orig_item_index = this.index_pool[i];
   318         let item_index = this.index_pool[i+this.item_offset];
   321         let item_index = this.index_pool[i+this.item_offset];
   319         let item_index_offset = item_index - orig_item_index;
   322         let item_index_offset = item_index - orig_item_index;
   320         for(let widget of item) {
   323         for(let widget of item) {
   321             subscribe.call(widget,new_offset + item_index_offset);
   324             todo.call(widget, new_offset + item_index_offset);
   322         }
   325         }
   323     }
   326     }
       
   327 }
       
   328 
       
   329 function foreach_subscribe(new_offset=0){
       
   330     foreach_widgets_do.call(this, new_offset, subscribe);
       
   331 }
       
   332 
       
   333 function widget_apply_cache() {
       
   334     for(let index of this.indexes){
       
   335         /* dispatch current cache in newly opened page widgets */
       
   336         let realindex = index+this.offset;
       
   337         let cached_val = cache[realindex];
       
   338         if(cached_val != undefined)
       
   339             dispatch_value_to_widget(this, realindex, cached_val, cached_val);
       
   340     }
       
   341 }
       
   342 
       
   343 function foreach_apply_cache() {
       
   344     foreach_widgets_do.call(this, this.offset, widget_apply_cache);
       
   345 }
       
   346 
       
   347 function foreach_onclick(opstr, evt) {
       
   348     new_item_offset = eval(String(this.item_offset)+opstr)
       
   349     if(new_item_offset + this.items.length > this.index_pool.length) {
       
   350         new_item_offset = 0;
       
   351     } else if(new_item_offset < 0) {
       
   352         new_item_offset = this.index_pool.length - this.items.length;
       
   353     }
       
   354     this.item_offset = new_item_offset;
       
   355     off = this.offset;
       
   356     foreach_unsubscribe.call(this);
       
   357     foreach_subscribe.call(this,off);
       
   358     update_subscriptions();
       
   359     need_cache_apply.push(this);
       
   360     requestHMIAnimation();
       
   361     console.log(opstr, new_item_offset);
   324 }
   362 }
   325 
   363 
   326 function switch_subscribed_page(page_name, page_index) {
   364 function switch_subscribed_page(page_name, page_index) {
   327     let old_desc = page_desc[current_subscribed_page];
   365     let old_desc = page_desc[current_subscribed_page];
   328     let new_desc = page_desc[page_name];
   366     let new_desc = page_desc[page_name];
   345     new_desc.relative_widgets.map(w=>w.sub(new_offset));
   383     new_desc.relative_widgets.map(w=>w.sub(new_offset));
   346 
   384 
   347     update_subscriptions();
   385     update_subscriptions();
   348 
   386 
   349     current_subscribed_page = page_name;
   387     current_subscribed_page = page_name;
   350     current_subscribed_page_index = page_index;
       
   351 
   388 
   352     requestHMIAnimation();
   389     requestHMIAnimation();
   353 }
   390 }
   354 
   391 
   355 function switch_visible_page(page_name) {
   392 function switch_visible_page(page_name) {
   378     }
   415     }
   379 
   416 
   380     svg_root.setAttribute('viewBox',new_desc.bbox.join(" "));
   417     svg_root.setAttribute('viewBox',new_desc.bbox.join(" "));
   381     current_visible_page = page_name;
   418     current_visible_page = page_name;
   382 };
   419 };
   383     
       
   384 function apply_cache() {
       
   385     let new_desc = page_desc[current_visible_page];
       
   386     for(let widget of chain(new_desc.absolute_widgets,new_desc.relative_widgets)){
       
   387         for(let index of widget.indexes){
       
   388             /* dispatch current cache in newly opened page widgets */
       
   389             let realindex = index+widget.offset;
       
   390             let cached_val = cache[realindex];
       
   391             if(cached_val != undefined)
       
   392                 dispatch_value_to_widget(widget, realindex, cached_val, cached_val);
       
   393         }
       
   394     }
       
   395     current_visible_page_index = current_subscribed_page_index;
       
   396 }
       
   397 
       
   398 
       
   399 
   420 
   400 // Once connection established
   421 // Once connection established
   401 ws.onopen = function (evt) {
   422 ws.onopen = function (evt) {
   402     init_widgets();
   423     init_widgets();
   403     send_reset();
   424     send_reset();