Changeset 32769

Show
Ignore:
Timestamp:
12.02.2019 12:44:28 (7 days ago)
Author:
ak19
Message:

Map editor history changed into an object prototype (class).

Location:
main/trunk/greenstone3/web/interfaces/default/js
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/web/interfaces/default/js/map-scripts-editor-history.js

    r32766 r32769  
    1 var prevOverlays = []; 
    2 var presentOverlays = []; 
    3 var undoneOverlays = []; 
    4 var cycleComplete = true; 
     1//var prevOverlays = []; 
     2//var presentOverlays = []; 
     3//var undoneOverlays = []; 
     4//var cycleComplete = true; 
    55 
    66function MapEditorHistory() 
     
    1212} 
    1313 
    14 MapEditorHistory.prototype.undo = function(overlays) { 
    15     for (var i = 0; i < overlays.length; i++) { 
     14MapEditorHistory.prototype.undo = function() { 
     15    for (let i = 0; i < overlays.length; i++) { 
    1616        overlays[i].setMap(null); 
    1717    } 
     
    3434 
    3535     
    36 function undo() { 
    37  
    38     for (var i = 0; i < overlays.length; i++) { 
    39         overlays[i].setMap(null); 
    40     } 
    41     if (prevOverlays.length != 0) { 
     36 
     37 
     38MapEditorHistory.prototype.redo = function() { 
     39 
     40    if (this.undoneOverlays.length != 0) { 
    4241        selectedShapes = []; 
     42        for (let i = 0; i < overlays.length; i++) { 
     43            overlays[i].setMap(null); 
     44        } 
     45 
    4346        overlays = []; 
    44         undoneOverlaysPush(); 
    45         var prev_overlay = prevOverlays.pop(); 
    46  
    47         if (prev_overlay.length > 0) { 
    48             for (var i = 0; i < prev_overlay.length; i++) { 
    49                 overlays[i] = prev_overlay[i]; 
    50                 overlays[i].setMap(map); 
    51                 overlays[i].draggable = draggableState; 
    52             } 
    53         } 
    54     } 
    55 } 
    56  
    57 function redo() { 
    58  
    59     if (undoneOverlays.length != 0) { 
    60         selectedShapes = []; 
    61         for (var i = 0; i < overlays.length; i++) { 
    62             overlays[i].setMap(null); 
    63         } 
    64  
    65         overlays = []; 
    66         var lastEntry = undoneOverlays[undoneOverlays.length - 1]; 
    67         for (var i = 0; i < lastEntry.length; i++) { 
     47        var lastEntry = this.undoneOverlays[this.undoneOverlays.length - 1]; 
     48        for (let i = 0; i < lastEntry.length; i++) { 
    6849            overlays[i] = lastEntry[i]; 
    6950            overlays[i].setMap(map); 
     
    7152        } 
    7253 
    73         var conditionPrevious = presentOverlays[0]; 
     54        var conditionPrevious = this.presentOverlays[0]; 
    7455        if (conditionPrevious !== undefined) { 
    7556            if (conditionPrevious.length == 0) { 
    76                 prevOverlays.push(presentOverlays[0]); 
     57                this.prevOverlays.push(this.presentOverlays[0]); 
    7758            } else { 
    7859                var overlays_copy = []; 
    79                 for (var i = 0; i < presentOverlays[0].length; i++) { 
    80                     var clone_shape = cloneShape(presentOverlays[0][i]); 
    81                     if (overlayItemIsShape(clone_shape)) { 
     60                for (let i = 0; i < this.presentOverlays[0].length; i++) { 
     61                    var clone_shape = this.cloneShape(this.presentOverlays[0][i]); 
     62                    if (this.overlayItemIsShape(clone_shape)) { 
    8263                        addShapeListeners(clone_shape, null); 
    8364                    } else { 
     
    8667                    overlays_copy[i] = clone_shape; 
    8768                } 
    88                 prevOverlays.push(overlays_copy); 
    89             } 
    90         } 
    91         presentOverlays = []; 
    92         presentOverlays.push(undoneOverlays[undoneOverlays.length - 1]); 
    93         undoneOverlays.pop(); 
    94     } 
    95 } 
    96  
    97 function clonePath(path) { 
     69                this.prevOverlays.push(overlays_copy); 
     70            } 
     71        } 
     72        this.presentOverlays = []; 
     73        this.presentOverlays.push(this.undoneOverlays[this.undoneOverlays.length - 1]); 
     74        this.undoneOverlays.pop(); 
     75    } 
     76} 
     77 
     78 
     79 
     80 
     81MapEditorHistory.prototype.clonePath = function(path){ 
    9882    var clone_path = []; 
    9983 
     
    11195} 
    11296 
    113 function clonePolyline(polyline) { 
     97 
     98MapEditorHistory.prototype.clonePolyline = function(polyline){ 
    11499    var path = polyline.getPath(); 
    115100    var geodesic = polyline.geodesic; 
     
    135120} 
    136121 
    137 function clonePolygon(polygon) { 
     122 
     123MapEditorHistory.prototype.clonePolygon = function(polygon){ 
    138124    var path = polygon.getPath(); 
    139125    var geodesic = polygon.geodesic; 
     
    163149} 
    164150 
    165 function cloneRectangle(rect) { 
     151MapEditorHistory.prototype.cloneRectangle = function(rect){ 
    166152    var strokeColor = rect.strokeColor; 
    167153    var strokeOpacity = rect.strokeOpacity; 
     
    190176} 
    191177 
    192 function cloneCircle(circ) { 
     178MapEditorHistory.prototype.cloneCircle = function(circ){ 
    193179    var strokeColor = circ.strokeColor; 
    194180    var strokeOpacity = circ.strokeOpacity; 
     
    217203} 
    218204 
    219 function cloneMarker(marker) { 
     205MapEditorHistory.prototype.cloneMarker = function(marker){ 
    220206    var position = marker.getPosition(); 
    221207    var anchorPoint = marker.anchorPoint; 
     
    232218} 
    233219 
    234 function cloneShape(shape) { 
     220MapEditorHistory.prototype.cloneShape = function(shape){ 
    235221    if (shape.type === google.maps.drawing.OverlayType.POLYLINE) { 
    236         var clone_polyline = clonePolyline(shape); 
     222        let clone_polyline = this.clonePolyline(shape); 
    237223        return clone_polyline; 
    238224    } else if (shape.type === google.maps.drawing.OverlayType.POLYGON) { 
    239         var clone_polygon = clonePolygon(shape); 
     225        let clone_polygon = this.clonePolygon(shape); 
    240226        return clone_polygon; 
    241227    } else if (shape.type === google.maps.drawing.OverlayType.RECTANGLE) { 
    242         var clone_rect = cloneRectangle(shape); 
     228        let clone_rect = this.cloneRectangle(shape); 
    243229        return clone_rect; 
    244230 
    245231    } else if (shape.type === google.maps.drawing.OverlayType.CIRCLE) { 
    246         var clone_circ = cloneCircle(shape); 
     232        let clone_circ = this.cloneCircle(shape); 
    247233        return clone_circ; 
    248234 
    249235    } else { 
    250         var clone_marker = cloneMarker(shape); 
     236        let clone_marker = this.cloneMarker(shape); 
    251237        return clone_marker; 
    252238    } 
    253239} 
    254240 
    255 function overlayItemIsShape(overlay_item) { 
     241MapEditorHistory.prototype.overlayItemIsShape = function(overlay_item){ 
    256242    var type = overlay_item.type; 
    257243 
     
    263249    return is_shape; 
    264250} 
    265  
    266 function historyOverlayPush() { 
    267     if (cycleComplete) { 
     251MapEditorHistory.prototype.historyOverlayPush = function(){ 
     252    if (this.cycleComplete) { 
    268253        var overlays_copy = []; 
    269254        for (var i = 0; i < overlays.length; i++) { 
    270             var clone_shape = cloneShape(overlays[i]); 
    271  
    272             if (overlayItemIsShape(clone_shape)) { 
     255            let clone_shape = this.cloneShape(overlays[i]); 
     256 
     257            if (this.overlayItemIsShape(clone_shape)) { 
    273258                addShapeListeners(clone_shape, null); // don't have an overlay event! 
    274259            } else { 
     
    278263        } 
    279264        undoneOverlays = []; 
    280         prevOverlays.push(overlays_copy); 
     265        this.prevOverlays.push(overlays_copy); 
    281266    } 
    282267 
    283268    cycleComplete = false; 
    284269} 
    285 function presentOverlayPush() { 
    286  
    287     OVbeforeClearing = overlays; 
    288     PObeforeClearing = presentOverlays; 
    289     presentOverlays = []; 
     270 
     271MapEditorHistory.prototype.presentOverlayPush = function(){ 
     272    this.presentOverlays = []; 
    290273    var overlays_copy = []; 
    291274    for (var i = 0; i < overlays.length; i++) { 
    292         var clone_shape = cloneShape(overlays[i]); 
    293         if (overlayItemIsShape(clone_shape)) { 
     275        let clone_shape = this.cloneShape(overlays[i]); 
     276        if (this.overlayItemIsShape(clone_shape)) { 
    294277            addShapeListeners(clone_shape, null); // don't have an overlay event! 
    295278        } else { 
    296             addMarkerListeners(clone_shape, null); // don't have an overlay event! 
     279            this.addMarkerListeners(clone_shape, null); // don't have an overlay event! 
    297280        } 
    298281        overlays_copy[i] = clone_shape; 
    299282    } 
    300     presentOverlays.push(overlays_copy); 
     283    this.presentOverlays.push(overlays_copy); 
    301284    cycleComplete = true; 
    302285 
    303286} 
    304287 
    305 function undoneOverlaysPush() { 
    306  
     288MapEditorHistory.prototype.undoneOverlaysPush = function(){ 
     289presentOverlayPush 
    307290    var conditionUndone = presentOverlays[presentOverlays.length - 1] !== undefined; 
    308291 
    309292    if (conditionUndone) { 
    310         var overlays_copy = []; 
     293        let overlays_copy = []; 
    311294        for (var i = 0; i < presentOverlays[0].length; i++) { 
    312295            var clone_shape = cloneShape(presentOverlays[0][i]); 
     
    327310        var overlays_copy = []; 
    328311        for (var i = 0; i < prevOverlays[prevOverlays.length - 1].length; i++) { 
    329             var clone_shape = cloneShape(prevOverlays[prevOverlays.length - 1][i]); 
     312            let clone_shape = cloneShape(prevOverlays[prevOverlays.length - 1][i]); 
    330313            if (overlayItemIsShape(clone_shape)) { 
    331314                addShapeListeners(clone_shape, null); // don't have an overlay event! 
     
    342325MapEditorHistory.prototype.undoneOverlaysPush = function() { 
    343326 
    344     var conditionUndone = this.presentOverlays[this.presentOverlays.length - 1] !== undefined; 
     327    let conditionUndone = this.presentOverlays[this.presentOverlays.length - 1] !== undefined; 
    345328 
    346329    if (conditionUndone) { 
    347330        var overlays_copy = []; 
    348331        for (var i = 0; i < this.presentOverlays[0].length; i++) { 
    349             var clone_shape = cloneShape(this.presentOverlays[0][i]); 
    350             if (overlayItemIsShape(clone_shape)) { 
     332            let clone_shape = this.cloneShape(this.presentOverlays[0][i]); 
     333            if (this.overlayItemIsShape(clone_shape)) { 
    351334                addShapeListeners(clone_shape, null); // don't have an overlay event! 
    352335            } else { 
     
    358341    } 
    359342 
    360     var conditionPresent = this.prevOverlays[this.prevOverlays.length - 1] !== undefined; 
     343    let conditionPresent = this.prevOverlays[this.prevOverlays.length - 1] !== undefined; 
    361344 
    362345    if (conditionPresent) { 
    363346        this.presentOverlays = []; 
    364347        var overlays_copy = []; 
    365         for (var i = 0; i < this.prevOverlays[this.prevOverlays.length - 1].length; i++) { 
    366             var clone_shape = cloneShape(this.prevOverlays[this.prevOverlays.length - 1][i]); 
    367             if (overlayItemIsShape(clone_shape)) { 
     348        for (let i = 0; i < this.prevOverlays[this.prevOverlays.length - 1].length; i++) { 
     349            var clone_shape = this.cloneShape(this.prevOverlays[this.prevOverlays.length - 1][i]); 
     350            if (this.overlayItemIsShape(clone_shape)) { 
    368351                addShapeListeners(clone_shape, null); // don't have an overlay event! 
    369352            } else { 
  • main/trunk/greenstone3/web/interfaces/default/js/map-scripts-editor.js

    r32766 r32769  
    176176            if (resizable) { 
    177177                if(dontResize == false){ 
    178                     historyOverlayPush(); 
     178                    mapEditorHistory.historyOverlayPush(); 
    179179                } 
    180180                 
     
    187187 
    188188    if (selectedShapes.length > 0) { 
    189         historyOverlayPush(); 
     189        mapEditorHistory.historyOverlayPush(); 
    190190    } 
    191191} 
     
    193193function shapeSpecsChangeMU() { 
    194194    if (selectedShapes.length > 0) { 
    195         presentOverlayPush(); 
     195        mapEditorHistory.presentOverlayPush (); 
    196196    } 
    197197} 
     
    309309    google.maps.event.addListener(drawingManager, 'overlaycomplete', function (e) { 
    310310        allowDeselect = true; 
    311         historyOverlayPush(); 
     311        mapEditorHistory.historyOverlayPush(); 
    312312        overlays.push(e.overlay); // store reference to added overlay 
    313313        var newShape = e.overlay; 
    314314        newShape.type = e.type; 
    315         presentOverlayPush(); 
     315        mapEditorHistory.presentOverlayPush(); 
    316316 
    317317        if (e.type !== google.maps.drawing.OverlayType.MARKER) { 
     
    348348    document.addEventListener('keydown', function () { 
    349349        if (event.code == 'KeyY' && (event.ctrlKey || event.metaKey) || (event.code == 'KeyZ' && event.code == 'ShiftLeft' && (event.ctrlKey || event.metaKey))) { 
    350             redo(); 
     350            mapEditorHistory.redo(); 
    351351        } 
    352352        if (event.code == 'KeyZ' && (event.ctrlKey || event.metaKey)) { 
    353353            if (shiftKeyPressed == false) { 
    354                 undo(); 
    355                 mapEditorHistory.undo(overlays); // ***** when we only have MapEditorHistory as a class 
     354                //undo(); 
     355                mapEditorHistory.undo(); 
     356                //mapEditorHistory.undo(overlays, draggableState); // ***** when we only have MapEditorHistory as a class 
    356357                //that.mapEditorHistory.undo(that.overlays); // ***** when we have both MapEditorHistory and MapEditor as classes 
    357358                 
     
    436437    google.maps.event.addListener(newShape, 'dragstart', function (e) { 
    437438        beingDragged = true; 
    438         historyOverlayPush(); 
     439        mapEditorHistory.historyOverlayPush(); 
    439440 
    440441    }); 
     
    442443    google.maps.event.addListener(newShape, 'dragend', function () { 
    443444        beingDragged = false; 
    444         presentOverlayPush(); 
     445        mapEditorHistory.presentOverlayPush (); 
    445446        allowDeselect = false; 
    446447    }); 
     
    458459    google.maps.event.addListener(newShape, 'dragstart', function (e) { 
    459460        allowDeselect = false; 
    460         historyOverlayPush(); 
     461        mapEditorHistory.historyOverlayPush(); 
    461462    }); 
    462463 
     
    465466    google.maps.event.addListener(newShape, 'dragend', function () { 
    466467        beingDragged = false; 
    467         presentOverlayPush(); 
     468        mapEditorHistory.presentOverlayPush (); 
    468469        settingThePath(); 
    469470         
     
    475476    google.maps.event.addListener(newShape, 'bounds_changed', function (e) { 
    476477        if (beingDragged == false) { 
    477             presentOverlayPush(); 
     478            mapEditorHistory.presentOverlayPush (); 
    478479        } 
    479480    }); 
     
    485486        } 
    486487        if (e.vertex !== undefined || e.edge !== undefined) { 
    487             historyOverlayPush() 
     488            mapEditorHistory.historyOverlayPush() 
    488489        } 
    489490        if (drawingManager.drawingMode == null) { 
     
    494495    google.maps.event.addListener(newShape, 'mouseup', function (e) { 
    495496        if (e.vertex !== undefined || e.edge !== undefined) { 
    496             presentOverlayPush() 
     497            mapEditorHistory.presentOverlayPush () 
    497498        } else { 
    498499            //setSelection(newShape, e); 
     
    572573function setSelectedThickness(sWeight) { 
    573574    if (selectedShapes.length > 0) { 
    574         //historyOverlayPush(); 
     575        //mapEditorHistory.historyOverlayPush(); 
    575576        for (var i = 0; i < selectedShapes.length; i++) { 
    576577            selectedShapes[i].set('strokeWeight', sWeight); 
     
    636637 
    637638function printHistory() { 
    638     console.log("prev", prevOverlays); 
    639     console.log("present ", presentOverlays); 
    640     console.log("undone ", undoneOverlays); 
     639    console.log("prev", mapEditorHistory.prevOverlays); 
     640    console.log("present ", mapEditorHistory.presentOverlays); 
     641    console.log("undone ", mapEditorHistory.undoneOverlays); 
    641642    console.log(mouseState); 
    642643} 
     
    668669function deleteSelectedShape() { 
    669670    //console.log("this fires"); 
    670     historyOverlayPush(); 
     671    mapEditorHistory.historyOverlayPush(); 
    671672    for (var i = 0; i < selectedShapes.length; i++) { 
    672673        selectedShapes[i].setMap(null); 
     
    680681    selectedShapes = []; 
    681682    //console.log("this fires"); 
    682     presentOverlayPush(); 
     683    mapEditorHistory.presentOverlayPush (); 
    683684} 
    684685 
     
    686687    console.log(nId); 
    687688    //console.log("this fires"); 
    688     historyOverlayPush(); 
     689    mapEditorHistory.historyOverlayPush(); 
    689690    for (var i = 0; i < overlays.length; i++) { 
    690691        overlays[i].setMap(null); 
     
    692693    overlays = []; 
    693694    //console.log("this fires"); 
    694     presentOverlayPush(); 
    695 } 
     695    mapEditorHistory.presentOverlayPush (); 
     696}