text: /*!
   * Copyright 2014 Apereo Foundation (AF) Licensed under the
   * Educational Community 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://opensource.org/licenses/ECL-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.
   */
  
  define(['jquery', 'oae.core'], function($, oae) {
  
      /*!
       * This widget will be initiated with a widgetData object that contains the following properties:
       *
       * @param  {Number}     count           The number of items that have been prepared to add into the system
       * @param  {String}     type            The type of item that will be added to the system (one of `file`, `link` or `collabdoc`)
       * @param  {String}     visibility      The default visibility for the items that are being added to the system (one of `public`, `loggedin` or `private`)
       */
      return function(uid, showSettings, widgetData) {
  
          // The widget container
          var $rootel = $('#' + uid);
  
          // Variable that keeps track of the current page context, as this will be used as a default
          // user/group that the item will be shared with
          var pageContext = null;
  
          /**
           * Translates and returns the i18n key associated to personal library that will be the target of the item being added
           *
           * @param  {String}   type    The type of the items handled by the widget ('file', 'link', 'collabdoc' or 'discussion')
           * @return {String}           Returns the translated string associated to personal library the item will be added to (e.g. `My Library`, `My Discussions`)
           */
          var getLibraryi18n = function(type) {
              if (type === 'discussion') {
                  return oae.api.i18n.translate('__MSG__MY_DISCUSSIONS__');
              } else {
                  return oae.api.i18n.translate('__MSG__MY_LIBRARY__');
              }
          };
  
          /**
           * Fires an event to let the widget caller know that the permission change has been aborted.
           */
          var cancelPermissionsChange = function() {
              $(document).trigger('oae.setpermissions.cancel.' + uid);
          };
  
          /**
           * Triggers an event when changes have been made to the visibility settings.
           */
          var savePermissionsChange = function() {
              // Get the list of users/groups the item should be shared with
              var viewers = oae.api.util.autoSuggest().getSelection($rootel);
  
              // Determine whether or not the widget is called from a context that is different than
              // the user's personal space. If so, we check if that context is part of the list of
              // users and groups to share the content with as this would be handled in a special way
              // in the summary message.
              var context = null;
              if (oae.data.me.id !== pageContext.id) {
                  var addedToGroupContext = _.some(viewers, function(viewer) {
                      return viewer.id === pageContext.id;
                  });
                  if (addedToGroupContext) {
                      context = pageContext.displayName;
                  }
              }
  
              // Filter the current user and the current page context out of the list of people to
              // share with, as they will be shown slightly different in the summary message
              var shared = [];
              $.each(viewers, function(index, viewer) {
                  if (viewer.id !== oae.data.me.id && viewer.id !== pageContext.id) {
                      shared.push(viewer);
                  }
              });
  
              // Get the selected visibility setting
              var visibility = $('#setpermissions-container input[type="radio"]:checked', $rootel).val();
  
              // Inform the widget caller that the permissions have changed
              $(document).trigger('oae.setpermissions.changed.' + uid, {
                  'visibility': visibility,
                  'viewers': viewers,
                  'summary': oae.api.util.template().render('#setpermissions-summary-template', {
                      'context': context,
                      'count': widgetData.count,
                      'mylibrary': getLibraryi18n(widgetData.type),
                      'resourceType': widgetData.type,
                      'shared': shared,
                      'visibility': visibility
                  })
              });
          };
  
          /**
           * Initializes the autosuggest used for sharing with other users or groups.
           *
           * @param  {Function}       callback            Standard callback function
           */
          var setUpAutoSuggest = function(callback) {
              var preFillData = [{
                  'displayName': getLibraryi18n(widgetData.type),
                  'id': oae.data.me.id,
                  'fixed': true
              }];
  
              // If we're not on the user's personal space, we also add the current page context
              // to the list of people/groups to share with
              if (pageContext && pageContext.id !== oae.data.me.id) {
                  preFillData.push($.extend({'fixed': true}, pageContext));
              }
  
              // Initialize the autoSuggest field
              oae.api.util.autoSuggest().setup($('#setpermissions-share', $rootel), {
                  'preFill': preFillData,
                  'url': '/api/search/interact'
              }, null, callback);
          };
  
          /**
           * Initializes the setpermissions widget.
           */
          var setUpSetPermissionsModal = function() {
              // Render the setpermissions template
              oae.api.util.template().render('#setpermissions-template', widgetData, '#setpermissions-container');
  
              $('#setpermissions-savepermissions', $rootel).on('click', savePermissionsChange);
              $('#setpermissions-cancelpermissions', $rootel).on('click', cancelPermissionsChange);
  
              // Trigger and bind an event that will ask for the page context
              $(document).on('oae.context.send.setpermissions', function(ev, data) {
                  pageContext = data;
                  // Immediately pass the default permissions summary back to the widget caller
                  setUpAutoSuggest(savePermissionsChange);
              });
              $(document).trigger('oae.context.get', 'setpermissions');
          };
  
          setUpSetPermissionsModal();
  
      };
  });
  