<commit_before>////////////////////////////////////////////////////////////////////////////////
// Node Controller
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2015 Concept
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
////////////////////////////////////////////////////////////////////////////////

"use strict";

GV.nodeCtrl = {

  /**
   * NodeLevel: deleteNode
   * parameter: _id (node collection id)
   * description: recursively deletes all children
   * from a node in a post-order manner
   */
  deleteNode: function(prevNode, template) {

    if (typeof prevNode === "string")
      prevNode = GV.collections.Nodes.findOne({
        _id: prevNode
      });

    var nodeId = prevNode._id;
    var fileId = prevNode.fileId;

    var nodes = GV.collections.Nodes.find({
      parent: nodeId
    }).fetch();

    nodes.forEach(function(node) {
      GV.nodeCtrl.deleteNode(node);
    });

    // Remove the tab
    GV.tabs.removeTab(nodeId);

    // Remove the node
    GV.collections.Nodes.remove({
      _id: nodeId
    }, function(error) {

      if (fileId) {
        GV.collections.Files.remove({
          _id: fileId
        }, function(error) {

          // Remove the reference from the document
          GV.collections.Documents.update({
            _id: Session.get('mainDocument')
          }, {
            $pull: {
              children: nodeId,
              fileIds: fileId
            }
          }, function(error) {
            if (error) {
              Notifications.warn('Feil', error.message);
            }

            // update positions
            if (template)
              GV.nodeCtrl.updatePositions(template);
          });
        });
      } else {
        // Remove the reference from the document
        GV.collections.Documents.update({
          _id: Session.get('mainDocument')
        }, {
          $pull: {
            children: nodeId
          }
        }, function(error) {
          if (error) {
            Notifications.warn('Feil', error.message);
          }

          // update positions
          if (template)
            GV.nodeCtrl.updatePositions(template);
        });
      }
    });
  },

  getSection: function(prevSection, prevNodePos) {
    if (prevSection)
      return prevSection + "." + (prevNodePos + 1);
    else
      return prevNodePos + 1;
  },

  /**
   * Generates a section label for the next child
   */
  generateSectionLabel: function(parentLabel, position) {
    parentLabel = parentLabel ? parentLabel + "." : "";

    if (position >= 0)
      return parentLabel + (position + 1);
    else
      return null;
  },

  /**
   * Opens a node given data
   */
  openNode: function(elData) {
    if (elData && elData._id) {
      GV.tabs.setDummyTab(elData._id);
      Session.set('nodeInFocus', elData._id);
      Router.current().subscribe('fileByNode', elData._id);
      Session.set("file", null);
      Session.set("uploadStopped", false);

      if (elData.nodeType === "chapter") {

        if (elData.title || elData.description) {
          Session.set('showNodeForm', false);
        } else {
          Session.set('showNodeForm', true);

          Meteor.defer(function() {
            $("input[name='title']").tooltip({
              'placement': 'top',
              'title':  'Navn på kapittel mangler og trengs for å få god ' +
                        'oversikt i kapittelstrukturen.'
            }).tooltip('show');
          });
        }

        Session.set('showMediaNodesView', true);
      } else {
        Session.set('showNodeForm', true);

        if (!elData.description) {
          Meteor.defer(function() {
            $("textarea[name='description']").tooltip({
              'placement': 'bottom',
              'title': 'Informasjonselement mangler'
            }).tooltip('show');
          });
        }

        if (!elData.title) {
          Meteor.defer(function() {
            $("input[name='title']").tooltip({
              'placement': 'top',
              'title':  'Navn på informasjonselement mangler og bør fylles ut ' +
                        'for å kunne få fullt utbytte av nøkkelord- og kildevisningen.'
            }).tooltip('show');
          });
        }
      }

      // Reset the form because of no update on tags field on data change.
      AutoForm.resetForm("update-node-form");
      Meteor.defer(function() {<commit_msg>Only tooltips in node update form<commit_after>////////////////////////////////////////////////////////////////////////////////
// Node Controller
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2015 Concept
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
////////////////////////////////////////////////////////////////////////////////

"use strict";

GV.nodeCtrl = {

  /**
   * NodeLevel: deleteNode
   * parameter: _id (node collection id)
   * description: recursively deletes all children
   * from a node in a post-order manner
   */
  deleteNode: function(prevNode, template) {

    if (typeof prevNode === "string")
      prevNode = GV.collections.Nodes.findOne({
        _id: prevNode
      });

    var nodeId = prevNode._id;
    var fileId = prevNode.fileId;

    var nodes = GV.collections.Nodes.find({
      parent: nodeId
    }).fetch();

    nodes.forEach(function(node) {
      GV.nodeCtrl.deleteNode(node);
    });

    // Remove the tab
    GV.tabs.removeTab(nodeId);

    // Remove the node
    GV.collections.Nodes.remove({
      _id: nodeId
    }, function(error) {

      if (fileId) {
        GV.collections.Files.remove({
          _id: fileId
        }, function(error) {

          // Remove the reference from the document
          GV.collections.Documents.update({
            _id: Session.get('mainDocument')
          }, {
            $pull: {
              children: nodeId,
              fileIds: fileId
            }
          }, function(error) {
            if (error) {
              Notifications.warn('Feil', error.message);
            }

            // update positions
            if (template)
              GV.nodeCtrl.updatePositions(template);
          });
        });
      } else {
        // Remove the reference from the document
        GV.collections.Documents.update({
          _id: Session.get('mainDocument')
        }, {
          $pull: {
            children: nodeId
          }
        }, function(error) {
          if (error) {
            Notifications.warn('Feil', error.message);
          }

          // update positions
          if (template)
            GV.nodeCtrl.updatePositions(template);
        });
      }
    });
  },

  getSection: function(prevSection, prevNodePos) {
    if (prevSection)
      return prevSection + "." + (prevNodePos + 1);
    else
      return prevNodePos + 1;
  },

  /**
   * Generates a section label for the next child
   */
  generateSectionLabel: function(parentLabel, position) {
    parentLabel = parentLabel ? parentLabel + "." : "";

    if (position >= 0)
      return parentLabel + (position + 1);
    else
      return null;
  },

  /**
   * Opens a node given data
   */
  openNode: function(elData) {
    if (elData && elData._id) {
      GV.tabs.setDummyTab(elData._id);
      Session.set('nodeInFocus', elData._id);
      Router.current().subscribe('fileByNode', elData._id);
      Session.set("file", null);
      Session.set("uploadStopped", false);

      if (elData.nodeType === "chapter") {

        if (elData.title || elData.description) {
          Session.set('showNodeForm', false);
        } else {
          Session.set('showNodeForm', true);

          Meteor.defer(function() {
            $("#update-node-form input[name='title']").tooltip({
              'placement': 'top',
              'title':  'Navn på kapittel mangler og trengs for å få god ' +
                        'oversikt i kapittelstrukturen.'
            }).tooltip('show');
          });
        }

        Session.set('showMediaNodesView', true);
      } else {
        Session.set('showNodeForm', true);

        if (!elData.description) {
          Meteor.defer(function() {
            $("#update-node-form textarea[name='description']").tooltip({
              'placement': 'bottom',
              'title': 'Informasjonselement mangler'
            }).tooltip('show');
          });
        }

        if (!elData.title) {
          Meteor.defer(function() {
            $("#update-node-form input[name='title']").tooltip({
              'placement': 'top',
              'title':  'Navn på informasjonselement mangler og bør fylles ut ' +
                        'for å kunne få fullt utbytte av nøkkelord- og kildevisningen.'
            }).tooltip('show');
          });
        }
      }

      // Reset the form because of no update on tags field on data change.
      AutoForm.resetForm("update-node-form");
      Meteor.defer(function() {
{'fraction_non_alphanumeric': 0.1687462266049507, 'fraction_numerical': 0.002414972831555645, 'mean_word_length': 1.6638970785312248, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1440097', 'n_tokens_mistral': 2728, 'n_tokens_neox': 2611, 'n_words': 682}