nnnjjj123
2020-11-17 1b2c1edb61190eeb19f465ff031eaa3b2a1b8dbc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
///////////////////////////////////////////////////////////////////////////
// Copyright © 2014 - 2018 Esri. All Rights Reserved.
//
// 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.
///////////////////////////////////////////////////////////////////////////
 
define(['dojo/_base/declare',
    'dijit/_WidgetBase',
    'dijit/_TemplatedMixin',
    'dijit/_WidgetsInTemplateMixin',
    'dojo/_base/lang',
    'esri/lang',
    "dojo/_base/array",
    "dojo/dom-style",
    'dojo/on',
    "dojo/dom",
    "dojo/query",
    "dojo/topic",
    'dojo/text!./templates/ShareOptions.html',
    'jimu/shareUtils',
    "dijit/form/Textarea",
    "dijit/form/CheckBox",
    "dijit/layout/BorderContainer"
  ],
  function(declare, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, lang, esriLang, array, domStyle,
           on, dom, query, topic, template, shareUtils) {
    /*global dijit*/
 
    var so = declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], {
      templateString: template,
      declaredClass: "jimu.dijit.ShareOptions",
 
      postMixInProperties: function() {
        this.inherited(arguments);
        this.nls = window.jimuNls.shareOptions;
        //TODO this._checkboxImgUrl this.i18n.openData;
      },
 
      constructor: function(portalUrl) {
        this.portalUrl = portalUrl;
      },
      startup: function() {
        shareUtils.getShareInfo(this.portalUrl).then(lang.hitch(this, function(result) {
          this._initShow(result);
          this.own(on(dom.byId("share-everyone-check"), "change", lang.hitch(this, this.clickEveryone)));
        }));
      },
 
      _initShow: function(shareInfo) {
        this.shareInfo = shareInfo;
        this.shareInfo.items = [this.shareInfo.item];
        this.shareInfo.sortField = "title";
        // same logic is in ShareMapDlg.js showShareOptions()
        // this.isAdmin is true if the user is an admin user and the item is owned by somebody else in his org
        // this.isAdmin is false if the user is an admin user and owns this item, of if the user is not an admin
        if ((this.shareInfo.userRole.isAdmin() ||
          ((this.shareInfo._roleCanUpdateItems && this.shareInfo._roleCanShare) ||
          this.shareInfo._roleCanShareOthersItems)) && this.shareInfo.itemUser) {
          // only use showAdmin if admin is not viewing their own content, does not require update privileges
          this.showAdmin(this.shareInfo.user.groups, this.shareInfo.itemUser, this.shareInfo.adminGroups);
        } else if (this.shareInfo.user && shareUtils._isUserOwnTheApp(this.shareInfo.user)) {
          // owner of item
          this.show(this.shareInfo.user.groups);
        } else if (this.shareInfo.user &&
          (this.shareInfo.item.access === "public" || this.shareInfo.item.access === "shared" ||
          (this.shareInfo.item.access === "org" && this.shareInfo.currentUser.orgId))) {
          // could be admin of an org that does not own the item
          // this.isAdmin is only true if the user is admin of the org that owns the item
          this.showGroups(this.shareInfo.user.groups);
        } else {
          this.show(this.shareInfo.user.groups);
        }
      },
 
      clear: function() {
        dom.byId("share-groups-list").innerHTML = "";
        dom.byId("share-everyone-check").setAttribute('value', false);
        dom.byId("share-account-check").setAttribute('value', false);
        dom.byId("share-groups-check").setAttribute('value', false);
        //dojo.style(dojo.byId("share-everyone-check_div"), "display", "none");
        //dojo.style(dojo.byId("share-account-check_div"), "display", "none");
        domStyle.set(dom.byId("share-everyone-check_div"), "display", "none");
        domStyle.set(dom.byId("share-account-check_div"), "display", "none");
 
        //dojo.disconnect(this.shareConnect);
        this.shareInfo.shareType = "item";
      },
 
      showAdmin: function(userGroups, itemUser, adminGroups) {
        this.shareInfo.itemUser = itemUser;
        this.shareInfo.userGroups = array.filter(userGroups || [], lang.hitch(this, function() {
          return (!this.shareInfo.item.capabilities || !this.shareInfo.item.capabilities.length);
        }));
        this.shareInfo.userGroups.sort(lang.hitch(this, this._sortFunc));
        this.shareInfo.adminGroups = this.filterAdminGroups(adminGroups);
        // continue with the dialog boot up after we've checked for the user being a part of an organization
        this.checkForAccount("admin");
      },
      filterAdminGroups: function(adminGroups) {
        if (adminGroups) {
          // filter out admin user groups that the admin and item owner are both members of
          var groupIds = [];
          for (var i = 0; i < this.shareInfo.user.groups.length; i++) {
            groupIds.push(this.shareInfo.user.groups[i].id);
          }
 
          var tmpGroups = [];
          for (i = 0; i < adminGroups.length; i++) {
            // if this group hasn't already been seen, add it to the list
            if (array.indexOf(groupIds, adminGroups[i].id) === -1 &&
              (!adminGroups[i].capabilities || !adminGroups[i].capabilities.length)) {
              tmpGroups.push(adminGroups[i]);
            }
          }
          return tmpGroups;
        }
        return null;
      },
      showGroups: function(userGroups) {
        this.shareInfo.userGroups = this.filterOwnedGroups(userGroups);
        if (this.shareInfo.userGroups) {
          this.shareInfo.userGroups.sort(lang.hitch(this, this._sortFunc));
        }
        this.clear();
        this.shareInfo.shareType = "group";
        // can't access the detailed item info for a publicly shared item, so just build list
        this.checkForAccount("group");
      },
      filterOwnedGroups: function(groups) {
        if (groups) {
          var tmpGroups = [];
          var group;
          for (var i = 0; i < groups.length; i++) {
            group = groups[i];
            if ((group.userMembership &&
              (group.userMembership.memberType === "owner" || group.userMembership.memberType === "admin")) &&
              (!group.capabilities || !group.capabilities.length)) {
              tmpGroups.push(group);
            }
          }
          return tmpGroups;
        }
        return null;
      },
      show: function(userGroups/*, itemUser*/) {
        //if (itemUser && itemUser !== null) {
        //this.itemUser = itemUser;
        //}
        this.shareInfo.userGroups = this.filterViewOnlyGroups(userGroups);
        //if(esri.isDefined(this.userGroups)) {
        this.shareInfo.userGroups.sort(lang.hitch(this, this._sortFunc));
        //}
 
        // continue with the dialog boot up after we've checked for the user being a part of an organization
        this.checkForAccount("item");
      },
      filterViewOnlyGroups: function(groups) {
        if (groups) {
          var tmpGroups = [];
          var group;
          for (var i = 0; i < groups.length; i++) {
            group = groups[i];
            // only add the group if the user is the owner/admin or the group is not view only
            if ((!group.capabilities || !group.capabilities.length) &&
              ((group.userMembership &&
              (group.userMembership.memberType === "owner" || group.userMembership.memberType === "admin")) ||
              (!esriLang.isDefined(group.isViewOnly) || !group.isViewOnly))) {
              tmpGroups.push(group);
            }
          }
          return tmpGroups;
        }
        return null;
      },
 
      checkForAccount: function(shareType) {
        dom.byId("share-account-check-label").innerHTML = this.shareInfo.organization.name;
        // hide org sharing if privilege denied
        if (this.shareInfo._isCustomRole && !this.shareInfo._roleCanShareToOrg &&
          !this.shareInfo._roleCanShareOthersItemsToOrg) {
          domStyle.set(dom.byId("share-account-check_div"), "display", "none");
        }
        //showAdminContinue
        this.clear();
        this.shareInfo.shareType = shareType;
        //TODO ONE item only
        // for (var i = 0; i < this.items.length; i++) {
        //   var item = this.items[i];
        //   item.sharing = null;
        //   var url = esriGeowConfig.restBaseUrl + 'content/users/' +
        //     (this.itemUser ? (this.itemUser.username ? this.itemUser.username : this.itemUser.email) : this.user.email);
        //   if ((esri.isDefined(item.folderId) && item.folderId !== '/') || (esri.isDefined(item.ownerFolder) && item.ownerFolder !== '/')) {
        //     url += '/' + (item.folderId || item.ownerFolder);
        //   }
        //   url += '/items/' + item.id;
        //   this.util.getJson(url, dojo.hitch(this, getDetailedInfoHandler, item));
        // }
        var oneItem = this.shareInfo.items[0];
        this.shareInfo.lowestAccess = this.findLowestAccessLevel(this.shareInfo.items);
        shareUtils.getItemById(oneItem, this.portalUrl).then(
          (lang.hitch(this, function(result) {
            //TODO get sharingInfo
            oneItem.sharing = result.sharing;
 
            if (this.shareInfo.shareType === "admin") {
              //1 admin user
              shareUtils.getItemByUserAndItemId(oneItem, this.shareInfo.itemUser, this.shareInfo.user,
                this.portalUrl).then(lang.hitch(this, function(result) {
                oneItem.sharing = result.sharing;
                this.buildAdminList();
              }));
            } else if (this.shareInfo.shareType === "group") {
              //3 in same org/
              shareUtils.getItemsGroups(oneItem, this.portalUrl).then(lang.hitch(this, function(result) {
                var groupIds = [];
                if (result.admin) {
                  for (var i = 0; i < result.admin.length; i++) {
                    groupIds.push(result.admin[i].id);
                  }
                }
                oneItem.sharing = {
                  groups: groupIds
                };
                this.buildGroupList();
              }));
            } else if (this.shareInfo.shareType === "item") {
              //2 creater
              shareUtils.getItemByUserAndItemId(oneItem, this.shareInfo.itemUser, this.shareInfo.user,
                this.portalUrl).then(lang.hitch(this, function(result) {
                  oneItem.sharing = result.sharing;
                  this.buildList();
                }));
            }
          })));
      },
 
      buildAdminList: function() {
        var i, j, k;
        if ((this.shareInfo.user.groups && this.shareInfo.user.groups.length > 0) ||
          (this.shareInfo.adminGroups && this.shareInfo.adminGroups.length > 0 && this.shareInfo.lowestAccess > 1)) {
          if (!this.shareInfo._isCustomRole || this.shareInfo._roleCanShareToGroup ||
            this.shareInfo._roleCanShareOthersItemsToGroup) {
            domStyle.set(dom.byId("share-groups-div"), "display", "block");
          } else {
            domStyle.set(dom.byId("share-groups-div"), "display", "none");
          }
        }
        // determine which items are shared to groups
        this.shareInfo.itemsAreSharedToGroups = ",";
        var groupCounter = [];
        var item = null,
          groupId = null,
          cntr = [];
        for (i = 0; i < this.shareInfo.items.length; i++) {
          item = this.shareInfo.items[i];
          //this.itemsInfo["_" + item.id] = {};
          //this.itemsInfo["_" + item.id].itemsIsSharedToGroups = ",";
          //eval("this.itemsInfo._" + item.id + " = new Object();");
          //eval("this.itemsInfo._" + item.id).itemsIsSharedToGroups = ",";
          if (item.sharing) {
            for (k = 0; k < item.sharing.groups.length; k++) {
              groupId = item.sharing.groups[k];
              //this.itemsInfo["_" + item.id].itemsIsSharedToGroups += groupId + ",";
              //eval("this.itemsInfo._" + item.id).itemsIsSharedToGroups += groupId + ",";
              if (this.shareInfo.itemsAreSharedToGroups.indexOf("," + groupId + ",") === -1) {
                this.shareInfo.itemsAreSharedToGroups += groupId + ",";
                cntr = [];
                cntr[0] = groupId;
                cntr[1] = 1;
                groupCounter[groupCounter.length] = cntr;
              } else {
                for (j = 0; j < groupCounter.length; j++) {
                  if (groupCounter[j][0] === groupId) {
                    groupCounter[j][1] = groupCounter[j][1] + 1;
                    break;
                  }
                }
              }
            }
          }
        }
        // item = this.shareInfo.items[0];
        // //this.itemsInfo["_" + item.id] = {};
        // //this.itemsInfo["_" + item.id].itemsIsSharedToGroups = ",";
        // //eval("this.itemsInfo._" + item.id + " = new Object();");
        // //eval("this.itemsInfo._" + item.id).itemsIsSharedToGroups = ",";
        // if (item.sharing) {
        //   for (k = 0; k < item.sharing.groups.length; k++) {
        //     groupId = item.sharing.groups[k];
        //     //this.itemsInfo["_" + item.id].itemsIsSharedToGroups += groupId + ",";
        //     //eval("this.itemsInfo._" + item.id).itemsIsSharedToGroups += groupId + ",";
        //     if (this.shareInfo.itemsAreSharedToGroups.indexOf("," + groupId + ",") === -1) {
        //       this.shareInfo.itemsAreSharedToGroups += groupId + ",";
        //       cntr = [];
        //       cntr[0] = groupId;
        //       cntr[1] = 1;
        //       groupCounter[groupCounter.length] = cntr;
        //     } else {
        //       for (j = 0; j < groupCounter.length; j++) {
        //         if (groupCounter[j][0] === groupId) {
        //           groupCounter[j][1] = groupCounter[j][1] + 1;
        //           break;
        //         }
        //       }
        //     }
        //   }
        // }
        var html = "";
        var hasOne = false;
        var group;
        var checked;
        var count;
        for (i = 0; i < this.shareInfo.user.groups.length; i++) {
          group = this.shareInfo.user.groups[i];
          checked = "";
          count = this.shareInfo.items.length;
          if (this.shareInfo.itemsAreSharedToGroups.indexOf("," + group.id + ",") > -1) {
            checked = "checked";
            hasOne = true;
            for (j = 0; j < groupCounter.length; j++) {
              if (groupCounter[j][0] === group.id) {
                count = groupCounter[j][1];
                break;
              }
            }
          }
          if (count < this.shareInfo.items.length) {
            // not all items are shared to this group
            html += "<div id=\"img_group_" + i + "_div\" style=\"display:block;\">" +
              "<table cellpadding='0' cellspacing='0' class='share-groups-table'><tr><td width='17' valign='bottom'>" +
              "<img id=\"img_group_" + i + "\" src=\"" + this._checkboxImgUrl + "\" border=\"0\">" +
              "</td><td>" + group.title + "</td></tr></table></div>" +
              "<div id=\"group_" + i + "_div\" style=\"display:none;\">" +
              "<table cellpadding='0' cellspacing='0' class='share-groups-table'><tr><td width='17' valign='bottom'>" +
              "<input id=\"group_" + i + "\" " + checked + " value=\"" + group.id + "\" type=\"checkbox\" " +
              "class='share-groups-input-checkbox' dojoType=\"dijit.form.Checkbox\" />" +
              "</td><td><label for='group_" + i + "' class='labels'>" + group.title + "</label>";
            html = this._addOpenDataStr(group, html);
            html += "</td></tr></table></div>";
          } else {
            html += "<div id=\"group_" + i + "_div\" style=\"display:block;\">" +
              "<table cellpadding='0' cellspacing='0' class='share-groups-table'><tr><td width='17' valign='bottom'>" +
              "<input id=\"group_" + i + "\" " + checked + " value=\"" + group.id + "\" type=\"checkbox\" " +
              "class='share-groups-input-checkbox' dojoType=\"dijit.form.Checkbox\" />" +
              "</td><td><label for='group_" + i + "' class='labels'>" + group.title + "</label>";
            html = this._addOpenDataStr(group, html);
            html += "</td></tr></table></div>";
          }
        }
 
        // add admin user groups if there are any that the user does not belong to already
        // admin's groups will only display if the lowest level of all items is account or public
        if (this.shareInfo.adminGroups && this.shareInfo.adminGroups.length && this.shareInfo.lowestAccess > 1) {
          html += "<div style=\"display:block;\"><hr/></div>";
 
          for (var m = 0; m < this.shareInfo.adminGroups.length; m++) {
            group = this.shareInfo.adminGroups[m];
            checked = "";
            count = this.shareInfo.items.length;
            if (this.shareInfo.itemsAreSharedToGroups.indexOf("," + group.id + ",") > -1) {
              checked = "checked";
              hasOne = true;
              for (var n = 0; n < groupCounter.length; n++) {
                if (groupCounter[n][0] === group.id) {
                  count = groupCounter[n][1];
                  break;
                }
              }
            }
            if (count < this.shareInfo.items.length) {
              // not all items are shared to this group
              html += "<div id=\"img_group_" + (m + i) + "_div\" style=\"display:block;\">" +
                "<table cellpadding='0' cellspacing='0' class='share-groups-table'><tr>" +
                "<td width='17' valign='bottom'>" +
                "<img id=\"img_group_" + (m + i) + "\" src=\"" + this._checkboxImgUrl + "\" border=\"0\">" +
                "</td><td>" + group.title + "</td></tr></table></div>" +
                "<div id=\"group_" + (m + i) + "_div\" style=\"display:none;\">" +
                "<table cellpadding='0' cellspacing='0'><tr><td width='17' valign='bottom'>" +
                "<input id=\"group_" + (m + i) + "\" " + checked + " value=\"" + group.id + "\" type=\"checkbox\"" +
                "class='share-groups-input-checkbox' dojoType=\"dijit.form.Checkbox\" />" +
                "</td><td><label for='group_" + i + "'>" + group.title + "</label>";
              html = this._addOpenDataStr(group, html);
              html += "</td></tr></table></div>";
            } else {
              html += "<div id=\"group_" + (m + i) + "_div\" style=\"display:block;\">" +
                "<table cellpadding='0' cellspacing='0' class='share-groups-table'><tr>" +
                "<td width='17' valign='bottom'>" +
                "<input id=\"group_" + (m + i) + "\" " + checked + " value=\"" + group.id + "\" type=\"checkbox\"" +
                "class='share-groups-input-checkbox' dojoType=\"dijit.form.Checkbox\" />" +
                "</td><td><label for='group_" + i + "'>" + group.title + "</label>";
              html = this._addOpenDataStr(group, html);
              html += "</td></tr></table></div>";
            }
          }
        }
 
        // it's too early to use the input element here
        this.own(on(dom.byId("share-groups-list"), "click", lang.hitch(this, this.checkGroup)));
        dom.byId("share-groups-list").innerHTML = html;
 
        if (this.shareInfo.organization) {
          var sharedToAccountCounter = 0;
          for (i = 0; i < this.shareInfo.items.length; i++) {
            if (this.shareInfo.items[i].sharing && this.shareInfo.items[i].sharing.access === "org") {
              sharedToAccountCounter++;
            }
          }
 
          if (!this.shareInfo._isCustomRole || this.shareInfo._roleCanShareToOrg ||
            this.shareInfo._roleCanShareOthersItemsToOrg) {
            if (sharedToAccountCounter === 0) {
              dijit.byId("share-account-check").set("checked", false);
              dijit.byId("share-account-check").set("disabled", false);
              domStyle.set(dom.byId("share-account-check_div"), "display", "block");
            } else if (sharedToAccountCounter === this.shareInfo.items.length) {
              dijit.byId("share-account-check").set("checked", true);
              dijit.byId("share-account-check").set("disabled", false);
              domStyle.set(dom.byId("share-account-check_div"), "display", "block");
            } else {
              dijit.byId("share-account-check").set("checked", true);
              dijit.byId("share-account-check").set("disabled", true);
              domStyle.set(dom.byId("share-account-check_div"), "display", "block");
            }
          } else {
            // share account enable
            dijit.byId("share-account-check").set("disabled", true);
            domStyle.set(dom.byId("share-account-check_div"), "display", "none");
          }
        }
 
        if (!this.shareInfo.organization || this.shareInfo._orgUserCanSharePublicOrOverride) {
          var sharedToEveryoneCounter = 0;
          for (i = 0; i < this.shareInfo.items.length; i++) {
            if (this.shareInfo.items[i].sharing && this.shareInfo.items[i].sharing.access === "public") {
              sharedToEveryoneCounter++;
            }
          }
          if (sharedToEveryoneCounter === 0) {
            dijit.byId("share-everyone-check").set("checked", false);
            domStyle.set(dom.byId("share-everyone-check_div"), "display", "block");
          } else if (sharedToEveryoneCounter === this.shareInfo.items.length) {
            dijit.byId("share-everyone-check").set("checked", true);
            domStyle.set(dom.byId("share-everyone-check_div"), "display", "block");
            if (this.shareInfo.organization &&
              (!this.shareInfo._isCustomRole || this.shareInfo._roleCanShareToOrg ||
              this.shareInfo._roleCanShareOthersItemsToOrg)) {
              dijit.byId("share-account-check").set("checked", true);
              dijit.byId("share-account-check").set("disabled", dijit.byId("share-everyone-check").get("checked"));
            }
          } else {
            dijit.byId("share-everyone-check").set("checked", true);
            if (this.shareInfo.organization &&
              (!this.shareInfo._isCustomRole || this.shareInfo._roleCanShareToOrg ||
              this.shareInfo._roleCanShareOthersItemsToOrg)) {
              dijit.byId("share-account-check").set("checked", true);
              dijit.byId("share-account-check").set("disabled", dijit.byId("share-everyone-check").get("checked"));
            }
          }
          this.clickEveryone();
          //this.own(on(dom.byId("share-everyone-check"), "click", lang.hitch(this, this.clickEveryone)));
        } else {
          dijit.byId("share-everyone-check").set("checked", false);
          domStyle.set(dom.byId("share-everyone-check_div"), "display", "none");
        }
 
        if (hasOne) {
          dijit.byId("share-groups-check").set("checked", true);
        } else {
          dijit.byId("share-groups-check").set("checked", false);
        }
        this.own(on(dom.byId("share-groups-check"), "click", lang.hitch(this, this.checkGroups)));
 
        if (this.shareInfo.organization &&
          (!this.shareInfo._isCustomRole || this.shareInfo._roleCanShareToOrg ||
          this.shareInfo._roleCanShareOthersItemsToOrg)) {
          this.own(on(dom.byId("share-account-check"), "click", lang.hitch(this, this.clickAccount)));
        }
 
        //dojo.publish("shareOptionsSetup");
      },
      buildList: function(/*counter*/) {
        var i, j;
 
        if (!this.shareInfo._isCustomRole || this.shareInfo._roleCanShareToGroup ||
          this.shareInfo._roleCanShareOthersItemsToGroup) {
          if (this.shareInfo.userGroups && this.shareInfo.userGroups.length > 0) {
            domStyle.set(dom.byId("share-groups-div"), "display", "block");
          } else {
            //dojo.publish("notMemberOfAnyGroups");
          }
        } else {
          domStyle.set(dom.byId("share-groups-div"), "display", "none");
        }
 
        this.shareInfo.itemsAreSharedToGroups = ",";
        var groupCounter = [];
        for (i = 0; i < this.shareInfo.items.length; i++) {
          var item = this.shareInfo.items[i];
          //this.itemsInfo["_" + item.id] = {};
          //this.itemsInfo["_" + item.id].itemsIsSharedToGroups = ",";
          //eval("this.itemsInfo._" + item.id + " = new Object();");
          //eval("this.itemsInfo._" + item.id).itemsIsSharedToGroups = ",";
          if (item.sharing) {
            for (var k = 0; k < item.sharing.groups.length; k++) {
              var groupId = item.sharing.groups[k];
              //this.itemsInfo["_" + item.id].itemsIsSharedToGroups += groupId + ",";
              //eval("this.itemsInfo._" + item.id).itemsIsSharedToGroups += groupId + ",";
              if (this.shareInfo.itemsAreSharedToGroups.indexOf("," + groupId + ",") === -1) {
                this.shareInfo.itemsAreSharedToGroups += groupId + ",";
                var cntr = [];
                cntr[0] = groupId;
                cntr[1] = 1;
                groupCounter[groupCounter.length] = cntr;
              } else {
                for (j = 0; j < groupCounter.length; j++) {
                  if (groupCounter[j][0] === groupId) {
                    groupCounter[j][1] = groupCounter[j][1] + 1;
                    break;
                  }
                }
              }
            }
          }
        }
 
        var html = "";
        var hasOne = false;
        for (i = 0; i < this.shareInfo.userGroups.length; i++) {
          var group = this.shareInfo.userGroups[i];
          var checked = "";
          var count = this.shareInfo.items.length;
          if (this.shareInfo.itemsAreSharedToGroups.indexOf("," + group.id + ",") > -1) {
            checked = "checked";
            hasOne = true;
            for (j = 0; j < groupCounter.length; j++) {
              if (groupCounter[j][0] === group.id) {
                count = groupCounter[j][1];
                break;
              }
            }
          }
          if (count < this.shareInfo.items.length) {
            // not all items are shared to this group
            html += "<div id=\"img_group_" + i + "_div\" style=\"display:block;\">" +
              "<table cellpadding='0' cellspacing='0' class='share-groups-table'><tr><td width='17' valign='bottom'>" +
              "<img id=\"img_group_" + i + "\" src=\"" + this._checkboxImgUrl + "\" border=\"0\">" +
              "</td><td>" + group.title + "</td></tr></table></div>" +
              "<div id=\"group_" + i + "_div\" style=\"display:none;\">" +
              "<table cellpadding='0' cellspacing='0'><tr><td width='17' valign='bottom'>" +
              "<input id=\"group_" + i + "\" " + checked + " value=\"" + group.id + "\" type=\"checkbox\"" +
              "class='share-groups-input-checkbox' dojoType=\"dijit.form.Checkbox\" />" +
              "</td><td><label for='group_" + i + "'>" + group.title + "</label>";
            html = this._addOpenDataStr(group, html);
            html += "</td></tr></table></div>";
          } else {
            html += "<div id=\"group_" + i + "_div\" style=\"display:block;\">" +
              "<table cellpadding='0' cellspacing='0' class='share-groups-table'><tr><td width='17' valign='bottom'>" +
              "<input id=\"group_" + i + "\" " + checked + " value=\"" + group.id + "\" type=\"checkbox\"" +
              "class='share-groups-input-checkbox' dojoType=\"dijit.form.Checkbox\" />" +
              "</td><td><label for='group_" + i + "'>" + group.title + "</label>";
            html = this._addOpenDataStr(group, html);
            html += "</td></tr></table></div>";
          }
        }
 
        // it's too early to use the input element here
        this.own(on(dom.byId("share-groups-list"), "click", lang.hitch(this, this.checkGroup)));
        dom.byId("share-groups-list").innerHTML = html;
 
        if (this.shareInfo.organization && this.shareInfo.userRole &&
          (!this.shareInfo._isCustomRole || this.shareInfo._roleCanShareToOrg ||
          this.shareInfo._roleCanShareOthersItemsToOrg)) {
          var sharedToAccountCounter = 0;
          for (i = 0; i < this.shareInfo.items.length; i++) {
            if (this.shareInfo.items[i].sharing && this.shareInfo.items[i].sharing.access === "org") {
              sharedToAccountCounter++;
            }
          }
          if (sharedToAccountCounter === 0) {
            dijit.byId("share-account-check").set("checked", false);
            domStyle.set(dom.byId("share-account-check_div"), "display", "block");
          } else if (sharedToAccountCounter === this.shareInfo.items.length) {
            dijit.byId("share-account-check").set("checked", true);
            domStyle.set(dom.byId("share-account-check_div"), "display", "block");
          } else {
            dijit.byId("share-account-check").set("checked", true);
            domStyle.set(dom.byId("share-account-check_div"), "display", "block");
          }
        }
 
        if (!this.shareInfo.organization || this.shareInfo._orgUserCanSharePublicOrOverride) {
          // only a real admin (not custom with canUpdateOrgItems) can overwrite that canSharePublic setting on the org, and only if the admin is in the same org as the item
          var sharedToEveryoneCounter = 0;
          for (i = 0; i < this.shareInfo.items.length; i++) {
            if (this.shareInfo.items[i].sharing && this.shareInfo.items[i].sharing.access === "public") {
              sharedToEveryoneCounter++;
            }
          }
          if (sharedToEveryoneCounter === 0) {
            dijit.byId("share-everyone-check").set("checked", false);
            domStyle.set(dom.byId("share-everyone-check_div"), "display", "block");
          } else if (sharedToEveryoneCounter === this.shareInfo.items.length) {
            dijit.byId("share-everyone-check").set("checked", true);
            domStyle.set(dom.byId("share-everyone-check_div"), "display", "block");
 
            if (this.shareInfo.organization && this.shareInfo.userRole &&
              (!this.shareInfo._isCustomRole || this.shareInfo._roleCanShareToOrg ||
              this.shareInfo._roleCanShareOthersItemsToOrg)) {
              dijit.byId("share-account-check").set("checked", true);
              domStyle.set(dom.byId("share-everyone-check_div"), "display", "block");
            }
          } else {
            dijit.byId("share-everyone-check").set("checked", true);
 
            if (this.shareInfo.organization && this.shareInfo.userRole &&
              (!this.shareInfo._isCustomRole || this.shareInfo._roleCanShareToOrg ||
              this.shareInfo._roleCanShareOthersItemsToOrg)) {
              dijit.byId("share-account-check").set("checked", true);
              domStyle.set(dom.byId("share-everyone-check_div"), "display", "block");
            }
          }
          this.clickEveryone();
          //this.own(on(dom.byId("share-everyone-check"), "click", lang.hitch(this, this.clickEveryone)));
        } else if (this.shareInfo.organization && !this.shareInfo.organization.canSharePublic) {
          var sharedToEveryoneCounter_org = 0;
          for (i = 0; i < this.shareInfo.items.length; i++) {
            if (this.shareInfo.items[i].sharing && this.shareInfo.items[i].sharing.access === "public") {
              sharedToEveryoneCounter_org++;
            }
          }
          if (sharedToEveryoneCounter_org) {
            dijit.byId("share-everyone-check").set("checked", true);
            //dojo.style(dojo.byId("share-everyone-check_div"), "display", "none");
            if (this.shareInfo.organization && this.shareInfo.userRole &&
              (!this.shareInfo._isCustomRole || this.shareInfo._roleCanShareToOrg ||
              this.shareInfo._roleCanShareOthersItemsToOrg)) {
              dijit.byId("share-account-check").set("checked", true);
              domStyle.set(dom.byId("share-account-check_div"), "display", "block");
            }
          } else {
            dijit.byId("share-everyone-check").set("checked", false);
            domStyle.set(dom.byId("share-everyone-check_div"), "display", "none");
          }
        } else {
          dijit.byId("share-everyone-check").set("checked", false);
          domStyle.set(dom.byId("share-everyone-check_div"), "display", "none");
        }
 
        if (hasOne) {
          dijit.byId("share-groups-check").set("checked", true);
        } else {
          dijit.byId("share-groups-check").set("checked", false);
        }
        this.own(on(dom.byId("share-groups-check"), "click", lang.hitch(this, this.checkGroups)));
 
        if (this.shareInfo.organization &&
          (!this.shareInfo._isCustomRole || this.shareInfo._roleCanShareToOrg ||
          this.shareInfo._roleCanShareOthersItemsToOrg)) {
          this.own(on(dom.byId("share-account-check"), "click", lang.hitch(this, this.clickAccount)));
        }
 
        //dojo.publish("shareOptionsSetup");
      },
      buildGroupList: function(/*counter*/) {
        var i, j;
        // for (i = 0; i < this.items.length; i++) {
        //   if (!this.items[i].sharing && counter < 20) {
        //     // wait longer
        //     counter = counter + 1;
        //     setTimeout(dojo.hitch(this, "buildGroupList", counter), 100);
        //     return;
        //   }
        // }
 
        if (!this.shareInfo._isCustomRole || this.shareInfo._roleCanShareToGroup ||
          this.shareInfo._roleCanShareOthersItemsToGroup) {
          if (this.shareInfo.userGroups.length > 0) {
            domStyle.set(dom.byId("share-groups-div"), "display", "block");
          } else {
            //dojo.publish("doesNotOwnGroups");
          }
        } else {
          domStyle.set(dom.byId("share-groups-div"), "display", "none");
        }
 
        // determine which items are shared to groups
        this.shareInfo.itemsAreSharedToGroups = ",";
        var groupCounter = [];
        for (i = 0; i < this.shareInfo.items.length; i++) {
          var item = this.shareInfo.items[i];
 
          //this.itemsInfo["_" + item.id] = {};
          //this.itemsInfo["_" + item.id].itemsIsSharedToGroups = ",";
          //eval("this.itemsInfo._" + item.id + " = new Object();");
          //eval("this.itemsInfo._" + item.id).itemsIsSharedToGroups = ",";
          if (item.sharing) {
            for (var k = 0; k < item.sharing.groups.length; k++) {
              var groupId = item.sharing.groups[k];
              //this.itemsInfo["_" + item.id].itemsIsSharedToGroups += groupId + ",";
              //eval("this.itemsInfo._" + item.id).itemsIsSharedToGroups += groupId + ",";
              if (this.shareInfo.itemsAreSharedToGroups.indexOf("," + groupId + ",") === -1) {
                this.shareInfo.itemsAreSharedToGroups += groupId + ",";
                var cntr = [];
                cntr[0] = groupId;
                cntr[1] = 1;
                groupCounter[groupCounter.length] = cntr;
              } else {
                for (j = 0; j < groupCounter.length; j++) {
                  if (groupCounter[j][0] === groupId) {
                    groupCounter[j][1] = groupCounter[j][1] + 1;
                    break;
                  }
                }
              }
            }
          }
        }
 
        var html = "";
        var hasOne = false;
        var group;
        var checked;
        var count;
        for (i = 0; i < this.shareInfo.userGroups.length; i++) {
          group = this.shareInfo.userGroups[i];
          checked = "";
          count = this.shareInfo.items.length;
          if (this.shareInfo.itemsAreSharedToGroups.indexOf("," + group.id + ",") > -1) {
            checked = "checked";
            hasOne = true;
            for (j = 0; j < groupCounter.length; j++) {
              if (groupCounter[j][0] === group.id) {
                count = groupCounter[j][1];
                break;
              }
            }
          }
          if (count < this.shareInfo.items.length) {
            // not all items are shared to this group
            html += "<div id=\"img_group_" + i + "_div\" style=\"display:block;\">" +
              "<table cellpadding='0' cellspacing='0' class='share-groups-table'><tr><td width='17' valign='bottom'>" +
              "<img id=\"img_group_" + i + "\" src=\"" + this._checkboxImgUrl + "\" border=\"0\">" +
              "</td><td>" + group.title + "</td></tr></table></div><div id=\"group_" + i + "_div\"" +
              "style=\"display:none;\">" +
              "<table cellpadding='0' cellspacing='0'>" +
              "<tr><td width='17' valign='bottom'>" +
              "<input id=\"group_" + i + "\" " + checked + " value=\"" + group.id + "\" type=\"checkbox\"" +
              "class='share-groups-input-checkbox' dojoType=\"dijit.form.Checkbox\" />" +
              "</td><td><label for='" + group.id + "'>" + group.title + "</label>";
            html = this._addOpenDataStr(group, html);
            html += "</td></tr></table></div>";
          } else {
            html += "<div id=\"group_" + i + "_div\" style=\"display:block;\">" +
              "<table cellpadding='0' cellspacing='0' class='share-groups-table'><tr><td width='17' valign='bottom'>" +
              "<input id=\"group_" + i + "\" " + checked + " value=\"" + group.id + "\" type=\"checkbox\"" +
              "class='share-groups-input-checkbox' dojoType=\"dijit.form.Checkbox\" />" +
              "</td><td><label for='group_" + i + "'>" + group.title + "</label>";
            html = this._addOpenDataStr(group, html);
            html += "</td></tr></table></div>";
          }
        }
 
        // it's too early to use the input element here
        this.own(on(dom.byId("share-groups-list"), "click", lang.hitch(this, this.checkGroup)));
        dom.byId("share-groups-list").innerHTML = html;
 
        if (hasOne) {
          dijit.byId("share-groups-check").set("checked", true);
        } else {
          dijit.byId("share-groups-check").set("checked", false);
        }
        this.own(on(dom.byId("share-groups-check"), "click", lang.hitch(this, this.checkGroups)));
        //dojo.publish("shareOptionsSetup");
      },
 
      share: function(e) {
        if (this.shareInfo.shareType === "group") {
          this.shareItemToGroups();
        } else if (this.shareInfo.shareType === "admin") {
          this.shareItemAsAdmin(e);
        } else { // item
          this.shareItem(e);
        }
      },
      checkGroup: function(e) {
        if (e.target.id.indexOf("img_group_") > -1) {
          domStyle.set(dom.byId(e.target.id + "_div"), "display", "none");
          domStyle.set(dom.byId(e.target.id.substring(4, e.target.id.length) + "_div"), "display", "block");
        }
 
        if (e.target.checked === true && e.target.id.indexOf("group_") > -1) {
          dijit.byId("share-groups-check").set("checked", true);
        }
        //dojo.publish("onClickGroup");
        //topic.publish('ShareOptions/onShareCheckChanged');
      },
      checkGroups: function() {
        //dojo.publish("onClickGroup");
        //topic.publish('ShareOptions/onShareCheckChanged');
      },
      clickEveryone: function(/*e*/) {
        //dojo.stopEvent(e);
        var isChecked = dijit.byId("share-everyone-check").get('checked');
        if (isChecked === true) { // checkbox checked
          var shareAccountCheckDiv = dom.byId("share-account-check_div");
          if (this.shareInfo.organization && shareAccountCheckDiv &&
            domStyle.get(shareAccountCheckDiv, "display") === "block") {
            dijit.byId("share-account-check").set("checked", true);
            dijit.byId("share-account-check").set("disabled", true);
          }
        } else { // checkbox unchecked
          if (this.shareInfo.organization && this.shareInfo.userRole &&
            (!this.shareInfo._isCustomRole || this.shareInfo._roleCanShareToOrg ||
            this.shareInfo._roleCanShareOthersItemsToOrg)) {
            domStyle.set(dom.byId("share-account-check_div"), "display", "block");
          }
          dijit.byId("share-account-check").set("disabled", false);
        }
        //dojo.publish("onClickEveryone");
        topic.publish('ShareOptions/onClickEveryone', isChecked);
        //topic.publish('ShareOptions/onShareCheckChanged');
      },
      clickAccount: function(/*e*/) {
        //dojo.stopEvent(e);
        if (dijit.byId("share-account-check").get('checked') === true) {
          if (this.shareInfo._orgUserCanSharePublicOrOverride) {
            // only a real admin (not custom with canUpdateOrgItems) can overwrite that canSharePublic setting on the org, and only if the admin is in the same org as the item
            if (domStyle.get(dom.byId("share-everyone-check_div"), "display") === "none") {
              domStyle.set(dom.byId("share-everyone-check_div"), "display", "block");
            }
            dijit.byId("share-everyone-check").set("checked", false);
          }
          dijit.byId("share-account-check").set("checked", true);
        } else { // account unchecked
          dijit.byId("share-account-check").set("checked", false);
        }
        //dojo.publish("onClickAccount");
        //topic.publish('ShareOptions/onShareCheckChanged');
      },
 
      shareItemAsAdmin: function(/*e*/) {
        var itemIds = "";
        var comma = "";
        array.forEach(this.shareInfo.items, function(item) {
          itemIds += comma + item.id;
          comma = ",";
        });
 
        var shareWithEveryone = false;
        var shareWithAccount = false;
        if ((dom.byId("share-everyone-check_div") &&
          domStyle.get(dom.byId("share-everyone-check_div"), "display") === "block")) {
          shareWithEveryone = dijit.byId("share-everyone-check").get('checked');
        }
        if ((dom.byId("share-account-check_div") &&
          domStyle.get(dom.byId("share-account-check_div"), "display") === "block")) {
          shareWithAccount = dijit.byId("share-account-check").get('checked');
        }
 
        var shareWithGroups = dijit.byId("share-groups-check").get('checked');
        var shareWithTheseGroups = "";
        var unshareWithTheseGroups = "";
        var comma1 = "";
        var comma2 = "";
        var i = 0;
        if (shareWithGroups) {
          var id;
          if (this.shareInfo.user.groups && this.shareInfo.user.groups.length) {
            for (i = 0; i < this.shareInfo.user.groups.length; i++) {
              id = this.shareInfo.user.groups[i].id;
              if (domStyle.get(dom.byId("group_" + i + "_div"), "display") === "block") {
                if (dom.byId("group_" + i).checked) {
                  shareWithTheseGroups += comma1 + id;
                  comma1 = ",";
                } else {
                  unshareWithTheseGroups += comma2 + id;
                  comma2 = ",";
                }
              }
            }
          }
 
          if (this.shareInfo.adminGroups && this.shareInfo.adminGroups.length && this.shareInfo.lowestAccess > 1) {
            for (var m = 0; m < this.shareInfo.adminGroups.length; m++) {
              id = this.shareInfo.adminGroups[m].id;
              if (domStyle.get(dom.byId("group_" + (m + i) + "_div"), "display") === "block") {
                if (dom.byId("group_" + (m + i)).checked) {
                  shareWithTheseGroups += comma1 + id;
                  comma1 = ",";
                } else {
                  unshareWithTheseGroups += comma2 + id;
                  comma2 = ",";
                }
              }
            }
          }
        } else {
          // don't share to any group
          comma2 = "";
          if (this.shareInfo.user.groups && this.shareInfo.user.groups.length) {
            array.forEach(this.shareInfo.user.groups, function(group) {
              unshareWithTheseGroups += comma2 + group.id;
              comma2 = ",";
            });
          }
 
          if (this.shareInfo.adminGroups && this.shareInfo.adminGroups.length && this.shareInfo.lowestAccess > 1) {
            array.forEach(this.shareInfo.adminGroups, function(group) {
              unshareWithTheseGroups += comma2 + group.id;
              comma2 = ",";
            });
          }
        }
 
        // groups: A comma separated list of group IDs that the item will be shared with or ""
        if (unshareWithTheseGroups.length > 0) {
          var unShareRequest = {
            "items": itemIds,
            "groups": unshareWithTheseGroups
          };
          for (i = 0; i < this.shareInfo.items.length; i++) {
            var item = this.shareInfo.items[i];
            //'content/users/' + (request.owner || user.email) + '/unshareItems';
            //arcgisonline.sharing.geow.Content.unshareItemByID(item, unShareRequest,dojo.hitch(this, unShareHandler, this.items, shareWithTheseGroups, shareWithEveryone, shareWithAccount));
            shareUtils._unshareItemById(unShareRequest, item.id, this.portalUrl).then(lang.hitch(
              this, this.unShareHandler_admin,
              this.shareInfo.items, itemIds, shareWithTheseGroups, shareWithEveryone, shareWithAccount)
            );
          }
        } else {
          this.unShareHandler_admin(this.shareInfo.items, itemIds,
            shareWithTheseGroups, shareWithEveryone, shareWithAccount);
        }
      },
      unShareHandler_admin: function(_items, itemIds, _shareWithTheseGroups, _shareWithEveryone, _shareWithAccount) {
        var totalRequests = _items.length;
        var shareHandler = function() {
          totalRequests--;
          if (totalRequests === 0) {
            //dojo.publish("onShareUpdate", [_shareWithEveryone ? "public" :
            //  (_shareWithAccount ? "org" : (_shareWithTheseGroups ? "shared" : "private"))]);
          }
        };
 
        // groups: A comma separated list of group IDs that the item will be shared with or ""
        var shareRequest = {
          "items": itemIds,
          "groups": _shareWithTheseGroups,
          "everyone": _shareWithEveryone,
          "account": _shareWithAccount
        };
        for (var i = 0; i < _items.length; i++) {
          var item = _items[i];
          //'content/items/' + item.id + '/share';
          //arcgisonline.sharing.geow.Content.shareItemByID(item, shareRequest, shareHandler);
          this.shareInfo.user.shareItem(shareRequest, item.id, item.folderId).then(lang.hitch(this, shareHandler));
        }
      },
 
      shareItem: function(/*e*/) {
        var itemIds = "";
        var comma = "";
        array.forEach(this.shareInfo.items, function(item) {
          itemIds += comma + item.id;
          comma = ",";
        });
 
        var shareWithEveryone = false;
        var shareWithAccount = false;
        if ((dom.byId("share-everyone-check_div") &&
          domStyle.get(dom.byId("share-everyone-check_div"), "display") === "block")) {
          shareWithEveryone = dijit.byId("share-everyone-check").get('checked');
        }
        if ((dom.byId("share-account-check_div") &&
          domStyle.get(dom.byId("share-account-check_div"), "display") === "block")) {
          shareWithAccount = dijit.byId("share-account-check").get('checked');
        }
 
        var shareWithGroups = dijit.byId("share-groups-check").get('checked');
        var shareWithTheseGroups = "";
        var unshareWithTheseGroups = "";
        var comma1 = "";
        var comma2 = "";
        this.shareInfo.userGroups = this.shareInfo.user.group;
        if (shareWithGroups) {
          if (this.shareInfo.userGroups && this.shareInfo.userGroups.length) {
            array.forEach(this.shareInfo.userGroups, function(group, i) {
              var id = group.id;
              if (domStyle.get(dom.byId("group_" + i + "_div"), "display") === "block") {
                if (dom.byId("group_" + i).checked) {
                  shareWithTheseGroups += comma1 + id;
                  comma1 = ",";
                } else {
                  unshareWithTheseGroups += comma2 + id;
                  comma2 = ",";
                }
              }
            });
          }
        } else {
          // don't share to any group
          comma2 = "";
          if (this.shareInfo.userGroups && this.shareInfo.userGroups.length) {
            array.forEach(this.shareInfo.userGroups, function(group) {
              unshareWithTheseGroups += comma2 + group.id;
              comma2 = ",";
            });
          }
        }
 
        // var shareErrorHandler = function(errors) {
        //   dojo.publish("onShareUpdate", [errors]);
        // };
 
        // groups: A comma separated list of group IDs that the item will be shared with or ""
        if (unshareWithTheseGroups.length > 0) {
          var unShareRequest = {
            "items": itemIds,
            "groups": unshareWithTheseGroups
          };
          if (this.shareInfo.itemUser && this.shareInfo.itemUser !== null) {
            var username;
            if (this.shareInfo.itemUser.username) {
              username = this.shareInfo.itemUser.username;
            } else {
              username = this.shareInfo.itemUser.email;
            }
            //shareUtils.unshareItemsByUser(username, unShareRequest,
            //  dojo.hitch(this, unShareHandler, this.itemUser, shareWithTheseGroups, shareWithEveryone, shareWithAccount));
            shareUtils.unshareItemsByUser(username, unShareRequest, this.portalUrl).then(lang.hitch(
              this, this.unShareHandler_item,
              this.shareInfo.itemUser, itemIds, shareWithTheseGroups, shareWithEveryone, shareWithAccount)
            );
          } else {
            // arcgisonline.sharing.geow.Content.unshareItems(unShareRequest,
            //   dojo.hitch(this, unShareHandler, this.itemUser, shareWithTheseGroups, shareWithEveryone, shareWithAccount)
            // );
            shareUtils.unshareItems(this.shareInfo.itemUser, unShareRequest, this.portalUrl).then(lang.hitch(
              this, this.unShareHandler_item,
              this.shareInfo.itemUser, itemIds, shareWithTheseGroups, shareWithEveryone, shareWithAccount)
            );
          }
        } else {
          this.unShareHandler_item(this.shareInfo.itemUser, itemIds,
            shareWithTheseGroups, shareWithEveryone, shareWithAccount);
        }
        //dojo.publish("shareOptionsTeardown");
      },
      unShareHandler_item: function(_itemUser, itemIds, _shareWithTheseGroups, _shareWithEveryone, _shareWithAccount) {
        // var shareHandler = function(){
        //   dojo.publish("onShareUpdate", [_shareWithEveryone?"public":(_shareWithAccount?"org":(_shareWithTheseGroups?"shared":"private"))]);
        // };
        // groups: A comma separated list of group IDs that the item will be shared with or ""
        var shareRequest = {
          "items": itemIds,
          "groups": _shareWithTheseGroups,
          "everyone": _shareWithEveryone,
          "account": _shareWithAccount
        };
        if (_itemUser && _itemUser !== null) {
          var username;
          if (_itemUser.username) {
            username = _itemUser.username;
          } else {
            username = _itemUser.email;
          }
          //arcgisonline.sharing.geow.Content.shareItemsByUser(username, shareRequest, shareHandler);
          shareUtils.shareItemsByUser(username, shareRequest, this.portalUrl).then(lang.hitch());
        } else {
          //arcgisonline.sharing.geow.Content.shareItems(shareRequest, shareHandler, shareErrorHandler);
          shareUtils.shareItems(_itemUser, shareRequest, this.portalUrl).then();
        }
      },
 
      findLowestAccessLevel: function(items) {
        var lowestAccess = 3;
        // determine lowest share level
        var accessLevel;
        var access;
        for (var i = 0; i < items.length; i++) {
          access = items[i].access;
          if (!access) {
            access = "private";
          }
          if (access.length === 1) {
            // convert from array (items from a DataGrid come with each property as an array value)
            access = access[0];
          }
          if (access === "public") {
            accessLevel = 3;
          } else if (access === "org") {
            accessLevel = 2;
          } else if (access === "shared") {
            accessLevel = 1;
          } else if (access === "private") {
            accessLevel = 0;
          }
          if (accessLevel < lowestAccess) {
            lowestAccess = accessLevel;
          }
        }
        return lowestAccess;
      },
 
      _sortFunc: function(a, b) {
        var aa = a[this.shareInfo.sortField].toLowerCase();
        var bb = b[this.shareInfo.sortField].toLowerCase();
        if (aa === null || bb === null || aa === bb) {
          return 0;
        }
        if (aa < bb) {
          return -1;
        }
        return 1;
      },
 
      _addOpenDataStr: function(group, html) {
        if (group.isOpenData && !shareUtils.portal.isPortal) {
          html += " " + this.i18n.openData;
        }
        return html;
      },
 
      _clearGroupCheckboxes: function() {
        if (false === dijit.byId("share-groups-check").get("checked")) {
          var groupCheckBoxes = query(".share-groups-input-checkbox");
          for (var i = 0, len = groupCheckBoxes.length; i < len; i++) {
            var cb = groupCheckBoxes[i];
            cb.setAttribute('value', false);
          }
        }
      }
    });
    return so;
  });