<commit_before>/*! 
 * TimeMap Copyright 2008 <NAME>.
 * Licensed under the MIT License (see LICENSE.txt)
 */

/**---------------------------------------------------------------------------
 * TimeMap Editing Tools
 *
 * @author <NAME> (www.nickrabinowitz.com)
 * Functions in this file offer tools for editing a timemap dataset in
 * a browser-based GUI. Call tm.enterEditMode() and tm.closeEditMode() 
 * to turn the tools on and off; set configuration options in 
 * tm.opts.editOpts (set as {options{editOpts{...}} in timemapInit()).
 * 
 * 
 * Depends on:
 * json2: lib/json2.pack.js
 * timemapexport.js
 * jQuery: jquery.com
 * jqModal: http://dev.iceburg.net/jquery/jqModal/
 * jeditable: http://www.appelsiini.net/projects/jeditable
 * Sorry for all the bloat, but jQuery makes UI development much easier.
 *---------------------------------------------------------------------------*/
 
 /**
  * Add editing tools to the timemap.
  *
  * @param (String) editPaneId      ID of DOM element to be the edit pane.
  * @param (Object) options         Options for edit mode - may also be set in tm.opts.editMode
  *     (String) saveTarget             URL for ajax save submissions (XXX:unsupported)
  *     (String) saveMode               Either "explicit" (default - you push a button) or "implicit"
  *                                     (save with every change) (XXX:unsupported)
  */
TimeMap.prototype.enterEditMode = function(editPaneId, options) {
    // set default options if none have been specified
    if (!this.opts.editOpts) {
        var opts = options || {};
        opts.saveTarget = opts.saveTarget || "";
        opts.saveMode =   opts.saveMode || "explicit";
        this.opts.editOpts = opts;
    }
    // create edit pane
    if (!this.editPane) {
        // default id
        if (!editPaneId) editPaneId = 'editpane';
        // look for existing div
        editPane = $('#' + editPaneId).get(0);
        var tmo = this;
        if (!editPane) {
            // make it from string
            editPane = $('<div id="' + editPaneId + '" class="jqmWindow"></div>').get(0);
            $(editPane).append(
                $('<div class="edittitle jqDrag" />').append(
                    $('<div id="editclose" />').click(function() {
                        tmo.closeEditMode();
                    })
                ).append($('<h3>Edit Datasets</h3>'))
            ).append($('<div id="editresize" class="jqResize" />'));
            $('body').append(editPane);
        }
        // make a holder for datasets
        var dsPane = $('<div id="dspane" />').get(0);
        $(editPane).append(dsPane);
        /* XXX: make a save button
        var saveButton = $('<div id="editsave"/>').append(
            $('<input type="button" value="Save Changes" />').click(function() {
              tmo.saveChanges()
            })
        );
        $(editPane).append(saveButton);
        */
        // save for later
        this.editPane = editPane;
        this.dsPane = dsPane;
        // make the edit pane a modal window
        $(this.editPane)
            .jqDrag('.jqDrag')
            .jqResize('.jqResize')
            .jqm({ overlay: 0 });
    }
    $(this.editPane).jqmShow();
    this.updateEditDatasets();
    // turn on placemark editing
    for (id in this.datasets) {
        var items = this.datasets[id].getItems();
        for (var x=0; x < items.length; x++) {
            items[x].enablePlacemarkEdits();
        }
    }
}

/**
 * Remove editing tools from the timemap.
 */
TimeMap.prototype.closeEditMode = function() {
    // close edit pane
    $(this.editPane).jqmHide();
    // turn off placemark editing
    for (id in this.datasets) {
        var items = this.datasets[id].getItems();
        for (var x=0; x < items.length; x++) {
            items[x].disablePlacemarkEdits();
        }
    }
}

/**
 * Save all the datasets the specified target
 */
TimeMap.prototype.saveChanges = function() {
    if (this.opts.editOpts.saveTarget) {
        // send the datasets to the server, serialized
        // XXX: Save options as well
        // XXX: Add a hook and default for a callback function
        $.post(this.opts.editOpts.saveTarget, 
               {'datasets':JSON.stringify( this.datasets)});
    }
}

/**
 * Update the entire dataset listing
 */
TimeMap.prototype.updateEditDatasets = function() {
    // clear
    $(this.dsPane).empty();
    // add all datasets
    for (id in this.datasets) {
        this.addEditDataset(this.datasets[id], this.dsPane);
    }
}

/**
 * Add a timemap dataset to the edit pane.
 * 
 * @param (Object) ds       Dataset to add
 * @param (DOM Element) el  Element to add the item to
 */
TimeMap.prototype.addEditDataset = function(ds, el) {
    // make the dataset div
    var dsdiv = $('<div class="dataset" />').get(0);
    // save reference
    ds.editpane = dsdiv;
    ds.updateEditPane();
    $(el).append(dsdiv);
}

/**
 * Update the edit pane version of a timemap dataset.
 */
TimeMapDataset.prototype.updateEditPane = function() {
    if (!this.editpane) return;
    else $(this.editpane).empty();
    var ds = this;
    // add new item button
    $(this.editpane).append(
        $('<div class="dsadditem">add new item</div>')
            .click(function() {
                var item = ds.loadItem({title:"Untitled Item"});
                ds.addEditItem(item, ds.editpane);
            })
    );
    // add title
    $(this.editpane).append(
        $('<div class="dstitle">' + this.getTitle() + '</div>')
            .editable(function(value, settings) { 
                ds.opts.title = value;
                return(value);
            })
    );
    // items
    var items = this.getItems();
    for (var x=0; x < items.length; x++) {
        this.addEditItem(items[x], this.editpane);<commit_msg>disentangling dataset edit/save functions from timemap functions<commit_after>/*! 
 * TimeMap Copyright 2008 <NAME>.
 * Licensed under the MIT License (see LICENSE.txt)
 */

/**---------------------------------------------------------------------------
 * TimeMap Editing Tools
 *
 * @author <NAME> (www.nickrabinowitz.com)
 * Functions in this file offer tools for editing a timemap dataset in
 * a browser-based GUI. Call tm.enterEditMode() and tm.closeEditMode() 
 * to turn the tools on and off; set configuration options in 
 * tm.opts.editOpts (set as {options{editOpts{...}} in timemapInit()).
 * 
 * 
 * Depends on:
 * json2: lib/json2.pack.js
 * timemapexport.js
 * jQuery: jquery.com
 * jqModal: http://dev.iceburg.net/jquery/jqModal/
 * jeditable: http://www.appelsiini.net/projects/jeditable
 * Sorry for all the bloat, but jQuery makes UI development much easier.
 *---------------------------------------------------------------------------*/
 
 /**
  * Add editing tools to the timemap.
  *
  * @param (String) editPaneId      ID of DOM element to be the edit pane.
  * @param (Object) options         Options for edit mode - may also be set in tm.opts.editMode
  *     (String) saveTarget             URL for ajax save submissions (XXX:unsupported)
  *     (String) saveMode               Either "explicit" (default - you push a button) or "implicit"
  *                                     (save with every change) (XXX:unsupported)
  */
TimeMap.prototype.enterEditMode = function(editPaneId, options) {
    // set default options if none have been specified
    if (!this.opts.editOpts) {
        var opts = options || {};
        opts.saveTarget = opts.saveTarget || "";
        opts.saveMode =   opts.saveMode || "explicit";
        this.opts.editOpts = opts;
    }
    // create edit pane
    if (!this.editPane) {
        // default id
        if (!editPaneId) editPaneId = 'editpane';
        // look for existing div
        editPane = $('#' + editPaneId).get(0);
        var tmo = this;
        if (!editPane) {
            // make it from string
            editPane = $('<div id="' + editPaneId + '" class="jqmWindow"></div>').get(0);
            $(editPane).append(
                $('<div class="edittitle jqDrag" />').append(
                    $('<div id="editclose" />').click(function() {
                        tmo.closeEditMode();
                    })
                ).append($('<h3>Edit Datasets</h3>'))
            ).append($('<div id="editresize" class="jqResize" />'));
            $('body').append(editPane);
        }
        // make a holder for datasets
        var dsPane = $('<div id="dspane" />').get(0);
        $(editPane).append(dsPane);
        /* XXX: make a save button
        var saveButton = $('<div id="editsave"/>').append(
            $('<input type="button" value="Save Changes" />').click(function() {
              tmo.saveChanges()
            })
        );
        $(editPane).append(saveButton);
        */
        // save for later
        this.editPane = editPane;
        this.dsPane = dsPane;
        // make the edit pane a modal window
        $(this.editPane)
            .jqDrag('.jqDrag')
            .jqResize('.jqResize')
            .jqm({ overlay: 0 });
    }
    $(this.editPane).jqmShow();
    this.updateEditDatasets();
    // turn on placemark editing
    for (id in this.datasets) {
        this.datasets[id].enterEditMode();
    }
}

/**
 * Remove editing tools from the timemap.
 */
TimeMap.prototype.closeEditMode = function() {
    // close edit pane
    $(this.editPane).jqmHide();
    // turn off placemark editing
    for (id in this.datasets) {
        this.datasets[id].closeEditMode();
    }
}

/**
 * Save all the datasets to the specified target
 *
 * @param (Function) f    Optional callback function
 */
TimeMap.prototype.saveAllChanges = function(f) {
    var target = this.opts.editOpts.saveTarget;
    f = f || function(d){};
    if (target) {
        // XXX: Save options as well
        var data = {'datasets':JSON.stringify( this.datasets)}
        // send the datasets to the server, serialized
        $.post(target, data, f);
    }
}

/**
 * Update the entire dataset listing
 */
TimeMap.prototype.updateEditDatasets = function() {
    // clear
    $(this.dsPane).empty();
    // add all datasets
    for (id in this.datasets) {
        ds.enterEditMode();
        $(this.dsPane).append(ds.editpane);
    }
}

/**
 * Turn on editing for a single dataset.
 */
TimeMapDataset.prototype.enterEditMode = function() {
    if (!this.editpane) {
        // make the dataset div
        var dsdiv = $('<div class="dataset" />').get(0);
        // save reference
        this.editpane = dsdiv;
    }
    this.updateEditPane();
    var items = this.getItems();
    for (var x=0; x < items.length; x++) {
        items[x].enablePlacemarkEdits();
    }
}

/**
 * Turn off editing for a single dataset.
 */
TimeMapDataset.prototype.closeEditMode = function() {
    $(this.editpane).empty();
    var items = this.getItems();
    for (var x=0; x < items.length; x++) {
        items[x].disablePlacemarkEdits();
    }
}

/**
 * Save the dataset to the specified target
 *
 * @param (Function) f    Optional callback function
 */
TimeMapDataset.prototype.saveAllChanges = function(f) {
    try {
        var target = this.timemap.opts.editOpts.saveTarget;
    } catch(e) {
      return;
    }
    f = f || function(d){};
    if (target) {
        var data = {'datasets':JSON.stringify({'ds':this})}
        // send the datasets to the server, serialized
        $.post(target, data, f);
    }
}

/**
 * Update the edit pane version of a timemap dataset.
 */
TimeMapDataset.prototype.updateEditPane = function() {
    if (!this.editpane) return;
    else $(this.editpane).empty();
    var ds = this;
    // add new item button
    $(this.editpane).append(
        $('<div class="dsadditem">add new item</div>')
            .click(function() {
                var item = ds.loadItem({title:"Untitled Item"});
                ds.addEditItem(item, ds.editpane);
            })
    );
    // add title
    $(this.editpane).append(
        $('<div class="dstitle">' + this.getTitle() + '</div>')
            .editable(function(value, settings) { 
                ds.opts.title = value;
                return(value);
            })
    );
    // items
    var items = this.getItems();
    for (var x=0; x < items.length; x++) {
        this.addEditItem(items[x], this.editpane);
{'fraction_non_alphanumeric': 0.16746840603342847, 'fraction_numerical': 0.0026090501426824296, 'mean_word_length': 1.9757399320718099, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7418402', 'n_tokens_mistral': 3677, 'n_tokens_neox': 3627, 'n_words': 952}